#!/bin/sh
#
# Submit script for GridSolve 
#
# Arguments:  GridSolve will pass one argument to the submit
# script, which is the name of the batch executable to be run.
# Also you should pass one argument to the batch executable,
# which is the request directory.  You should normally use $PWD
# since this script would be invoked by the service process
# which is already in the request directory, but the batch
# executable needs to know where to begin.
#
# Whatever back-end system you submit to, this script should
# only produce one line on stdout: a job identifier that can
# be used by the probe and cancel scripts to check status and
# kill the job, respectively.
#
# GridSolve submit scripts should exit with status 0 on success
# and non-zero on error.
#
# $Id: gs_mscc_submit,v 1.2 2007/05/11 14:44:09 yarkhan Exp $

# Export the working directory and the executable name to the environment
# so that they can be seen from the cmd shell
export GRIDSOLVE_WD=`cygpath -w $PWD`
export GRIDSOLVE_EXE=`cygpath -w $1`

# Copy the cygwin1.dll to the working directory so that the 
# compute node (which probably does not have Cygwin installed) can 
# run the executable.  Remember, this will run from $GRIDSOLVE_WD
cp ../../examples/microsoft_compute_cluster/cygwin1.dll $GRIDSOLVE_WD

echo export GRIDSOLVE_WD=${GRIDSOLVE_WD} >> job_submit_environment
echo export GRIDSOLVE_EXE=${GRIDSOLVE_EXE} >> job_submit_environment
echo "cmd /c job submit /scheduler:ms-hn /jobname:gridsolve /numprocessors:1 /workdir:%GRIDSOLVE_WD% /stdout:stdout.txt /stderr:stderr.txt %GRIDSOLVE_EXE% %GRIDSOLVE_WD% " >> job_submit_environment

# Change to root directory to avoid error messages from the CMD client in the server.log
cd \
# Use the cmd shell, with the environment variables to run the executable
cmd /c job submit /scheduler:ms-hn /jobname:gridsolve /numprocessors:1 /workdir:%GRIDSOLVE_WD% /stdout:stdout.txt /stderr:stderr.txt %GRIDSOLVE_EXE% %GRIDSOLVE_WD% > job_submit_output

# Using the cygwin shell, get the jobid from the job_submit_output file
JOBID=`cat job_submit_output | grep "ID:" | tr -d -c "0123456789" `
if [ "${JOBID}" = "" ]; then
    # error occured, return non-zero
    exit 1
else 
    # echo JOBID to stdout and return success
    echo ${JOBID}
    exit 0
fi
