#! /bin/bash -f
# set -x
# 
#  Cancel job running in SGE.
#  Input: grami file (same as Globus)
# 
#  Based on globus submission script for pbs
#
#  Based on ng submission script for pbs
#    Guenter Duckeck <gduckeck@lmu.de>
#    Juha Lento <Juha.Lento@csc.fi>


echo "----- starting cancel_sge_job -----" 1>&2

joboption_lrms=SGE
##############################################################
# These should be set automatically
##############################################################
# Where to store temporary files on gatekeeper
TMP_DIR=${TMP_DIR:-/tmp}
# Where runtime scripts can be found on computing nodes (empty if does not exist)
RUNTIME_CONFIG_DIR=${RUNTIME_CONFIG_DIR:-}
# Description of (cross-)mounted disc space on cluster
RUNTIME_LOCAL_SCRATCH_DIR=${RUNTIME_LOCAL_SCRATCH_DIR:-}
RUNTIME_FRONTEND_SEES_NODE=${RUNTIME_FRONTEND_SEES_NODE:-}
#RUNTIME_NODE_SEES_FRONTEND=${RUNTIME_NODE_SEES_FRONTEND:-}
RUNTIME_NODE_SEES_FRONTEND=${RUNTIME_NODE_SEES_FRONTEND:-yes}


arg_file=$1
##############################################################
# Source the argument file. 
##############################################################
if [ -z "$arg_file" ] ; then
   echo "Arguments file should be specified" 1>&2
   exit 1
fi
if [ ! -f $arg_file ] ; then
   echo "Missing arguments file" 1>&2
   exit 1
fi
. $arg_file


##############################################################
# Read ARC config file
##############################################################
ARC_CONF=${ARC_CONF:-/etc/arc.conf}
ARC_LOCATION=${ARC_LOCATION:-/opt/nordugrid}

if [ ! -f "$ARC_LOCATION/libexec/config_parser.sh" ] ; then
    echo "$ARC_LOCATION/libexec/config_parser.sh not found." 1>&2
    exit 1
fi

source $ARC_LOCATION/libexec/config_parser.sh

config_parse_file $ARC_CONF >&2 || exit $?
config_update_from_section "common"
config_update_from_section "infosys"
config_update_from_section "grid-manager"

##############################################################
# Set SGE specific environment.
##############################################################
if [ ! -f "${ARC_LOCATION}/libexec/configure-sge-env.sh" ] ; then
    echo "${ARC_LOCATION}/libexec/configure-sge-env.sh not found." >&2
    exit 1
fi
source ${ARC_LOCATION}/libexec/configure-sge-env.sh >&2 || exit $?

##############################################################


tmpdir=${TMP_DIR:-/tmp}

echo executing $SGE_BIN_PATH/qdel with job id $joboption_jobid 1>&2
$SGE_BIN_PATH/qdel "${joboption_jobid}"

echo "----- exiting cancel_sge_job -----" 1>&2
echo "" 1>&2
exit 0







