#!/bin/bash

# Kill any stale processes

killall -9 ccd_monitor.sh  1>/dev/null 2>/dev/null
killall -9 xpaccd          1>/dev/null 2>/dev/null
killall -9 xmccd           1>/dev/null 2>/dev/null
killall -9 xmccd1          1>/dev/null 2>/dev/null
killall -9 ds9             1>/dev/null 2>/dev/null
sleep 1

# Create new data directory with new tonight link

cd /home/john/data
YMD=$(date --date='12 hours ago' +%Y%m%d)
mkdir $YMD
rm tonight
ln -s $YMD tonight
cd $YMD
cp ../queues/*que ./

# Start the crontab to copy this directory to the server

echo 
echo "Removing the current crontab ..."
echo

crontab -r


echo
echo "Starting backup crontab for tonight's run ..."
echo


crontab /usr/local/observatory/crontabs/crontab.update_tonight &



# Power up the camera hardware that is usually off

/usr/local/bin/power_aux_on.sh        1>/dev/null 2>/dev/null
/usr/local/bin/power_camera_on.sh     1>/dev/null 2>/dev/null


sleep 20

# Start the camera xpa server 

xpaccd 1>/usr/local/observatory/logs/xpaccd.log 2>/usr/local/observatory/logs/xpaccd_error.log &

sleep 10


# Start the ccd image file monitor

ccd_monitor.sh 1>/dev/null 2>/dev/null & 


# Start the user interface

sleep 10
xmccd &
sleep 10


# Set ds9 preferences

xpaset -p ds9 preserve scale yes
xpaset -p ds9 preserve pan yes
xpaset -p ds9 preserve regions yes
xpaset -p ds9 colorbar no
xpaset -p ds9 regions centroid radius 40

# Set ds9 for N up W right telescope pointing east

xpaset -p ds9 orient x
xpaset -p ds9 rotate 180






