#!/bin/sh

# Globus Job Manager FBSNG interface script for reporting job status
# Author:              Anzar Afaq & Igog Mandrichenko (FNAL) 

# Last Modified:       08/09/2002 
#
# This script uses information obtained from a file passed as the
# script's argument. This file contains a list of environment variables
# which are set by way of "sourcing" the file from this script. One of
# the evironment variables set as a result of this action is then used
# to obtain the job id for which status is requested. Once the job id
# is determined the LSF bjobs command is used to obtain the current
# status of the job. This status is then returned.
#
# The Status field can contain one of the following strings:
#
# string	stands for			Globus context meaning
# --------------------------------------------------------------------
# RUN		Running				ACTIVE
# PEND		Wating to be scheduled		PENDING
# USUSP		Suspended while running 	SUSPENDED
# PSUSP		Suspended while pending 	SUSPENDED
# SSUSP		Suspended by system 		SUSPENDED
# DONE		Completed sucessfully 		DONE
# EXIT		Completed unsuccessfully	FAILED 
# UNKWN		Unknown state			FAILED 
# ZOMBI		Unknown state			FAILED 


. ${GLOBUS_LOCATION}/libexec/globus-script-initializer
 
globus_source ${libexecdir}/globus-gram-protocol-constants.sh
globus_source ${libexecdir}/globus-sh-tools.sh
globus_source ${libexecdir}/globus-gram-job-manager-tools.sh
 
kill=${GLOBUS_SH_KILL-kill}
 
arg_file=$1
# check for the argument file if it does not exist
# then return with an error immediately.
if [ ! -f $arg_file ] ; then
   echo GRAM_SCRIPT_ERROR:$GLOBUS_GRAM_PROTOCOL_ERROR_BAD_SCRIPT_ARG_FILE
   exit 1
fi
 
# use the argument file to define all the arguments.
# Of special interest here is the grami_job_id parameter.
. $arg_file
 
# if a logfile has been passed in then assume debug mode.
if [ $grami_logfile = "/dev/null" ] ; then
    DEBUG_ECHO=:
else
    DEBUG_ECHO=${GLOBUS_SH_ECHO-echo}
fi


#DEBUG_ECHO=echo
#grami_logfile=/tmp/anzar.tmp



$DEBUG_ECHO in gram_script_fbsng_poll >> $grami_logfile
$DEBUG_ECHO executing bjobs with job id $grami_job_id >> $grami_logfile

$DEBUG_ECHO "Testing if this is part of log: $grami_job_id" >> $grami_logfile

#---------------------------------------------
# Set up FBSNG environment
#---------------------------------------------

. /fnal/ups/etc/setups.sh >/tmp/setups.out 2>/tmp/setups.err
setup fbsng

fbs=fbs
grep=${GLOBUS_SH_GREP-grep}
awk=${GLOBUS_SH_AWK-awk}

q=`which fbs`
$DEBUG_ECHO "JMI which fbs: ${q}" >> $grami_logfile


# Obtain job status
status=`${fbs} ls ${grami_job_id}.GlobusMain | ${grep} $grami_job_id | ${awk} '{print $3}'`
$DEBUG_ECHO "JOB STATUS: $status" >> $grami_logfile 

#if [ "$status" = "*" ] ;then
#	gram_job_state=$GLOBUS_GRAM_CLIENT_JOB_STATE_ACTIVE;;
#else
	case $status in
		r|h|w)  gram_job_state=$GLOBUS_GRAM_PROTOCOL_JOB_STATE_PENDING;;
		d)  	gram_job_state=$GLOBUS_GRAM_PROTOCOL_JOB_STATE_DONE;;
		f|c|z)	gram_job_state=$GLOBUS_GRAM_PROTOCOL_JOB_STATE_FAILED;;
		*)  	gram_job_state=$GLOBUS_GRAM_PROTOCOL_JOB_STATE_ACTIVE;;
	esac
#fi

$DEBUG_ECHO "returning success, job state: $gram_job_state" >> $grami_logfile
echo "GRAM_SCRIPT_SUCCESS:$gram_job_state"

$DEBUG_ECHO "exiting gram_script_fbsng_poll\n\n" >> $grami_logfile

exit
