#!/bin/sh

if [ -f /etc/cm-base-system-config ]; then
	CONFIG=/etc/cm-base-system-config
else
	CONFIG=/usr/local/etc/cm-base-system-config
fi

PATH=`$CONFIG BIN-DIR`:$PATH

daemon=`$CONFIG TS-DAEMON`

case $daemon in
"")
	daemon=/cm/cparis/cmds/ts-daemon/bin.sun4/ts-daemon
	;;
*)
	;;
esac

cd /usr/spool/cm
LOGFILE=/usr/spool/cm/run-ts-daemon-$$
TLOGFILE=$LOGFILE
cmattach=""
nextarg=none
daemonargs=""
errors=FALSE

for i in $*
do
	case $i in
	-[nqw])
		cmattach="$cmattach $i"
		;;
	-[ipSC])
		cmattach="$cmattach $i"
		nextarg=cmattach
		;;	
	# the following several options are identical, it's just that they
	# don't all fit onto a single line...
	-acctfile|-checksum|-clientnice|-daemonnice|-logfile|-latency)
		daemonargs="$daemonargs $i"
		nextarg=daemon
		;;
	-ll-mem|-ll-proc|-ll-sched|-ll-swap|-loglevel|-maxprocs)
		daemonargs="$daemonargs $i"
		nextarg=daemon
		;;
	-minquantum|-overhead|-procsize|-suspendtime|-swapsize)
		daemonargs="$daemonargs $i"
		nextarg=daemon
		;;
	-idle|-sramsize|-quantum)
		daemonargs="$daemonargs $i"
		nextarg=daemon
		;;
	-dvwd|-logfile)
		nextarg=$i
		;;
	-[ipSC]*)
		cmattach="$cmattach $i"
		;;
	*)
		case $nextarg in
		cmattach)
			cmattach="$cmattach $i"
			;;
		daemon)
			daemonargs="$daemonargs $i"
			;;
		-dvwd)
			DVWD=$i
			export DVWD
			;;
		-logfile)
			LOGFILE=$i
			;;
		none)
			echo \"$i\" is an unrecognized argument.
			errors=TRUE
			;;
		esac
		nextarg=none
		;;
	esac
done

case $errors in
TRUE)
	cat <<!EOF!

Usage: $0 [cmattach options] [ts-daemon options] -dvwd DVWD

The cmattach options that are supported are:

  -n
  -q
  -w
  -p np
  -S seq
  -C cm
  -i interface		

The ts-daemon options that are supported are:

  -acctfile <name>
  -clientnice <number>
  -daemonnice <number>
  -idle <seconds>
  -logfile <name>
  -latency <seconds>
  -loglevel <level>
  -quantum <seconds>
  -maxprocs <count>
  -minquantum <seconds>
  -overhead <fract>
  -procsize <pages>
  -sramsize <number_Kwords>
  -swapsize <pages>

Also, -dvwd can be used to select a DVWD to swap to, or the DVWD
environment variable can be set.

Finally, the cmattach and ts-daemon options may be intermixed freely. 

!EOF!
	exit 1
	;;
esac

# initialize the data file used to determine if something is seriously
# wrong with the ts-daemon (bad arguments, bad hardware, or something)

echo > /tmp/hour-$$
started_in_this_hour=1

touch $LOGFILE
while true;
do
	cd /usr/spool/cm

	mv /tmp/hour-$$ /tmp/old-hour-$$
	date +%y-%m-%d-%H > /tmp/hour-$$
	if cmp -s /tmp/hour-$$ /tmp/old-hour-$$; then
		started_in_this_hour=`expr $started_in_this_hour + 1`
	else
		started_in_this_hour=1
	fi 
	if [ $started_in_this_hour -gt 10 ]; then
		echo "ts-daemon restarted 10 times in one hour!"
		echo "Something must be seriously wrong."
		echo "run-ts-daemon exiting"
		exit 0;
	fi

	if cmattach -n -w $cmattach $daemon -loglevel 1 $daemonargs
	then
		echo -n "ts-daemon exits due to shut-down request at "; date
		exit 0;
	else
		echo -n "ts-daemon restarting at "; date
	fi
	if [ -f gmon.out ]; then
		mv gmon.out gmon.out-`date +%y-%m-%d-%H-%M`
	fi

done > $LOGFILE 2>&1 &

if [ "$TLOGFILE" = "$LOGFILE" ]; then
	mv $LOGFILE /usr/spool/cm/run-ts-daemon-$!
fi

echo "kill $!" > /tmp/unrun-ts-daemon

chmod +x /tmp/unrun-ts-daemon

case $LOGFILE in
/tmp/run-ts-daemon*)
	mv $LOGFILE /tmp/run-ts-daemon-$!
	;;
esac

