#! /bin/csh -f
#
#
#  script to queue a job to...
#  halt a running TRANSP run "now" or at a given time
#
set PGM = "tr_halt"
set USAGE = "$PGM <runid> [tok] [ t <time>]"
set ERR = "$PGM -E- "
set INF = "$PGM -I- "

if ( $#argv == 0 ) then
    echo "$PGM requires runid as argument;"
    echo "         tokamak id  and/or t <time> are optional."
    echo " e.g. $USAGE"
    exit 1
endif
umask 113

  @ verified = 0
  @ thalt = 0
# Check arguments
set essential = ( )
    set inum = $#argv
    @ i = 0
    while ( $i < $inum )
        @ i++
        set arg = `tolower $argv[$i]`
        if ( $arg == "verified" ) then
            @ verified++
        else if ( $arg == "t" ) then
            @ i++
	    set thalt =  $argv[$i]
        else
            set essential = `echo $essential $argv[$i]`
        endif
    end

if ( $#essential == 0 ||  $#essential > 3 ) then 
  echo "$USAGE"
  exit 1
endif

# Call tr_ckargs
set tmpfil = "/tmp/${PGM}_`/bin/date +%H%M%S`_$$"
tr_ckargs $tmpfil $essential
if ($status == 1) then
   set order = `cat $tmpfil`
   rm -f $tmpfil
   set runid=`echo $order |cut -f1 -d:`
   set tok=`echo $order |cut -f2 -d:`
else
  echo "$USAGE"
  exit 1
endif   
if ( "$runid" == "" ) then
  echo "$USAGE"
  exit 1
endif   

# If no tok: try to figure it out   
if ( "$tok" == "" ) then
    set tok = `get_tokid $runid`
    if ( "$tok" == "no_such_run" ) then 
       echo "$ERR failed to determine tokamak"
       exit 1
    endif
endif


# set QSHARE, unless TRANP_DEVELOPER
if ( $?TRANSP_DEVELOPER == 0) then
  if ( -f /u/pshare/globus/.userrc_csh ) then
    source /u/pshare/globus/.userrc_csh
  else
    goto use_globus
  endif
endif

#
#Check where running
#
set domain = `hostname | tr '[A-Z]', '[a-z]' | sed 's/.*\.pppl.gov/pppl.gov/'`

if ( "$domain" == "pppl.gov" ) then
  ls $QSHARE/$tok/$runid/globus_gass_cache* >& /dev/null
  if ( $status == 0 && `whoami` != pshare ) then
     goto use_globus
  endif    
#
  @ stop = 0
  ck_inactive $runid $tok
  if ( $status ) then
     if ( -f $QSHARE/${runid}_${tok}.submitted ) then
       echo "$INF run $tok $runid has not yet started."
     else if ( -f $QSHARE/${runid}_${tok}.success ) then
       echo "$INF run $tok $runid has already completed."
     else
       echo "$INF could not find active run $tok $runid."
     endif
     @ stop++
  else if ( -f $QSHARE/${runid}_${tok}.stopped ) then
       echo "$INF run $tok $runid has aborted."
       @ stop++
  else if ( -f $QSHARE/${runid}_${tok}.halt_rcvd ) then
       echo "$INF halt for $tok $runid already recieved."
       @ stop++
  else if ( -f $QSHARE/${runid}_${tok}.HALT_RQST) then
       echo "$INF halt for $tok $runid already requested."
       @ stop++
  endif
  if ( $stop ) then
       echo "$ERR can not send halt requested."
     exit 1
  endif

#Check if owner
    ck_owner $runid $tok
    if ( $status == 0 ) then
      echo "$ERR You are not the owner - you have no privilege "
      exit 1
    endif


    if ( $verified ) then
      set ians = $verified
    else
      ask_yesno "verify request to halt run $tok ${runid} at ${thalt} sec:"
      set ians = $status
    endif
#
    if ( $ians ) then
      echo $thalt > $QSHARE/${runid}_${tok}.HALT_RQST
      chmod g+w $QSHARE/${runid}_${tok}.HALT_RQST
      echo "$INF request to halt run $tok $runid has been queued."
      exit 0
    else
      echo "$INF request cancelled."
      exit 1
    endif

#
# else not PPPL
else
  goto use_globus
endif

use_globus:
if ( $?TRANSPGRID_SERVER ) then
  set srvnam = "$TRANSPGRID_SERVER"
else
  set srvnam = "transpgrid.pppl.gov"
endif

set HOST = "$srvnam"    # remove when transpgrid!=transpgrid1

set HALT = "/u/pshare/globus/transp_halt"
# Check Globus proxy
$GLOBUS_LOCATION/bin/grid-proxy-info -exists >& /dev/null
if ( $status != 0 ) then
  echo "$ERR Globus not initialized and/or no valid grid proxy"
  exit 1
endif

  echo globus-job-submit $HOST $HALT "$runid" "$tok" "$thalt"
  set URL=`globus-job-submit $HOST $HALT "$runid" "$tok" "$thalt"`
  set stat = $status
# record URL
  set string="\n<FG>$HALT $URL "
  echo $string | tr_log.pl 

exit 
