#!/bin/bash 
if test $1 = '-h'; then
  echo "***********************************"
  echo "*** Script to verify owned jobs ***"
  echo "***********************************"
  echo "check_PBS.bash [jobNum](job number or nothing for all jobs)"
else
  Njob=$1

  if test $# -eq 1; then
    jobs=${Njob}
  else
    server=`qstat -f | grep server | head -n 1 | awk '{print $3}' | tr '.' ' ' | awk '{print $1}'`
    jobs=`qstat -u $USER | grep ${server} | awk '{print $1}' | grep -v ':'`
  fi

  for job in ${jobs}; do
    numjob=`echo ${job} | tr '.' ' ' | awk '{print $1}'`
    echo ${numjob}"..."
    qstat -f -1 ${numjob} >& allout.tmp
    jname=`cat allout.tmp | grep Job_Name | awk '{print $3}'`
    rundir=`dirname $(cat allout.tmp | grep Output_Path | awk '{print $3}' | tr ':' ' ' | awk '{print $2}')`
    statusv=`cat allout.tmp | grep job_state | awk '{print $3}'`
    queue=`cat allout.tmp | grep queue | grep -v type | awk '{print $3}'`
    nodes=`cat allout.tmp | grep Resource_List.nodes | awk '{print $3}'`
    vmem=`cat allout.tmp | grep Resource_List.vmem | awk '{print $3}'`
    walltime=`cat allout.tmp | grep Resource_List.walltime | awk '{print $3}'`
    runtime=`cat allout.tmp | grep resources_used.walltime | awk '{print $3}'`

    echo ${numjob}"  jobName: "${jname}
    echo ${numjob}"  rundir: "${rundir}
    echo ${numjob}"  status: "${statusv}
    echo ${numjob}"  queue: "${queue}
    echo ${numjob}"  nodes: "${nodes}
    echo ${numjob}"  vmem: "${vmem}
    echo ${numjob}"  walltime: "${walltime}
    echo ${numjob}"  runtime: "${runtime}

#    exit
  
  done
  rm allout.tmp
fi
