#!/bin/sh
#  UTILITY SCRIPT NAME :  ingest_qmgr.sh
#               AUTHOR :  Luke Lin
#         DATE WRITTEN :  05/29/1998
#
#  Abstract:  This utility script manages the non-decoder production (or
#             checkout if so specified) ingest job queue on the IBM-CCS based
#             upon the following criteria:
#             1. any existing non-decoder production (or checkout if so
#                specified) ingest job running in the ingest queue will
#                continue to the end.
#             2. any new incoming non-decoder ingest job (production, test, or
#                checkout) will not run on the IBM-CCS if there is an existing
#                production (or checkout if so specified) ingest job of the
#                same type running in the queue; the new job will set itself to
#                complete under SMS and kill itself (or return with status code
#                99 if so specified)
#
#  History:
#  1999-11-30  Gilbert     - Converted to the IBM CCS.
#  2001-07-31  Keyser      - See option 3 in Abstract.
#  2004-08-13  Keyser      - Added diag. message to joblog; Added option to NOT
#                            kill the job but just exit w/ RC=99 (variable
#                            KILL-default is still YES); added option to test
#                            for jobs owned by a user other than nwprod
#                            (variable OWNER-default is still nwprod)
#  2007-05-15  Keyser      - No longer tests to see if a ruc2a dump job is in
#                            the system when this job is of the type "radsnd"
#                            (in order to then kill or stop this job) because
#                            ruc2a dump jobs no longer contain a step to
#                            ingest "radsnd" data prior to dumping data
#

set -x
KILL=${KILL:-YES}
OWNER=${OWNER:-nwprod}
env

reqid=`echo ${LOADL_STEP_ID}`
echo "reqid= $reqid "
jobname=${LOADL_JOB_NAME}
echo "jobname= $jobname"

#   Get a list of jobs on the system owned by $OWNER (defaults to nwprod)

joblist=${DATA}/jobs.list.$reqid
llq -r %jn %id -u $OWNER > $joblist
cat $joblist

#   for qid in $qjobids

export IFS=!
while read jname jid
do
       if test "$jid" = "$reqid"
       then
           echo " yes, this is the job itself -  $jid"
           echo " This job should continue to the end "
#          .. this is the only ingest job in the queue with the same type
#          .. this job will stay and run to the end
       else

#          ... Make sure the job has the same type of ingest job 
#          ... This is for safe guard purpose, some other FTP jobs
#          ... may run at the same time.

           if test "$jname" = "$jobname"
           then
#             ... Another job with this name is in the system, let that job
#                 run and ...
              if test "$KILL" = 'YES'
              then
#                ... kill the current job and set it to complete on SMS
                 msg="Another job with this name is in the system, this \
ingest job will kill itself"
                 echo $msg
                 $DATA/postmsg "$jlogfile" "$msg"
                 $SMSBIN/smscomplete
                 cd /tmpnwprd
                 rm -rf $DATA
                 llcancel $reqid
                 sleep 120
              else
#                ... exit out of this script with rc=99 but do not kill the
#                    current job 
                 exit 99
              fi
           fi 
       fi
done < $joblist


