#!/bin/sh
# ---------------------------------------------------------------------------------------
#
# /home/smc/startup  --  startup script for the SMC software
#
# ---------------------------------------------------------------------------------------

# crontab must be set-user-id-on-execute
MakeCrontabSetUserIDOnExecute() {

	CTAB=/usr/bin/crontab

	if [ -f "$CTAB" ]
	then
		if [ ! -u "$CTAB" ]
		then
			echo "Changing $CTAB to set-user-id-on-execute"
			sudo chmod +s "$CTAB"
		else
			echo "$CTAB is already set-user-id-on-execute"
		fi
	else
		echo "Missing $CTAB"
	fi

}

# the octal UART driver is not necessarily loaded yet; loads the octal UART driver
# if it was not already loaded and sets the ports so that anyone can read or write
LoadOctalUart() {

	# load the .ko file for the octal UART and modify the settings
	# so that anyone can open the port
	if [ ! -c /dev/ttyLX0 ]
	then
		sudo insmod ~/drivers/lrixr16v798.ko
	fi

}

# configures the console so that stray characters cannot cause a process to abort
StopConsoleAbort() {

	sudo stty -isig -brkint -ixoff -ixon

}

# deletes any crontab that already exists for this user
ClearCrontab() {

	crontab -r
	rm -f ~/startup.crontab

}

# starts up the software using SMCMonitor
StartupWithMonitor() {

	if [ -n "$1" ]
	then
		SMCMonitor
	else
		SMCMonitor &
	fi

}

# starts up the software by looking for directories under ~/roots that have a startup
# script and for which there is no .disabled file
StartupWithoutMonitor() {

	#find the list of files to be executed (any scripts called 'startup' in the
	# subdirectories of the 'roots' directory up to a couple of levels down)
	STARTUP_FILES=`find ~/roots -maxdepth 2 -type f -print | egrep '[/]startup$' | sort`

	# now for each file in the list, go to the directory and run the script file
	for SFILE in $STARTUP_FILES
	do
		BASE=`basename $SFILE`
		DIR=`dirname $SFILE`
		if [ ! -f "$DIR/.disabled" ]
		then
			( cd $DIR && ./$BASE $1 )
		fi
	done
}

# ---------------------------------------------------------------------------------------

# reset paths
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:~/bin

MakeCrontabSetUserIDOnExecute
LoadOctalUart
sudo ConfigurePower
sudo InitPowerSwitches

# Configure the transciever hardware protocol for each of the octal
# UART ports (3-8, 1-2 are always RS-232); the parameters may be
# any of the following:
#
#	rs232 for RS-232 signal levels
#	rs485 for RS-485 differential multi-drop signal levels
#	off for to disable the transceiver
#
# If fewer than 6 parameters are given, the remaining ports are
# configured off.
#
sudo InitOctalUartLines rs232 rs232 rs232 rs232 rs232 rs232

pps-setup

echo ""
echo =========================
echo Starting SMC applications
echo =========================
echo ""

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:~/bin
export LD_LIBRARY_PATH=/usr/local/lib:~/lib:/usr/lib/nss
export OMNIORB_CONFIG=/home/smc/omniORB.cfg

StopConsoleAbort
ClearCrontab

if [ -f ~/autostart ]
then
	StartupWithoutMonitor "$1"
else
	echo "Automatic startup disabled: to enable it, run \"touch ~/autostart\""
fi

echo ""
