#!/bin/sh

# check for required variables from server_common script
[ -z "$MBS_ROOT" ] && echo '[WARNING] MBS_ROOT variable is not set!'

# setup mBSA root (relative to fwk MBS_ROOT variable)
#  export MBSA_ROOT=$MBS_ROOT
# ###[IM]### export MBSA_ROOT=$MBS_ROOT/../mbsa

export MBSA_ROOT=$MBS_ROOT/../mbsa

# setup LD_LIBRARY_PATH
if [ -z "$PROCESSOR" ]; then PROCESSOR=Hager-ARMv7; fi

# avoid setting "::" in MBS_SERVER_JAR, which causes warnings
if [ -n "$MBS_SERVER_JAR" ]; then 
  export MBS_SERVER_JAR="$MBS_SERVER_JAR:$MBSA_ROOT/lib/mbsa.jar"
else
  export MBS_SERVER_JAR="$MBSA_ROOT/lib/mbsa.jar"
fi

export MBS_NATIVE_PATH="$MBS_NATIVE_PATH:$MBSA_ROOT/lib/runtimes/linux/$PROCESSOR:$MBSA_ROOT/lib/runtimes/linux/$PROCESSOR/plugins"

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

# mBSA Core setup
export FEATURES="$FEATURES -Dmbsa.lib.core.prs=$MBSA_ROOT/bin/java/configs/linux/mbsal.core.prs -Dmbsa.lib.core.plugins.dir=$MBSA_ROOT/lib/runtimes/linux/$PROCESSOR/plugins"

# Uncomment to enable mBSA FaultManager support
export FEATURES="$FEATURES -Dmbs.fm.class=com.prosyst.mbs.impl.framework.module.fm.MBSAFaultManagerImpl"

# mBSA console debug/stacktraces
#export FEATURES="$FEATURES -Dmbsa.debug=true -Dmbsa.events.debug=true -Dmbsa.stacktrace=true"

# Set mBSA java core log configuration
export FEATURES="$FEATURES -Dmbsa.lib.core.log=$MBSA_ROOT/bin/java/logs/mbsaj.core.log -Dmbsa.lib.core.log.parts=2 -Dmbsa.lib.core.log.maxsize=250 -Dmbsa.lib.core.log.maxcount=10 -Dmbsa.lib.core.log.enable=$LOG_ENABLED"

# Uncomment to force overriding of main mBSA TargetInfo address to localhost
export FEATURES="$FEATURES -Dmbsa.lib.tm.mbsa.override=127.0.0.1"

# mBS watchdog setup (ping timeout should be "mbs.manager.ping.timeout" / 2). mbs.mbsa.commsErrors (if > 0, mbs will exit after specified comms send errors)
export FEATURES="$FEATURES -Dmbs.comms=comms3 -Dmbs.mbsa.ping.timeout=30000 -Dmbs.mbsa.commsErrors=3"

# mBS NRuntime setup (should match mBSA nrunner plugin config)
export FEATURES="$FEATURES -Drunner.pipe.in=/tmp/.runner.ipc_i -Drunner.pipe.out=/tmp/.runner.ipc_o"

# Update Manager related configuration
if [ -d "$MBSA_ROOT/../update_storage" ]; then
  export FEATURES="$FEATURES -Dmbs.um.storage=$MBSA_ROOT/../update_storage"
fi
export FEATURES="$FEATURES -Dmbs.um.osUpdate=false"

echo
echo "MBS_CUR_DIR     : \"`pwd`\""
echo "MBS_NATIVE_PATH : \"$MBS_NATIVE_PATH\""
echo "MBSA_ROOT       : \"$MBSA_ROOT\""
echo "Update Storage  : \"$MBSA_ROOT/../update_storage\""
echo "Features        : \"$FEATURES\""
echo

# sanity checks...
[ -d $MBSA_ROOT ] || echo '[WARNING] MBS_ROOT path "$MBS_ROOT" is inconsistent!'
[ -e "$MBSA_ROOT/lib/mbsa.jar" ] || echo '[WARNING] "$MBSA_ROOT/lib/mbsa.jar" path is inconsistent!'
[ -e "$MBSA_ROOT/bin/java/configs/linux/mbsal.core.prs" ] || echo '[WARNING] "$MBSA_ROOT/bin/java/configs/linux/mbsal.core.prs" path is inconsistent!'
[ -e "$MBSA_ROOT/lib/runtimes/linux/$PROCESSOR/plugins" ] || echo '[WARNING] "$MBSA_ROOT/lib/runtimes/linux/$PROCESSOR/plugins" path is inconsistent!'

echo
