#!/bin/csh -f
#
# 	14-Dec-92 (MDM) - Changed to use the append option when executing
#			  the IDL command (>>& instead of >&)
#			- Also, remove the log file if it already exists
#	22-Jan-93 (MDM) - Added third parameter option.  If passed, then
#			  append the YYMMDD.HHMMSS to the log file name.
#	18-Mar-93 (MDM) - Changed to use "nox" option for running IDL
#	 4-Aug-93 (SLF) - use environmental $ys instead of link /ys
#			  add fast start csh reference
#	13-aug-93 (SLF) - add environ setup section. 
#	 4-sep-93 (SLF) - add ys_batch set / rename to idl_batch  / ys protect
#	30-Nov-93 (MDM) - Added definition of IDL_BATCH_LOGFIL
#	17-Feb-94 (SLF) - set ys_nox
#	28-Feb-94 (SLF) - check log file; send mail if abnormally terminated
#        1-Mar-94 (SLF) - add /notify, /monitor, /nonotify, /nomonitor
#	 		  (if monitor AND notify, send message on normal)
#       10-May-94 (SLF) - add /noftp and /nounix keywords (inhibit monitoring
#                         for certain errors - see check_log.pro 
#       26-Jul-94 (SLF) - default noftp
#       29-Aug-94 (SLF) - unset ys_fast
#       29-Sep-94 (SLF) - add IDL_BATCH_REMOVE (remove succesful jobs  and logs)
#       13-Oct-94 (SLF) - use SET as well as SETENV for IDL_BATCH_RUN 
#			  for Borne Shell children (copy MDM correction)
#       18-oct-94 (SLF) - use explicit /bin/csh command
#       11-apr-95 (SLF) - turn off auto-bin in batch mode
#
if ( (!($?ys)) && (-e /ys) ) setenv ys /ys
set arglist=()
setenv IDL_BATCH_NOFTP 1		# dont check for FTP errors
unsetenv IDL_BATCH_REMOVE		# default is not to remove
set remove="0"

foreach argn ($argv)
   switch ($argn)
      case /ftp:
            unsetenv IDL_BATCH_NOFTP         
         breaksw
      case /mon: 
      case /monitor:
         setenv IDL_BATCH_MONITOR 1
         breaksw
      case /not
      case /notify
         setenv IDL_BATCH_NOTIFY 1
         breaksw
      case /nonotify:
      case /nonot:
         unsetenv IDL_BATCH_NOTIFY
         breaksw
      case /nomon:
      case /nomonitor:
         unsetenv IDL_BATCH_MONITOR
         breaksw
      case /noftp:
         setenv IDL_BATCH_NOFTP 1		# dont check for FTP errors
         breaksw
      case /nounix:
         setenv IDL_BATCH_NOUNIX 1		# dont check for UNIX errors
         breaksw
      case /remove:
         set remove="1"				# remove jobs/logs if success success
         breaksw
      case /noremove:
         set remove="0"
         breaksw
      default:
         set arglist=($arglist $argn)	# pass on
         breaksw
   endsw
end
         
setenv IDL_BATCH_RUN $arglist[1]
set    IDL_BATCH_RUN = ($arglist[1])

if ($#arglist < 2)  then
   set OUTFIL = "~/IDL_BATCH_RUN"
else
   set OUTFIL = $arglist[2]
endif

if ($#arglist == 3) set OUTFIL = $OUTFIL`$ys/gen/script/date2fid`

setenv IDL_BATCH_LOGFIL $OUTFIL

unalias rm
rm -f $OUTFIL
source $ys/gen/setup/idl_setup >& $OUTFIL

# ----------- slf 13-aug-93 - add idl env context setup section
setenv ys_nomore 1		# disable more-like behavior
setenv ys_nox	 1		# 
setenv ys_batch  1		# batch mode flag slf, 4-sep-1993
unsetenv ys_fast		# make it slow if batch, slf, 29-aug-94
unsetenv ys_autobin		# dont run binary startup 
# -----------

/bin/csh -f $ys/gen/setup/run_idl nox $ys/gen/script/idl_batch_run >>& $OUTFIL

chmod 777 $OUTFIL

if ($?IDL_BATCH_MONITOR) then 
   set monitor_pro=$OUTFIL.pro
   set mailnorm=($?IDL_BATCH_NOTIFY)
   echo check_log, '"'$OUTFIL'"', mailnorm=$mailnorm ,remove=$remove, job='"'$IDL_BATCH_RUN'"'  >  $monitor_pro
   echo end 		                              >>  $monitor_pro
   $ys/gen/script/idl_batch $monitor_pro $monitor_pro.log /nonotify /nomonitor
   /bin/rm -f $monitor_pro $monitor_pro.log
else
   if ($?IDL_BATCH_NOTIFY) then
       echo "IDL_BATCH is done running program $1" | mail $user
   endif
endif

exit
