#! /bin/sh

#############################################################
# ISTI.com NSI custom starting script for UNIX servers
#############################################################

NSIname=menlo
PIDFILE=${NSIname}NSI.pid
date

# the following three env vars should be set to your hosts setup

# where is java
JHOME=/ebird/isti/j2re1.4.2_03
# where is my code (class files)
SNWHOME=/ebird/isti/menloNSI
# where are my config and logging files
NSI=/ebird/isti/${NSIname}NSI


# name of conf file (standard)
CONF=NSI.conf
# size of NSI JVM 
MEM="-server -Xms100m -Xmx250m"
# orbconfig name
OC=${NSI}/conf/orb.config

# for solaris we need the old style ps, this should not effect linux
PATH=/usr/ucb:$PATH

#############################################################
# DO NOT EDIT ANYTHING BELOW THESE LINES (thank you)
#############################################################

this=$0

Usage="usage: NSI [start|stop|restart|stopec]"

#############################################################
# check for required command line args
#############################################################

if [ $# != "1" ]; then
	echo "Warning NSI missing directive argument"
	echo $Usage
	exit 1
fi

if [ $1 != "start" -a  $1 != "restart" -a  $1 != "stop" -a $1 != "stopec" ]; then
	echo "Fatal Error: NSI directive $1 not understood"
	echo $Usage
	exit 1
fi


#############################################################
CLASSPATH=.:${SNWHOME}/jars/isti.snwserver.jar

PATH=$JHOME/bin:$PATH

export CLASSPATH
export PATH

java -version

any_ec_started=0



#############################################################
case "$1" in

'start')
    # cd to the home directory
    cd $NSI
    a=start
    echo "using $OC to start event channels"
    for port in `grep DefaultEventChannel $OC |cut -d':' -f3 |cut -d'/' -f1`
    do   
	if [ -f EC.port${port}.pid ]; then
		ps auxwww | grep `cat EC.port${port}.pid`|grep -v grep |grep ORB >/dev/null
		if [ $? -eq 0 -a -f EC.port${port}.pid ] ; then
			echo "EC already at port $port"
			a=0
		fi
	fi
	if [ "$a" = "start" ]; then
		echo "Starting Event Channel on port $port"
		java com.ooc.CosEventServer.Server -ORBconfig $OC -i -OAport $port  > /dev/null 2>&1 &
		echo $! > EC.port${port}.pid
		any_ec_started=1
	fi
    done


    # wait for the Event Channels to start
    if [ $any_ec_started -eq 1 ]; then
    	echo "Sleeping for 5 while event channels start"
    	sleep 5
    fi

    # check if we have one started already
    already_started=1
    if [ -f $PIDFILE ]; then
	echo "NSI pid file found"
    	ps auxwwwww | grep `cat $PIDFILE`|grep -v grep |grep baseDir >/dev/null
	already_started=$?
	#echo "already_started=$already_started"
    fi
    if [ $already_started -ne 0 ]; then
          java   ${MEM} com.isti.trinetwatch.server.Server --baseDir $NSI \
		-c $NSI/conf/$CONF --serverName ${NSIname} --daemon &

          echo $! >$PIDFILE
	  echo "NSI started"
    else 
	  echo "NSI previously running"
    fi
	
;;

'restart')
    # echo "restarting not supported yet"
    /bin/sh $this stop
    /bin/sh $this start
    echo "restarted NSI"
;;

'stop')
    # cd to the home directory
    cd $NSI
    echo "killing NSI named $NSIname"
    if [ ! -f $PIDFILE ] ; then
	echo "NSI named $NSIname .pid file not found"
	echo "Is this NSI even running?"
	exit 1
    fi
    kill `cat $PIDFILE`
    rm $PIDFILE
;;

'stopec')
    # cd to the home directory
    cd $NSI
    echo "killing NSI's $NSIname event channels"
    for port in `grep DefaultEventChannel $OC |cut -d':' -f3 |cut -d'/' -f1`
    do   
	if [ ! -f EC.port${port}.pid ] ; then
		echo "EC at port $port did not have a pid file"
	else
		kill `cat EC.port${port}.pid`
		rm EC.port${port}.pid 
	fi
    done
    echo "done stopping event channels"
;;

esac
