#!/bin/bash -f
#------------------------------------------------------------------------------
# Script to run "spamd" as a cron job.
#
# D. Schlegel, Princeton, 8 Mar 2001
#------------------------------------------------------------------------------

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

cronfile=/tmp/$USER.spamd.cron

#------------------------------------------------------------------------------
# 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

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

#------------------------------------------------------------------------------
# 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 == "SPAMD" && $3 == "BEGIN") {begin = 1}; if ($2 == "SPAMD" && $3 == "END" && begin == 1) {end = 1}}END{if (begin == 1 && end == 1){print "1"} else {print "0"}}'`
if [ $qrun = 1 ] ; then
  echo "The Spectro-AMD 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 SPAMD lines.
crontab -l | awk 'BEGIN{doprint = 1}{if ($2 == "SPAMD" && $3 == "BEGIN") {doprint = 0}; if (NR > 3 && doprint == 1) {print $0}; if ($2 == "SPAMD" && $3 == "END") {doprint = 1} }' > $cronfile

echo "# SPAMD 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 "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 every 10 minutes to keep the disk automounter up." >> $cronfile
echo "*/10 * * * * spamdup 2> /dev/null" >> $cronfile
echo "# SPAMD END" >> $cronfile

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

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

\rm $cronfile

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