#!/bin/bash
# tr_status
# inquire status of a transp run

# Arguments to tr_status are project and runid.
# An optional argument is where to copy the status file.

# LOGIC
# 1) globus_job_submit transp_status
# 2) Look in ~incoming directory for <project>_<runid>.STATUS file.
# 3) If status file not there, then wait X seconds and retry

# 2003-11-19 cal
# History:
# 2004-01-20 cal: call tr_ckargs
# 
trap `rm -f $tmpfile; exit 1` INT TERM
GREP=/bin/grep
ERR="tr_status -E- "
infomsg () { $silent || echo "$@" ; }
debugmsg () { $debug && echo "$@" ; }

usage="usage: tr_status [ -s ] [ -d ] <runid> <project> [<TARGETDIRECTORY>]"

# Get Options
silent=false
debug=false

# Check Arguments
let n=0
for arg
do
  case "$arg" in
  -s) silent=true; shift;;
  -debug) debug=true; DBG="-dbg"; shift;;
  -d) debug=true; shift;;
  -*) shift ;;
   *) 
      e[$n]=$1
      let n=n+1
      shift
      ;;
  esac
done

# Check if last argument is an optional directory
if [ $n -gt 2 -a -d "${e[$n-1]}" ]; then
   let n=n-1
   tardir=${e[$n]}
   havedir=y
   let i=0
   while [ $i -lt $n ]; do  
      essential[$i]=${e[$i]}  
      let i=i+1      
   done
else
   essential=(${e[@]})
fi 

if [ $n -eq 0 -o  $n -gt 3 ]; then 
  echo "$usage" 1>&2
  exit 1
fi

# Get Target Directory and Check
if [ -z "$tardir" ]; then
  tardir="`pwd`"
fi
if [ ! -d "/$tardir" ] ; then
  tardir="`pwd`/$tardir"
  echo "usisng $tardir"
fi
if [ ! -d "$tardir" ] ; then
  echo "$ERR Given directory ($tardir) is not a directory." 1>&2
  exit 1 
elif [ ! -w "$tardir" ] ; then
  echo "$ERR Given directory ($tardir) is not writable." 1>&2
  exit 1
fi
# Call tr_ckargs
tmpfile="/tmp/${PGM}_`/bin/date +%d%H%M%S`$$"
debugmsg "tr_ckargs $tmpfile ${essential[@]}"
tr_ckargs $tmpfile ${essential[@]}
if [ $? -eq 1 ]; then
   order=`cat $tmpfile`
   runid=`echo $order |cut -f1 -d:`
   project=`echo $order |cut -f2 -d:`
   rm -f $tmpfile
else
   echo "$usage" 1>&2
   exit 1
fi
if [ -z "$runid" -o -z "$project" ]; then
   echo "$usage" 1>&2
   exit 1
fi
debugmsg "silent=$silent, debug=$debug, project=$project, runid=$runid"
debugmsg "tardir=$tardir"

# Check Globus proxy
$GLOBUS_LOCATION/bin/grid-proxy-info -exists > /dev/null 2>&1
status=$?
if [ $status -ne 0 ] ; then
  echo "$ERR Globus not initialized and/or no valid grid proxy" 2>&1
  exit 1
fi

# Find name of gridftp host
if [ -n "$TRANSPGRID_SERVER" ]; then
  srvnam="$TRANSPGRID_SERVER"
else
  srvnam="transpgrid.pppl.gov"
fi

host=${TRANSP_GRIDFTP_HOST:-$srvnam}   
rate=${TRANSP_GRIDFTP_RATE:-3}

debugmsg "host=$host, rate=$rate"

# Get names of files when ready
#srcdir=\~/transp/result/$project
srcdir=\~/incoming
status_file=$srcdir/${runid}_${project}.STATUS
status_here="$tardir/${runid}_${project}.STATUS"
#HOST="transpgrid.pppl.gov"
CMD="/u/pshare/globus/transp_status"
debugmsg "globus-job-submit $host $CMD $runid $project"
URL=`globus-job-submit $host $CMD $runid $project`
status=$?
debugmsg "status = $status"
if [ -z "$URL"  ]; then
  echo "tr_status -E- globus-job-submit failed " 2>&1
  exit 1
elif [ $status -ne 0  ]; then
  echo "tr_status -E- globus-job-submit failed " 2>&1
  exit 1  
fi

op=`echo "<FG>$CMD $URL " | tr_log.pl`
debugmsg "tr_log :- $op "  

infomsg "Looking for status on TRANSP host"
i=0
while [ 1 ]
do
  sleep 2
  debugmsg "i=$i" 
  debugmsg "globus-url-copy -nodcau $DBG gsiftp://$host/$status_file file://$status_here"
  if [ -n "$DBG" ]; then
    a=`globus-url-copy -nodcau $DBG gsiftp://$host/$status_file \
                file://$status_here`
    status=$?
  else
    a=`globus-url-copy -nodcau gsiftp://$host/$status_file \
                file://$status_here 2>&1`
    status=$?
  fi
  debugmsg "status=$status"
  debugmsg "$a"
  if [ $status -eq 0 ] ; then
    break
  else 
    status=`echo $a | grep "not a plain file"`
    if [ -z "$status" ]; then 
       if [ $i -gt 0 ]; then
         echo "$ERR GridFTP failure '$a'"
         exit 1
       else
         echo "GridFTP failure first attempt..."
       fi
    fi
    i=`expr ${i} + 1`
    infomsg "... waiting `/bin/date`" 1>&2
    sleep $rate
  fi
done
msg=`cat $status_here`
echo "$msg"

# Clean up
if [ "$havedir" != "y" ]; then 
  /bin/rm -f $status_here
fi
exit 0

