#!/bin/sh
PROCESSOR=Hager-ARMv7

# mbsa trap cleanup funtion
cleanup()
{
  echo
  echo "mbsae.core terminated unexpectedly..."
  sleep 1

  # uncomment to ensure mbsae.core threads are all killed
  #echo " - Killing mbsae.core ..."; killall -9 mbsae.core

  # uncomment to ensure jvm is killed after mbsa termination
  #echo " - Killing JVM..."; killall -9 j9

  # Cleanup default sync file 'mbsa_sync_core.snc'
  echo " - Clenaup sync: $MBSA_DIR/mbsa_sync_core.snc"
  rm -f $MBSA_DIR/mbsa_sync_core.snc

  echo
  echo
}


# dirname could be missing, so prevent changing dir to $HOME
MBSA_DIR=`dirname $0 2>/dev/null`
[ -n "$MBSA_DIR" ] && cd $MBSA_DIR

## try to parse ip from ifconfig (Change eth0 to another interface if required)
#IP=`ifconfig eth0 | grep 'inet addr:' | cut -f 2 -d: | cut -d' ' -f 1`
#[ -n "$IP" ] && export MBSA_HOST_IP=$IP

# setup LD_LIBRARY_PATH
if [ -z "$PROCESSOR" ]; then PROCESSOR=i386; fi
export LD_LIBRARY_PATH="../lib/runtimes/linux/$PROCESSOR:../lib/runtimes/linux/$PROCESSOR/plugins:$LD_LIBRARY_PATH"

echo "Starting mBSA [`pwd`/../lib/runtimes/linux/$PROCESSOR/mbsae.core $@]..."
[ -n "$MBSA_HOST_IP" ] && echo "Using MBSA_HOST_IP: $MBSA_HOST_IP"

## Uncomment for auto updating of main mbsa TargetID, when external ip is changed
## MBSA_AUTO_IP Format is "#" for using hostname to resolve external ip, or filename e.g. "/tmp/.mbsa.host.ip" to read IP when changed
# export MBSA_AUTO_IP=#

if [ "$MBSA_AUTO_IP" = "#" ]; then 
  echo "Using MBSA_AUTO_IP: from hostname (`hostname`)"
else
  [ -n "$MBSA_AUTO_IP" ] && echo "Using MBSA_AUTO_IP: from file: $MBSA_AUTO_IP"
fi

## mBSA modules print on console enablers
# export LOG_MBSMANAGER=1
# export LOG_NRUNNER=1 NRUNNER_DEBUG=3

## Global mBSA log disable
# export MBSA_LOG_DISABLED=1

## Uncomment to override default mBSA crash log dir "."
#export MBSA_CRASHDIR=/tmp

export LOG_ENABLED="true"
[ "$MBSA_LOG_DISABLED" = "1" ] && export LOG_ENABLED="false" 

# mBSA Log configuration
export LOG_CFG="core.log.maxsize=250 core.log.parts=2 core.log.maxcount=10 core.log.enable=$LOG_ENABLED"


# uncomment to enable cleanup after mbsa crash
#trap cleanup SIGINT SIGTERM SIGABRT SIGKILL SIGSEGV SIGQUIT SIGILL SIGBUS

../lib/runtimes/linux/$PROCESSOR/mbsae.core mbsa.cmd=start core.plugins.dir=../lib/runtimes/linux/$PROCESSOR/plugins mbsa.log.file=./logs/mbsa_start.log core.log.file=./logs/core.log core.prs.file=./configs/linux/mbsal.core.prs $LOG_CFG $*
rc=$?
echo "mBSA exit code: $rc"
exit $rc

##########
# code 0 - mbsa started and exitted normally
# code 1 - mbsa start failed (e.g. configuration issues)
# code 2 - mbsa already started
