#! /bin/sh


# HARD CODED LOCATIONS:

ini=/etc/stations.ini


# argument 1  is the station name + net code CI-PAS
# argument 2 is the desired usage level 

#ul = 1 off  (no server running)
#ul = 2 eval (dlog only)
#ul = 3 full (dlog +cwda)


if [ $# -ne 2 ]; then
	echo usage_control sname ul
	echo " missing station name and usage level arguments"
	exit 10
fi

# DISCLAIMER: this is just an example control script. We do not
# 	recommend that you use this in production for controlling
#	comserv data acquisitions for stations.



##################################################################
# step 1 strip the netcode from the argument to get just station
#        because comserv only knows about stations

station=`echo $1 | sed -e 's/[A-Z][A-Z]\-//' `


echo Station= $station

##################################################################
# step 2 find the station.ini file from the /etc/stations.ini  file

ini_file=`grep \/${station}\$ $ini | sed -e 's/dir=//' `/station.ini

echo ini_file= $ini_file

##################################################################
# step 3 save a copy of the station.ini with a timestamp

# make a timestamp
ts=`date +%Y:%j:%H:%M`

# save a copy of it 
cp $ini_file $ini_file.$ts

##################################################################
# step 4 terminate station comserv and clients netmon -t SSS
netmon -t $station

# now wait for all procs to disappear!

# NOTE, this logic can fail if there are more than just comserv
# processes that have a tailing commandline argument that is
# name of the station being modified....

date
while (test 0 -ne `ps -ef | grep $station'$' | wc -l` )
do 
sleep 1
done
date

##################################################################
# step 5 comment out appropriate lines of file depending on ul
#get the existing state from the .ini file
state=`grep state= $ini_file`
echo "state string is $state"

# the following logic makes the following assumptions:
#	1. the client1 is datalog
#	2. the client2 is cwda
case $2 in
1)
	# OFF - no comserv/mserv of clients
	# set the state to state=N
	sed -e s/$state/state=N/ $ini_file > /tmp/$station.station.ini
	# turn on client2 if it is commented out(WHY? because we set state to N)
	sed -e 's/^\*client2=/client2=/' /tmp/$station.station.ini > $ini_file 
	mv /tmp/$station.station.ini $ini_file
;;
2)
	# no cwda (dlog only)
	# set the state to state=A
	sed -e s/$state/state=A/ $ini_file > /tmp/$station.station.ini
	# turn off client2 if it is not commented out
	sed -e 's/^client2=/\*client2=/' /tmp/$station.station.ini > $ini_file
;;
3)
	# both cwda & dlog only
	# set the state to state=A
	sed -e s/$state/state=A/ $ini_file > /tmp/$station.station.ini
	# turn on client2 if it is commented out
	sed -e 's/^\*client2=/client2=/' /tmp/$station.station.ini > $ini_file 
;;
esac
# if we are at usage level 1 (OFF), we are done here!

if [ $2 -eq 1 ]; then
	date
	echo "done ul = 1"
	exit 0
fi

##################################################################
# step 6 restart station comserv (if UL is not == OFF)
netmon -s $station


# save some resources  and don't poll for the first 100 secs
# because it is a waste!!! 
sleep 100

while (test `ps -ef | grep $station'$' | wc -l` -lt 2 )
do 
sleep 1
done
date
echo server and all clients started



exit 0
