#!/bin/bash -f
#------------------------------------------------------------------------------
# Script to start the processing part of the Spectro-Robot (as a cron job).
# Logging information is written to the file "$BOSS_SPECTRO_REDUX/sprobot.log".
#
# For ssh to work, you need the environment variables SSH_AUTH_SOCK
# and SSH_AGENT_PID set.
#
# Note that the same version of idlspec2d used to start this script is
# used to reduce all the data (passed throught the UPSVERSION keyword
# from the "sprobot.sh" script).  The only exception is if the current
# version is not a UPS version, but just NOCVS; in that case, the version
# declared current on each remote host is used.
#
# D. Schlegel, Princeton, 20 Dec 2000
#------------------------------------------------------------------------------
# If a second argument is give, such as "sprobot_start plate-mapper", or 
# "sprobot_start observer@plate-mapper.apo.nmsu.edu", then run this command 
# remotely on the machine "plate-mapper".

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

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

cronfile=/tmp/$USER.spredrobot.cron
SPRLOG=$BOSS_SPECTRO_REDUX/spredrobot.log

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

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

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

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

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

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

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

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

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

# 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

# Make sure someone can hear cron scream.
if [ -z "$MAILTO" ] ; then
  echo "MAILTO must be set!"
  exit
fi

#------------------------------------------------------------------------------
# Insist that we have set up the cvs version of speclog.  This is because
# these scripts automatically check in new files to that product.

echo

if [ "`speclog_version`" != "NOCVS: cvs" ] ; then
  echo "The cvs version of speclog must be set up!"
  exit
fi

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

echo

if [ `uname -n` = "sdsshost" ] ; then
  echo "You are not allowed to launch Spectro-Robot 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 == "SPREDROBOT" && $3 == "BEGIN") {begin = 1}; if ($2 == "SPREDROBOT" && $3 == "END" && begin == 1) {end = 1}}END{if (begin == 1 && end == 1){print "1"} else {print "0"}}'`
if [ $qrun = 1 ] ; then
  echo "The Spectro-Robot 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 SPREDROBOT lines.
crontab -l | awk 'BEGIN{doprint = 1}{if ($2 == "SPREDROBOT" && $3 == "BEGIN") {doprint = 0}; if (NR > 3 && doprint == 1) {print $0}; if ($2 == "SPREDROBOT" && $3 == "END") {doprint = 1} }' > $cronfile

echo "# SPREDROBOT BEGIN "$vers >> $cronfile
echo "MAILTO=$MAILTO" >> $cronfile
echo "PATH=$PATH" >> $cronfile
echo "SSH_AUTH_SOCK=$SSH_AUTH_SOCK" >> $cronfile
echo "SSH_AGENT_PID=$SSH_AGENT_PID" >> $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 "ASTROLOG_DIR=$ASTROLOG_DIR" >> $cronfile
echo "SPECLOG_DIR=$SPECLOG_DIR" >> $cronfile
echo "SPECFLAT_DIR=$SPECFLAT_DIR" >> $cronfile
echo "BOSS_SPECTRO_DATA=$BOSS_SPECTRO_DATA" >> $cronfile
echo "BOSS_SPECTRO_REDUX=$BOSS_SPECTRO_REDUX" >> $cronfile
echo "# This job will run at 12:59 am every day." >> $cronfile
echo "59 0 * * * \mv $SPRLOG.1dayold $SPRLOG.2dayold; \mv $SPRLOG $SPRLOG.1dayold" >> $cronfile
echo "# This job will run at 1:00 am every day." >> $cronfile
echo "0 1 * * * sprobotlist.sh >> $SPRLOG 2>&1" >> $cronfile
echo "# This job will run every 30 minutes." >> $cronfile
echo "*/30 * * * * spredrobot.sh >> $SPRLOG 2>&1" >> $cronfile
# echo "# This job will run at 45 minutes past each hour." >> $cronfile
# if [ ${vers:0:5} != NOCVS ] ; then upsversion=$vers ; fi
# echo "45 * * * * sprobot1d.sh \",topdir=\'$BOSS_SPECTRO_REDUX\', upsversion=\`$upsversion\`, nice=19\" >> $SPRLOG 2>&1" >> $cronfile
echo "# This job will run every 10 minutes to keep the disk automounter up." >> $cronfile
# echo "*/10 * * * * spamdup 2> /dev/null" >> $cronfile
echo "# SPREDROBOT END" >> $cronfile

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

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

\rm $cronfile

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