#!/bin/bash
#
# Startup script for Verax SNMP Simulator
# Copyright (c) Verax Systems
# 
# chkconfig: 5 98 55
# description: Verax SNMP Simulator service
#


#######################################
# Please verify the configuration     # 
# file location.                      #
#######################################

# Path to configuration file (e.g. '/etc/verax.d/simulator.conf')
CONF_SIM_FILE='/etc/verax.d/simulator.conf'


# Please do not change the script beyond this line!

for line in $(cat ${CONF_SIM_FILE}|grep -v '^#')
do
	eval "VS_${line}"
done

RUNCMD=""
if [ "" != "$VS_SERVER_PORT" ] 
then
	RUNCMD="${RUNCMD} -p ${VS_SERVER_PORT}"
fi

if [ "" != "$VS_PRIMARY_INTERFACE" ] 
then
	RUNCMD="${RUNCMD} -d ${VS_PRIMARY_INTERFACE}"
fi

if [ "" != "$VS_SIMULATOR_CONFIG_FILE" ] 
then
	RUNCMD="${RUNCMD} -f ${VS_SIMULATOR_CONFIG_FILE}"
fi

cd $VS_SIMULATOR_HOME

case "$1" in
    console)
	${VS_JRE_HOME}java -jar $VS_SIMULATOR_HOME/jar/snmp-simulator-rmi-client.jar $@
	;;
    start) 
        echo "Verax SNMP Simulator ver. 1.2.0. Copyright (c) Verax Systems. All rights reserved."
	ulimit -n 100000
	nohup ${VS_JRE_HOME}java -jar $VS_SIMULATOR_HOME/jar/snmp-simulator-server.jar $RUNCMD &> /dev/null &
	
        echo "Verax SNMP Simulator started. Loading network configuration..."
        sleep 2
        ;;
    stop)
        echo "Stopping Verax SNMP Simulator..."
        cd $VS_SIMULATOR_HOME/conf
	./stop "$RUNCMD"
        ;;
    status)
        status=`ps -ef | grep "snmp-simulator-server.jar$RUNCMD" | grep -v grep`
        if [ "$status" == "" ]
        then
            echo "Verax SNMP Simulator stopped."
        else
            echo "Verax SNMP Simulator running."
            echo "Number of virtual interfaces created: `cat /tmp/created_interfaces.txt | wc -l`."
        fi
        exit 0
        ;;
    *)
        echo "Usage: $0 {console|start|stop|status}"
        exit 1
        ;;
esac

exit 0
