#!/bin/csh -f
set CDIR=$LAUE_MASTER/bin
set PROGDIR=$LAUE_BIN
set SAV='Y'
#
# See if program name or command file was specified
#
if ($1 != "") then
   set pname=$1
   goto PROGCMD
endif
#
# If not, read in program (/command file) name
#
GETNAME:
echo -n 'Program (/command file) name: '
set pname=$<
if (pname == "") exit
#
# See whether program name or command file name
#
PROGCMD:
if ($pname:r != $pname) goto OPNCMD
#
# Program name processing
# -----------------------
#
if (-e "${CDIR}/${pname}.cmd" == 1) goto PREPFIL
ERRNAME:
echo '**Program not available via the laue command**'
goto GETNAME
#
# Prepare command file
#
PREPFIL:
set TYP=prog
set PROG=$pname
set CMDNAM=${pname}.com
set CMDFIL=${LAUE_SCR}/${CMDNAM}
chkcmd:
if ( -e ${CMDFIL} == 0 ) goto wrcmd
echo "**File $CMDNAM already exists**"
echo -n "Enter new name for command file or rm to remove existing file: "
set newcom=$<
if ( $newcom == "rm" ) then
   /bin/rm $CMDFIL
   goto wrcmd
endif
if ( $newcom == "" ) set newcom=$CMDNAM
set CMDNAM=$newcom
set CMDFIL=${LAUE_SCR}/${CMDNAM}
goto chkcmd
wrcmd:
echo "#" > ${CMDFIL}
${CDIR}/${pname}.cmd  $CMDFIL $PROGDIR
if ($status == 101) goto INTACT
if ($status == 99) goto CLEANUP
chmod a+x $CMDFIL
goto RUNTYP
#
# Command file option processing
#
OPNCMD:
set TYP=cmd
if (-e $pname == 0) goto ERRCMD
set CMDNAM=$pname
set PROG=$pname:r
set SAV=Y
goto RUNTYP
ERRCMD:
echo '**Command file not found**'
goto GETNAME
#
# Determine the program execution conditions required
#
RUNTYP:
echo -n 'Interactive (i), batch (b) or background (bg) [interactive]: '
set reply=$<
if ($reply =~ B*) goto BATCHBG
if ($reply =~ b*) goto BATCHBG
#
# Terminal
#
echo -n 'Print file name or terminal [terminal]: '
set OUT=$<
if ($TYP == prog) then
  echo -n 'Save command file at end of run (n/y): '
  set SAV=$<
endif
onintr ENDINT
if ($OUT != "" && $OUT != terminal) goto INT2
#
# Terminal with terminal output
#
chmod a+x $CMDFIL
$CMDFIL
goto ENDINT
#
# Terminal with log file output
#
INT2:
chkout:
if ( -e $OUT == 0 ) goto runint
echo "*File $OUT already exists*"
echo -n "Enter new name for log file or rm to remove existing file: "
set newlog=$<
if ( $newlog == "rm" ) then
   /bin/rm $OUT
   goto runint
endif
if ( $newlog == "" ) set newlog=$OUT
set OUT=$newlog
goto chkout
runint:
csh -fx < $CMDFIL >& $OUT
#
# Delete command file if required
#
ENDINT:
if ($SAV =~ y* || $SAV =~ Y*) exit
/bin/rm $CMDFIL
exit
#
# Batch job or background job submission
#
BATCHBG:
set OUT=${PROG}.log
if ( -e $OUT == 1 ) then
   echo "*Warning* File $OUT already exists"
   echo -n "Log file name: "
   set OUT=$<
   goto chklog
endif
echo -n "Print file name [${OUT}]: "
set temp=$<
if ($temp != "") set OUT=$temp
chklog:
if ( -e $OUT == 0 ) goto startj
echo "**File $OUT already exists**"
echo -n "Enter new name for log file or rm to remove existing file: "
set newlog=$<
if ( $newlog == "rm" ) then
   /bin/rm $OUT
   goto startj
endif
if ( $newlog == "" ) set newlog=$OUT
set OUT=$newlog
goto chklog
startj:
if ($reply =~ bg* || $reply =~ BG*) goto bgrd
echo -n "Time limit for job in seconds [none]: "
set timlim=$<
lbatch $CMDFIL $OUT $timlim
exit
#
# Background
#
bgrd:
nohup $CMDFIL >$OUT &
exit
#
# Interactive program with terminal input and output
#
INTACT:
onintr CLEANUP
chmod a+x $CMDFIL
$CMDFIL
/bin/rm $CMDFIL
exit
#
# Clean up after CNTRL C or CNTRL Y in program specific section
#
CLEANUP:
/bin/rm $CMDFIL
exit
