#! /bin/sh -f
# set -x
# 
#  Cancel job running in LSF.
#  Input: grami file (same as Globus)
# 
#  Based on globus submission script for pbs
#
#  Based on ng submission script for pbs
#
#  LSF uses bkill <jobID>
#
#  Sergio Maffioletti <sergio.maffioletti@cscs.ch>

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

# Set variables:
#   LSF_BIN_PATH
#
if [ -z ${NORDUGRID_LOCATION} ] ; then
    echo "NORDUGRID_LOCATION not set."  1>&2
    exit 1
fi
if [ ! -f "${NORDUGRID_LOCATION}/libexec/configure-lsf-env.sh" ] ; then
    echo "${NORDUGRID_LOCATION}/libexec/configure-lsf-env.sh not found." 1>&2
    exit 1
fi
source ${NORDUGRID_LOCATION}/libexec/configure-lsf-env.sh

joboption_lrms=LSF
##############################################################
# 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

echo "   whoami: "`whoami` 1>&2
LSF_BKILL='bkill'
if [ ! -z "$LSF_BIN_PATH" ] ; then
  LSF_BKILL="${LSF_BIN_PATH}/${LSF_BKILL} -s 9 "
fi

tmpdir=${TMP_DIR:-/tmp}

echo "executing bkill with job id ${joboption_jobid}" 1>&2
echo "${LSF_BKILL} ${joboption_jobid}" 1>&2
$LSF_BKILL $joboption_jobid

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