#!/bin/bash -f
#------------------------------------------------------------------------------
# Script to start the rsync part of the Spectro-Robot (as a cron job).
# Logging information is written to the file "$RAWDATA_DIR/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 spsyncrobot_start
  exit
fi

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

cronfile=/tmp/$USER.spsyncrobot.cron
SPRLOG=$RAWDATA_DIR/spsyncrobot.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 "$RAWDATA_DIR" ] ; then
  echo "RAWDATA_DIR must be set!"
  exit
fi

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

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

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

if [ -z "$CVSROOT" ] ; then
  echo "CVSROOT must be set!"
  exit
fi
# Assume some obvious things:
if [ -z "$CVS_RSH" ]; then 
    CVS_RSH=ssh
fi
if [ -z "$SPROBOT_RSH" ]; then 
    SPROBOT_RSH=ssh
fi

# Make sure someone can hear cron scream.
if [ -z "$MAILTO" ] ; then
  echo "MAILTO 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

#------------------------------------------------------------------------------
# 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 == "SPSYNCROBOT" && $3 == "BEGIN") {begin = 1}; if ($2 == "SPSYNCROBOT" && $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 SPSYNCROBOT lines.
crontab -l | awk 'BEGIN{doprint = 1}{if ($2 == "SPSYNCROBOT" && $3 == "BEGIN") {doprint = 0}; if (NR > 3 && doprint == 1) {print $0}; if ($2 == "SPSYNCROBOT" && $3 == "END") {doprint = 1} }' > $cronfile

echo "# SPSYNCROBOT 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 "RAWDATA_DIR=$RAWDATA_DIR" >> $cronfile
echo "SPECTRO_DATA=$SPECTRO_DATA" >> $cronfile
echo "SPROBOT_HOST=$SPROBOT_HOST" >> $cronfile
echo "SPROBOT_LOCALDISKS=$SPROBOT_LOCALDISKS" >> $cronfile
echo "SPROBOT_RSH=$SPROBOT_RSH" >> $cronfile
echo "CVS_RSH=$CVS_RSH" >> $cronfile
echo "CVSROOT=$CVSROOT" >> $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 10:30 am every day (local time)." >> $cronfile
echo "30 10 * * * spsyncrobot.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=\'$SPECTRO_DATA\', 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 "# SPSYNCROBOT END" >> $cronfile

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

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

\rm $cronfile

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