#!/bin/sh
# ______________________________________________________________________
#
#  Command to run a D0 framework execuable.         09NOV01, HLM
#
#  For help, use -h option.
# ______________________________________________________________________
#

SCRIPTNAME="rund0exe"                # Name of this script
SCRIPTPATH="$D0TOOLS_BIN"            # Location of this script

# ______________________________________________________________________
#
#  Internal parameters (with default values)
# ______________________________________________________________________


BUILD="OFFICIAL"                     # Build: OFFICIAL or LOCAL
COLLIDS="NONE"                       # List of specific collision ID's to process
DEFNAME="NONE"                       # SAM definition name
EXE="NONE"                           # Executable
FILELIST="NONE"                      # List of files to process
FPE="OFF"                            # Linux floating point exception handling
FWKRCPAREA="OFFICIAL"                # Framework RCP area: OFFICIAL or LOCAL
INITSCRIPT="NONE"                    # Initialization script
JOBNAME="rund0exe"                   # Default batch job name
MAXOPT=""                            # Use maxopt build
MODE="INTER"                         # Running mode: INTER or BATCH
NAME="NONE"                          # Name of specific job
NUMEVT=0                             # Number of events to process
NUMSKIP=0                            # Number of events to skip
OUTFILE="outputfile"                 # Output filename
FORCERCPOUTFILE="NO"                 # Force use of output filenames from WriteEvent rcp
PURIFY="NO"                          # Run with purify
PROFILE="NO"                         # Run with profiling
RCP="NONE"                           # Framework RCP script
RCPAREA="OFFICIAL"                   # RCP area: OFFICIAL or LOCAL
RCPPKG="NONE"                        # Framework RCP package
RUNSCRIPT="NONE"                     # Script that will be run in clued0 batch before exe.
SAMGROUP="dzero"                     # Default SAM group
SUPPRESS=""                          # Suppression mode for error logger

DEBUG="OFF"                          # Internal debug flag
QUOTE='"'                            # A double quote

OSNAME=`uname -s`                    # OS name
NODENAME=`uname -n`                  # Node name

# Defaults for batch systems

QUEUE="short"                        # Default queue
CPUT="03:00:00"                      # CPU time for Clued0 batch
MEM="500mb"                          # Memory for Clued0 batch
NODE=""                              # Select current node if -node switch is used

if [ "$NODENAME" = "d0mino" ]; then
    MEM="500000"                     # Memory for domino batch
fi

USERTE="NO"                          # switch to trigger use of d0rte
RTEPKG="NONE"                        # package to run using rte

# ______________________________________________________________________
#
#  Process the command line
# ______________________________________________________________________

#  Help mode
for var in $*; do
    if [ "$var" = "-h" ]; then    
	cat \
	$D0TOOLS_DOC/rund0exe.man* |  more
	exit
    fi
done

for var in $*; do
    # -v: verbose mode [ to debug script ]
    if [ "$var" = "-v" ]; then             
        DEBUG="ON"

    # -d: run exe in debug mode
    elif [ "$var" = "-debug" ]; then
        MODE="DEBUG"

    # -b: run exe in batch mode
    elif [ "$var" = "-batch" ]; then
        MODE="BATCH"

    # -b: run exe with purify
    elif [ "$var" = "-purify" ]; then
        PURIFY="YES"

    # -b: run exe with profiler
    elif [ "$var" = "-profile" ]; then
        PROFILE="YES"

    # -l: local build mode
    elif [ "$var" = "-localbuild" ]; then
        BUILD="LOCAL"

    # -l: local rcp mode
    elif [ "$var" = "-localrcp" ]; then
        RCPAREA="LOCAL"

    # -maxopt
    elif [ "$var" = "-maxopt" ]; then
        MAXOPT="-maxopt"

    # -l: local fwk rcp mode
    elif [ "$var" = "-localfwkrcp" ]; then
        FWKRCPAREA="LOCAL"

    # Turn FPE handling on
    elif [ "$var" = "-fpe" ]; then
        FPE="ON"

    #  Don't suppress error messages
    elif [ "$var" = "-suppress" ]; then
        SUPPRESS="-suppress"

    #  Don't suppress error messages
    elif [ "$var" = "-forcercpoutfile" ]; then
        FORCERCPOUTFILE="YES"

    # -exe=xxx: executable
    elif [ `echo $var| grep -c "\-exe="` != 0 ]; then
        EXE=`echo $var | sed 's/\-exe=//'`

    # -rcp=xxx: framework rcp
    elif [ `echo $var| grep -c "\-rcp="` != 0 ]; then
        RCP=`echo $var | sed 's/\-rcp=//'`

    # -rcppkg=xxx: framework rcp
    elif [ `echo $var| grep -c "\-rcppkg="` != 0 ]; then
        RCPPKG=`echo $var | sed 's/\-rcppkg=//'`

    # -num=xxx: process "xxx" events
    elif [ `echo $var| grep -c "\-num="` != 0 ]; then
        NUMEVT=`echo $var | sed 's/\-num=//'`

    # -skip=xxx: skip "xxx" events
    elif [ `echo $var| grep -c "\-skip="` != 0 ]; then
        NUMSKIP=`echo $var | sed 's/\-skip=//'`

    # -eventlist=xxx: process only given events
    elif [ `echo $var| grep -c "\-eventlist="` != 0 ]; then
        COLLIDS=`echo $var | sed 's/\-eventlist=//'`

    # -q=queue: use "queue"
    elif [ `echo $var| grep -c "\-q="` != 0 ]; then
        QUEUE=`echo $var | sed 's/\-q=//'`

    # -node=xxx: select a special node for running (clued0 batch only)
    elif [ `echo $var| grep -c "\-node="` != 0 ]; then
        NODE="-l nodes=`echo $var | sed 's/\-node=//'`"

    # -thisnode: select current node for running (clued0 batch only)
    elif [ `echo $var| grep -c "\-thisnode"` != 0 ]; then
        NODE="-l nodes=`echo $NODENAME`"

    # -jobname=jobname: batch jobname
    elif [ `echo $var| grep -c "\-jobname="` != 0 ]; then
        JOBNAME=`echo $var | sed 's/\-jobname=//'`

    # -o=outfile: specify outfile name
    elif [ `echo $var| grep -c "\-out="` != 0 ]; then
        OUTFILE=`echo $var | sed 's/\-out=//'`

    # -filelist=xxx: List of files on disk to process
    elif [ `echo $var| grep -c "\-filelist="` != 0 ]; then
        FILELIST=`echo $var | sed 's/\-filelist=//'`

    # -defname=xxx: SAM definition name
    elif [ `echo $var| grep -c "\-defname="` != 0 ]; then
        DEFNAME=`echo $var | sed 's/\-defname=//'`

    # -name=xxx: job name
    elif [ `echo $var| grep -c "\-name="` != 0 ]; then
        NAME=`echo $var | sed 's/\-name=//'`

    # -initscript=xxx: initialization script
    elif [ `echo $var| grep -c "\-initscript="` != 0 ]; then
        INITSCRIPT=`echo $var | sed 's/\-initscript=//'`

    # -group=xxx: SAM group
    elif [ `echo $var| grep -c "\-group="` != 0 ]; then
        SAMGROUP=`echo $var | sed 's/\-group=//'`

    # -cput=xxx: CPU time for Clued0 batch system
    elif [ `echo $var| grep -c "\-cput="` != 0 ]; then
        CPUT=`echo $var | sed 's/\-cput=//'`

    # -mem=xxx: Memory for batch system
    elif [ `echo $var| grep -c "\-mem="` != 0 ]; then
        MEM=`echo $var | sed 's/\-mem=//'`

    # -runscript=xxx: initialization script
    elif [ `echo $var| grep -c "\-runscript="` != 0 ]; then
        RUNSCRIPT=`echo $var | sed 's/\-runscript=//'`

    # -userte: select use of runtime environment
    elif [ "$var" = "-userte" ] ; then
	echo "rund0exe: d0rte operation selected. "
	echo "rund0exe: this overides localrcp and localbuild flags"
	USERTE="YES"
	BUILD="RTE"
    elif [ `echo $var| grep -c "\-rtepkg="` != 0 ]; then
	RTEPKG=`echo $var | sed 's/\-rtepkg=//'`
	echo "rund0exe :  using " $RTEPKG
    else
        echo "ERROR: Unsupported option $var."
        exit
    fi

done

# ______________________________________________________________________
#
#  Check environment
# ______________________________________________________________________

# For Clued0 batch running, we need to be on a clusterwide mounted disk,
# check this.
# If we are on /work/local, we change to /work/machine-name
if [ "$MODE" = "BATCH" -a "$OSNAME" = "Linux" ]; then
    LPATH="`pwd`"
    if [ `pwd | grep "/rooms/"` ]; then
        echo Running from a disk that is mounted on all nodes, fine...
    elif [ `pwd | grep "/home/"` ]; then
        echo Running from a disk that is mounted on all nodes, fine...
    elif [ `pwd | grep '/work/.*-clued0/'` ]; then
	echo Running from a disk that is mounted on all nodes, fine...
    elif [ `pwd | grep "/work/local"` ]; then
        echo "You are trying to run from /work/local, this is fine..."
        echo "But on the other machine this will be called /work/$NODENAME," 
        echo "changing directory..."
        cd /work/$NODENAME/`echo $LPATH | sed 's/\/work\/local\///'`
        echo "Now the directory is `pwd`."
        echo ""
    else
        echo "Trying to run from the path `pwd`."
        echo "This is not mounted on all Clued0 nodes. Please move to a partition that is,"
        echo "for example /work/local or /rooms/..."
        echo ""
        echo "You should also submit from the machine that the files are on."
        exit 1
    fi
fi

if [ "$USERTE" = "YES" ]; then
    echo "Initialising environment from the rte"
    if [ -f GlobalEnvBootstrap.sh ] ; then
	source GlobalEnvBootstrap.sh
    else
	echo "rte GlobalEnvBootstrap.sh script not found in " `pwd`
	exit
    fi
    echo "PATH = " $PATH
    echo `which coorsim`
else
#  Make sure D0RunII is setup.
    if [ ${BFCURRENT:-0} = 0 ]; then
      echo "ERROR: D0RunII not setup yet.  Please 'setup D0RunII xxx'"
	exit
    fi
fi
#  If debug mode, must have totalview.
if [ "$MODE" = "DEBUG" ]; then
    if [ ${TOTALVIEW_DIR:-0} = 0 ]; then
        echo "ERROR: totalview not setup yet."
        exit
    fi
fi

#  If purify mode, must have purify.
if [ "$PURIFY" = "YES" ]; then
    if [ ${PURIFY_DIR:-0} = 0 ]; then
        echo "ERROR: purify not setup yet."
        exit
    fi
fi


# ______________________________________________________________________
#
#  Check executable and framework rcp
# ______________________________________________________________________


if [  $EXE = "NONE"  ]; then
    echo "ERROR: No framework executable has been specified."
    echo "ERROR: Use the -exe=... option."
    exit
fi

if [  $RCP = "NONE"  ]; then
    echo "ERROR: No framework RCP has been specified."
    echo "ERROR: Use the -rcp=... option."
    exit
fi


if [ "$USERTE" = "YES" ]; then
   if [ "$RTEPKG" = "NONE" ] ; then
     echo "ERROR: No rte package was specified."
     echo "ERROR: Please use the -rtepkg option"
   fi
else
    if [  $RCPPKG = "NONE"  ]; then
	echo "ERROR: No framework RCP package has been specified."
	echo "ERROR: This package should contain the framework rcp file."
	echo "ERROR: Use the -rcppkg=... option."
	exit
    fi
fi

if [ $BUILD = "OFFICIAL" ]; then
    EXEPATH="$BFDIST/releases/$BFCURRENT/bin/$BFARCH$MAXOPT"
    RCPPATH="$BFDIST/releases/$BFCURRENT/$RCPPKG/rcp"
elif [ $BUILD = "RTE" ] ; then
    EXEPATH=${D0RTE_RTEDIR}/bin/${D0RTE_ARCH}
    RCPPATH=${D0RTE_RTEDIR}/rundata/framework_rcps/${RTEPKG}
else
    EXEPATH=`pwd`"/bin/$BFARCH$MAXOPT"
    RCPPATH=`pwd`"/$RCPPKG/rcp"
fi

if [ ! -x $EXEPATH/$EXE ]; then
    echo "ERROR: Requested executable is not available."
    echo "ERROR: $EXEPATH/$EXE"
    exit
fi

# check for local fwk RCP here
if [ ${FWKRCPAREA} = "LOCAL" ]; then
    RCPPATH=`pwd`"/$RCPPKG/rcp"
fi

if [ ! -r $RCPPATH/$RCP ]; then
    echo "ERROR: Requested framework RCP is not readable."
    echo "ERROR: $RCP"
    exit
fi

# ______________________________________________________________________
#
#  Determine input files to process:
#  Either with a list of files, or with a SAM dataset definition
# ______________________________________________________________________


#  Input is specified by list of files on disk
if [ $FILELIST != "NONE" ]; then

    if [ ! -r $FILELIST ]; then
        echo "ERROR: Can't open specified filelist."
        echo "ERROR: $FILELIST"
        exit
    else
        # we have a filelist, so if we have clued0 batch, we need to do something
        if [ "$MODE" = "BATCH" -a "$OSNAME" = "Linux" ]; then
            # so we have a Clued0 batch run. Make sure that all inputfiles
            # can be reached on the other system.
            echo Clued0 batch running, processing file list...
            INFILESTEMP="`cat $FILELIST`"
            INFILES=""
            for file in $INFILESTEMP; do
                if [ `echo $file | grep -c '/work/.*-clued0/'` != 0 ]; then
                    # cluster readable partition, so fine:
                    INFILES="$INFILES $file"
                elif [ `echo $file | grep -c "/rooms/"` != 0 ]; then
                    # cluster readable partition, so fine:
                    INFILES="$INFILES $file"
                elif [ `echo $file | grep -c "/work/local/"` != 0 ]; then
                    # this needs to be translated: /work/local/ -> /work/$NODENAME/
                    INFILES="$INFILES /work/$NODENAME/`echo $file | sed 's/\/work\/local\///'`"
                elif [ `echo $file | grep -c "/home/"` != 0 ]; then
                    # cluster readable partition, so fine:
                    INFILES="$INFILES $file"
                elif [ `echo $file | grep -c "^/"` = 0 ]; then
                    # file in local directory, needs the full path
                    INFILES="$INFILES `pwd`/$file"
                else
                    echo The file $file 
                    echo is on a disk which is not mounted on the whole Clued0 cluster,
                    echo or is reached through a path that is not valid on the whole cluster.
                    echo Move it to /work/local or /rooms/...
                    exit 1
                fi
            done
#           echo $INFILES
        else
                INFILES="`cat $FILELIST`"    #  The list of actual files to process
        fi      
        for file in $INFILES; do 
            if [ `echo $file | grep -c "SAMDEFNAME:"` != 0 ]; then
                DEFNAME=`echo $file | sed 's/\SAMDEFNAME://'`
            elif [ ! -r $file ]; then  #  Check that they exist
                echo "ERROR: Can't find requested input file."
                echo "ERROR: $file is not readable."
                echo "ERROR: Check content of $FILELIST."
                exit
            fi
        done
    fi

    DATANAME=`echo $FILELIST | sed 's/\.dat//'`

#  Or is specified by a SAM dataset definition name.
elif [ $DEFNAME != "NONE" ]; then

    DATANAME=$DEFNAME

fi

# ______________________________________________________________________
#
#  Other parameters for this job
# ______________________________________________________________________


DOTOUT="$EXE.out"          #  .out file
DOTLOG="$EXE.log"          #  .log file

# ______________________________________________________________________
#
#  framework command line options
# ______________________________________________________________________


FOPTIONS=""
FOPTIONS="$FOPTIONS -rcp framework.rcp"

if [ $DEFNAME = "NONE" ]; then
    FOPTIONS="$FOPTIONS -input_file $INFILES"      #  Not SAM
else
    FOPTIONS="$FOPTIONS -input_file SAMInput:"     #  SAM
fi

if [ $NUMEVT != 0 ]; then
    FOPTIONS="$FOPTIONS -num_events $NUMEVT"
fi

if [ $NUMSKIP != 0 ]; then
    FOPTIONS="$FOPTIONS -skip_events $NUMSKIP"
fi

if [ $FORCERCPOUTFILE = "NO" ]; then
    FOPTIONS="$FOPTIONS -output_file $OUTFILE"
fi

if [ $COLLIDS != "NONE" ]; then
    FOPTIONS="$FOPTIONS -only_collids `cat $COLLIDS`"
fi

FOPTIONS="$FOPTIONS -out $DOTOUT"
FOPTIONS="$FOPTIONS -log $DOTLOG"
FOPTIONS="$FOPTIONS -time -mem"
FOPTIONS="$FOPTIONS $SUPPRESS"

if [ $OSNAME = "Linux" ]; then
    if [ $FPE = "ON" ]; then
        FOPTIONS="$FOPTIONS -fpe"
    fi
fi

# ______________________________________________________________________
#
#  Batch mode requested
# ______________________________________________________________________


if [ "$MODE" = "BATCH" ]; then

    BOUT="$EXE.bout"
    BERR="$EXE.berr"

    if [ $OSNAME = "IRIX64" ]; then
        #  LSF batch options
        BCOMMAND="bsub"

        if [ $DEFNAME = "NONE" ]; then
            BOPTIONS="-J$JOBNAME -md0mino -o$BOUT -e$BERR -q$QUEUE -M$MEM"  # Not SAM
        else
            BOPTIONS="-J$JOBNAME -md0mino -o$BOUT -e$BERR"           # SAM: it handles queues now...
        fi
    elif [ $OSNAME = "Linux" ]; then
        # Clued0 batch system
        BCOMMAND=""      
        # We don't set a BCOMMAND here, because we need to make a script
        # to run the batch system on Clued0. We will use the normal command
        # in the script. If BCOMMAND=="", this will just add a space in
        # front of the command, and that is fine.
    else
        echo "ERROR: batch system not yet supported on this machine."
        exit
    fi

fi

# ______________________________________________________________________
#
#  Input files on local disk
# ______________________________________________________________________

if [ $DEFNAME = "NONE" ]; then

    #  Batch mode
    if [  "$MODE" = "BATCH"  ]; then
        COMMAND="$BCOMMAND $BOPTIONS ./$EXE $FOPTIONS"
        if [ "$PURIFY" = "YES" ]; then
            COMMAND="$BCOMMAND $BOPTIONS ./$EXE.pure $FOPTIONS"
        fi
        if [ "$PROFILE" = "YES" ]; then
            COMMAND="$BCOMMAND $BOPTIONS ssrun -usertime ./$EXE $FOPTIONS"
        fi
    else

        # Interactive - debug mode
        if [ "$MODE" = "DEBUG" ]; then
            COMMAND="totalview ./$EXE -a $FOPTIONS"

        # Interactive - normal mode
        else
            COMMAND="./$EXE $FOPTIONS"
            if [ "$PURIFY" = "YES" ]; then
                COMMAND="./$EXE.pure $FOPTIONS"
            fi
            if [ "$PROFILE" = "YES" ]; then
                COMMAND="ssrun -usertime ./$EXE $FOPTIONS"
            fi
        fi
    fi

# ______________________________________________________________________
#
#  Input files in SAM
# ______________________________________________________________________

else
    COMMAND="sam submit"
    COMMAND="$COMMAND --defname=$DEFNAME"
    COMMAND="$COMMAND --cpu-per-event=2m"
    COMMAND="$COMMAND --group=$SAMGROUP"

    #  Batch mode
    if [  "$MODE" = "BATCH"  ]; then
        COMMAND="$COMMAND --batch-system-flags=$QUOTE$BOPTIONS$QUOTE"
        COMMAND="$COMMAND --framework-exe=./$EXE"
        COMMAND="$COMMAND --framework-params=$QUOTE$FOPTIONS$QUOTE"
    else

        #  Interactive - debug mode
        if [ "$MODE" = "DEBUG" ]; then
            COMMAND="$COMMAND --interactive"
            COMMAND="$COMMAND --framework-exe=totalview"
            COMMAND="$COMMAND --framework-params=$QUOTE./$EXE -a $FOPTIONS$QUOTE"

        #  Interactive - normal mode
        else
            COMMAND="$COMMAND --interactive"
            COMMAND="$COMMAND --framework-exe=./$EXE"
            COMMAND="$COMMAND --framework-params=$QUOTE$FOPTIONS$QUOTE"

        fi
    fi
fi

# ______________________________________________________________________
#
#  Create local directory to run job in
# ______________________________________________________________________


if [  $NAME = "NONE" ]; then
    RUNDIR="$EXE-$BFCURRENT$MAXOPT-$OSNAME-$DATANAME"
else
    RUNDIR="$NAME"
fi

#  If directory is already there, wipe it (sorry if you wanted it...)
if [ -r $RUNDIR ]; then                    
    rm -r $RUNDIR
fi

mkdir $RUNDIR

# ______________________________________________________________________
#
#  Setup run time environment in run directory
# ______________________________________________________________________


echo "Results for this job to be placed in ./$RUNDIR"

# This next block doesn't do anything for clued0 batch running,
# but it won't hurt either.
if [ "$USERTE" = "YES" ]; then
    cd ${RUNDIR}
else
    if [ "$RCPAREA" = "OFFICIAL" ]; then
	cd $RUNDIR
	. ${SRT_PUBLIC_CONTEXT}/D0reltools/d0setwa.sh
    else
	. ${SRT_PUBLIC_CONTEXT}/D0reltools/d0setwa.sh
	cd $RUNDIR
    fi
fi

ln -sf $EXEPATH/$EXE $EXE
ln -sf $RCPPATH/$RCP framework.rcp



#  If using purify, need to generate purify exe

if [ "$PURIFY" = "YES" ]; then
    echo "Running purify on $EXE"
    if [ "$MODE" = "BATCH" ]; then
        purify -windows=no -view-file=purify.pv ./$EXE
    else
        purify ./$EXE
    fi
fi
### run init scripts
if [ "$USERTE" = "YES" ]; then
    myfile=${D0RTE_RTEDIR}/rundata/runtime-scripts/${RTEPKG}/${RTEPKG}-runtime.sh
    if [ -f "$myfile" ]; then
    echo "rund0exe : Sourcing initscript : " $myfile
	source ${myfile} ${D0RTE_RTEDIR}/${RUNDIR}
    else 
# this might not be an error condition since we don't 
# demand that a package have an init scripts
	echo "WARNING: d0rte initscript not found for package : " $RTEPKG
	echo "WARNING: looked for " $myfile
    fi
else
    if [  "$INITSCRIPT" != "NONE" ]; then
	$INITSCRIPT
    fi
fi
# ______________________________________________________________________
#
#  RUN THE COMMAND
# ______________________________________________________________________
if [ "$MODE" = "BATCH" -a "$OSNAME" = "Linux" ]; then
    # Linux batch running, this requires a script.
    # We will build it on the fly and start it
    # We will call the script runME, and it will be in $RUNDIR
    BATCHSCRIPT="runME"
    rm -f BATCHSCRIPT
    touch $BATCHSCRIPT

    cat <<EOF >> $BATCHSCRIPT
#!/bin/sh
. /etc/bashrc
WORKDIR="NONE"
if [ -r /usr/local/etc/pbssetup.sh ]; then
    . /usr/local/etc/pbssetup.sh
    echo "Found pbssetup.sh"
    echo "Workdir is now:"
    echo \$WORKDIR
else
    echo "ERROR: Could not find /usr/local/etc/pbssetup.sh"
    echo "ERROR: Maybe the partition is not mounted correctly"
    echo "ERROR: Exiting..."
    exit 1
fi
if [ \$WORKDIR = "NONE" ]; then
    echo "ERROR: WORKDIR is not defined, problem with batch system!"
    echo "ERROR: Exiting..."
    exit 1
fi 
if [ \$WORKDIR = \$HOME ]; then
    echo "ERROR: WORKDIR is defined to be your homedir"
    echo "ERROR: This means there is a problem with setting WORKDIR!"
    echo "ERROR: Exiting..."
    exit 1
fi

# Go to working directory on executing machine
cd \$WORKDIR
# Copy all the files from the submitting to the batch machine
cp -r `pwd`/* .
echo Working directory is:
echo \$PWD
setup D0RunII $BFCURRENT
if [ "$RCPAREA" = "OFFICIAL" ]; then
    . ${SRT_PUBLIC_CONTEXT}/D0reltools/d0setwa.sh
else
    cd `pwd`
    cd ..
    . ${SRT_PUBLIC_CONTEXT}/D0reltools/d0setwa.sh
    cd \$WORKDIR
fi
EOF

    if [  "$RUNSCRIPT" != "NONE" ]; then
	cat ../$RUNSCRIPT >>$BATCHSCRIPT
	echo -e "\n" >> $BATCHSCRIPT   # To make it foolproof....
    fi
    
    cat <<EOF >> $BATCHSCRIPT
cd \$WORKDIR
$COMMAND
cp -r * `pwd`
EOF

    chmod +x $BATCHSCRIPT
    if [ "$DEBUG" = "OFF" ]; then
        /usr/local/bin/cluesow -l cput=$CPUT -l mem=$MEM $NODE $BATCHSCRIPT 
    else
	echo
	echo "The script thas would have been run is:"
	cat $BATCHSCRIPT
    fi

    cd ..
    exit


else
    if [ "$DEBUG" = "OFF" ]; then
	if [  "$RUNSCRIPT" != "NONE" ]; then
	    ../$RUNSCRIPT
	fi
        eval $COMMAND
    else
        echo $COMMAND
    fi
fi
# ______________________________________________________________________
#
#  Return to original directory and finish
# ______________________________________________________________________


cd ..
exit


# ______________________________________________________________________
#
#  Modification history:
# ______________________________________________________________________
#
#  16JAN02 - JMH
#   1. Fixed localfwkrcp behaviour. Using the -localfwkrcp switch will
#      now use the official build exe with a local framework rcp file 
#      found in `pwd`/$RCPPKG/rcp/$RCP. -localbuild behaviour is unmodified
#      by these changes - it still picks up the local fwk rcp and the local exe
#   2. Changed occurences of D0RTE_INSTALL to D0RTE_RTEDIR to match changes in
#      d0rte
# ______________________________________________________________________
#  09NOV01 - HLM
#   1. Add -node, -thisnode options for clued0 (Phaf)
#
#  06NOV01 - HLM
#   1. Fix -mem option on d0mino.
#   2. Change "defineGlobalEnv.sh" to "GlobalEnvBootstrap.sh" (Ritchie)
# ______________________________________________________________________
#
#  25OCT01 - JMH
#   1. Added -userte option 
#   2. Added -rtepkg=<pkgname> option to select which exe is to be used
#   3. Added support for d0rte - only used is -userte selected.
# ______________________________________________________________________
#
#  25OCT01 - HLM
#   1. Change default clued0 memory to 500Mb (Phaf)
#   2. Support -mem option on d0mino (default = 500000) (Melanson/Verzocchi)
# ______________________________________________________________________
#
#  27SEP01 - HLM
#   1. Add -runscript option (Phaf)
#   2. Support -v option on CLUED0 (Phaf)
# ______________________________________________________________________
#
#  27SEP01 - HLM
#   1. New version from Lukas Phaf that supports clued0 batch system.
#   2. Also contains bug fixes from Lukas for sh if statements.
#   3. Bug fix for -h option.
# ______________________________________________________________________
#
#  09SEP01 - HLM
#    1. Add -eventlist to process a specific list of collision ids
# ______________________________________________________________________
#
#  06SEP01 - HLM
#    1. Add -profile to support profiling on d0mino.
# ______________________________________________________________________
#
#  26AUG01 - HLM
#    1. Add -forcercpoutfile to force using the output file list
#       as specified by the users WriteEvent.rcp.  This can be used
#       when the job needs a list of outputfiles.
# ______________________________________________________________________
#
#  10JUL01 - HLM
#    1. Add -suppress to disable error messages in log file
# ______________________________________________________________________
#
#  28JUN01 - HLM
#    1. Add -jobname=jobname option for batch jobs
#    2. Modify batch submission for SAM jobs - let SAM choose the queue
# ______________________________________________________________________
#
#  26JUN01 - HLM
#    1. Update documentation to indicate need for full paths when using
#       local files
#    2. Add -group=group option for SAM (default = dzero)
#    3. Add "-input_file SAMInput:" for SAM jobs (as suggested by Martin W. Gruenewald)
# ______________________________________________________________________
#