#!/bin/bash -f
#------------------------------------------------------------------------------
# Script to start Son-of-Spectro (as a cron job).
#
# D. Schlegel, Princeton, 11 Dec 2000
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
# If a second argument is give, such as "sos_start plate-mapper", or 
# "sos_start observer@plate-mapper.apo.nmsu.edu", then run this command 
# remotely on the machine "plate-mapper".

if [ $# = 1 ] ; then
  ssh $1 sos_start
  exit
fi

#------------------------------------------------------------------------------
# Test that certain environment variables are already set.

if [ -z "$IDLSPEC2D_DIR" ] ; then
  echo "IDLSPEC2D_DIR must be set!"
  exit
fi

if [ -z "$IDLUTILS_DIR" ] ; then
  echo "IDLUTILS_DIR must be set!"
  exit
fi

if [ -z "$IDL_DIR" ] ; then
  echo "IDL_DIR must be set!"
  exit
fi

if [ -z "$IDL_PATH" ] ; then
  echo "IDL_PATH must be set!"
  exit
fi

# Set defaults for RAWDATA_DIR, RAWDATA_HOST and ASTROLOG_DIR
# appropriate for sos.apo

if [ -z "$RAWDATA_DIR" ] ; then
   RAWDATA_DIR=/data/spectro
fi
echo "Setting \$RAWDATA_DIR=$RAWDATA_DIR"

if [ -z "$RAWDATA_HOST" ] ; then
   RAWDATA_HOST=sdsshost.apo.nmsu.edu
fi
echo "Setting \$RAWDATA_HOST=$RAWDATA_HOST"

if [ -z "$ASTROLOG_DIR" ] ; then
   ASTROLOG_DIR=/data/spectro/astrolog
   SPECLOG_DIR=/data/spectro/astrolog
fi
echo "Setting \$ASTROLOG_DIR=$ASTROLOG_DIR"
echo "Setting \$SPECLOG_DIR=$ASTROLOG_DIR"

if [ -z "$SPECTROLOG_DIR" ] ; then
   SPECTROLOG_DIR=/data/spectro/spectrologs
fi
echo "Setting \$SPECTROLOG_DIR=$SPECTROLOG_DIR"

# If the PATH is too long, then abort.
if [ `echo $PATH | wc -c` -gt 1020 ] ; then
  echo "PATH is too long (more than 1020 characters)"
  exit
fi

#------------------------------------------------------------------------------
# Set file names used internally in this script.

cronfile=/tmp/$USER.sos.cron
SOSLOG=$SPECTROLOG_DIR/sos.log

#------------------------------------------------------------------------------
# Don't allow this to be run from the machine "sdsshost"!

echo

if [ `uname -n` = "sdsshost" ] ; then
  echo "You are not allowed to launch Son-of-Spectro from sdsshost!"
  exit
fi

#------------------------------------------------------------------------------
# Test to see if the cron job is already loaded.  If so, then quit.

qrun=`crontab -l | awk 'BEGIN{begin = 0; end = 0}{if ($2 == "SOS" && $3 == "BEGIN") {begin = 1}; if ($2 == "SOS" && $3 == "END" && begin == 1) {end = 1}}END{if (begin == 1 && end == 1){print "1"} else {print "0"}}'`
if [ $qrun = 1 ] ; then
  echo "The Son-of-Spectro cron job is already running."
  exit
fi

#------------------------------------------------------------------------------
# Decide what the current version of idlspec2d is.

# vers=`echo $SETUP_IDLSPEC2D | awk '{print $2}'`
vers=`echo "print,idlspec2d_version()" | idl 2> /dev/null`

#------------------------------------------------------------------------------
# Construct the cron file to be loaded.  Start with the existing cron file,
# then append more to it.

# Print the cron tab w/out the first 3 lines and w/out the SOS lines.
crontab -l | awk 'BEGIN{doprint = 1}{if ($2 == "SOS" && $3 == "BEGIN") {doprint = 0}; if (NR > 3 && doprint == 1) {print $0}; if ($2 == "SOS" && $3 == "END") {doprint = 1} }' > $cronfile

echo "# SOS BEGIN "$vers >> $cronfile
echo "PATH=$PATH" >> $cronfile
echo "IDLSPEC2D_DIR=$IDLSPEC2D_DIR" >> $cronfile
echo "IDLUTILS_DIR=$IDLUTILS_DIR" >> $cronfile
echo "IDL_DIR=$IDL_DIR" >> $cronfile
echo "IDL_PATH=$IDL_PATH" >> $cronfile
echo "RAWDATA_HOST=$RAWDATA_HOST" >> $cronfile
echo "RAWDATA_DIR=$RAWDATA_DIR" >> $cronfile
echo "ASTROLOG_DIR=$ASTROLOG_DIR" >> $cronfile
echo "SPECLOG_DIR=$SPECLOG_DIR" >> $cronfile
echo "SPECTROLOG_DIR=$SPECTROLOG_DIR" >> $cronfile
echo "# This job will run every minute." >> $cronfile
echo "* * * * * sos.sh >> $SOSLOG 2>&1" >> $cronfile
echo "# This job will run at 7:45 am every day." >> $cronfile
echo "45 7 * * * aporsync_alllog.sh >> $SOSLOG 2>&1" >> $cronfile
echo "# This job will run at 8:10 am every day." >> $cronfile
echo "10 8 * * * mailhtml.sh >> $SOSLOG 2>&1" >> $cronfile
echo "# This job will run at 8:15 am every day." >> $cronfile
echo "15 8 * * * mv -f $SOSLOG $SOSLOG.old" >> $cronfile
echo "# This job will run every 10 minutes to keep the disk automounter up." >> $cronfile
echo "*/10 * * * * ls $IDLSPEC2D_DIR > /dev/null" >> $cronfile
echo "# SOS END" >> $cronfile

#------------------------------------------------------------------------------
# Load this new cron file.

crontab $cronfile
echo "Now running Son-of-Spectro version "$vers"."

\rm $cronfile

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