#!/bin/csh -f
# script run when U Mich system is booted
# this is expected to be run from /etc/rc.d/rc.local like this:
# TELHOME=/usr/local/telescope
# $TELHOME/archive/config/boot | 2>&1 | \
#   tee $TELHOME/archive/logs/boot

echo Initializing observatory control software

# confirm TELHOME is set
if ( ! $?TELHOME ) then
    echo no TELHOME
    exit 1
endif

# normal user
set user = teloper

# load the driver modules
echo Loading hardware drivers ...
/sbin/insmod -f /usr/local/telescope/dev/pc39.o pc39_io_base=0x300

# start daemons as $user
echo -n "Starting telescope daemons ..."

# start the telescoped daemon with a fresh note in log
echo -n " telescoped"
set log = $TELHOME/archive/logs/telescoped.log
su $user -c "touch $log"
su $user -c "echo -n Starting new telescoped at " >> $log
su $user -c "date" >> $log
su $user -c "$TELHOME/bin/telescoped" >>& $log

# start CCD camera daemon
# echo -n " camerad"
# set log = $TELHOME/archive/logs/camerad.log
# su $user -c "touch $log"
# su $user -c "echo -n Starting camerad at " >> $log
# su $user -c "date" >> $log
# su $user -c "$TELHOME/bin/camerad" >>& $log

# GPS deamon -- run as root so it can set time
# echo -n " gpsd"
# set log = $TELHOME/archive/logs/gpsd.log
# su $user -c "touch $log"
# $TELHOME/bin/gpsd -fsad >>& $log

# Weather deamon
# echo -n " wxs"
# set log = $TELHOME/archive/logs/wx.log
# su $user -c "touch $log
# su $user -c "$TELHOME/bin/wxd -dls $log
# echo ""

# finished starting daemons
echo " done."

# wait for daemons to start up
echo -n "Waiting for daemons to start up..."
set n = 10
while ($n > 0 && ! -e ${TELHOME}/comm/Focus.in)
    @ n -= 1
    sleep 1
    echo -n .
end
if ($n == 0) then
    echo " no telescoped"
    exit 1
else
    echo " Ok"
endif
unset n

# find scope home and wait
# echo "Initializing telescope..."
# /bin/echo home >> $TELHOME/comm/Ctrl.in
# dd bs=1 count=1 < $TELHOME/comm/Ctrl.out >& /dev/null

# start telrun
# echo "Starting telrun.."
# set log = $TELHOME/archive/logs/telrun.log
# su $user -c "touch $log
# su $user -c "$TELHOME/bin/telrun >>& $log &

# ok
echo Finished.
