#! /bin/csh -f
#
#  tr_look (run by user)
#  make a request to generate interim output files for a transp run
#  that is currently running or stopped before completion.
#  If PPPL: ssh ... transp_look
#  Else: globus-job-submit ... transp_look
#
#  Mods
#    17Nov03   jec    Decode args before going to use_globus..
#----------------------------------------------------------------------

set PGM = "tr_look"
set USAGE = "$PGM <runid> [tok] [archive] [nomdsplus]"
set ERR = "$PGM -E- "
set INF = "$PGM -I- "

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

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

set LOOK = "/u/pshare/globus/transp_look"
#set LOOK = "/p/transpusers/xyuan2/transp_r6/codesys/sbin/transp_look"

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

set mdsplus = ""
set QCP = "/u/pshare/globus/cp2qshare"

# Check arguments
set essential = ( )
    set inum = $#argv
    @ i = 0
    while ( $i < $inum )
        @ i++
        set arg = `tolower $argv[$i]`
        if ( $arg == "archive" ) then
	    set archive = $arg
        else if ( $arg == "nomdsplus" ) then
	    set nomdsplus = $arg
	    set mdsplus = $arg
        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
    if ( $tr_u != "" ) then
        echo " "
        echo "If you want to archive an aborted run as 'transp_support' "
        echo "use 'trlook' NOT 'tr_look'"
        echo " "
        echo " trlook $tok $runid archive " 
        echo " "
        exit 1
    endif
  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
  if ( ! -f $QSHARE/$tok/$runid/${runid}_${tok}.REQUEST ) then
     goto use_globus
  endif
  ls $QSHARE/$tok/$runid/globus_gass_cache* >& /dev/null
  if ( $status == 0 ) then
     goto use_globus
  endif    
#
  ck_inactive $runid $tok
  if ( $status == 2 ) then
     echo "$INF run $tok $runid was suspended."
  else if ( $status == 1 ) then
     if ( -f $QSHARE/${runid}_${tok}.queue ) 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
     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

# Check if Archive request
    if ( $?archive ) then
# check if aborted
       if ( -f $QSHARE/${runid}_${tok}.stopped || \
            -f $QSHARE/${runid}_${tok}.suspended || \
            -f $QSHARE/${runid}_${tok}.active ) then
           echo ${PGM}: run transp_look "$runid" "$tok" "$mdsplus"
           nohup ssh tr_`whoami`@$HOST $LOOK "$runid" "$tok" "archive" "$mdsplus" &

#          set email = `grep EMAIL $QSHARE/$tok/$runid/${runid}_${tok}.REQUEST`
#          whoami > $tmpfil
#	  echo $email >> $tmpfil
#          qdate >> $tmpfil
#	  $QCP $tmpfil

          if ( $status == 0 ) then
             echo "${PGM}: request to archive $tok $runid as is ... submitted."
#            rm -f $tmpfil
             exit 0
          else
             echo "$ERR Error request to archive $tok $runid."
#             rm -f $tmpfil
             exit 1
          endif
       else
          echo  "$INF  run $tok $runid not aborted and not running."
          exit 1
       endif
# else if archive
    else
# tr_look only
#-------------
#   If aborted: run check if it already exists
      if ( -f $QSHARE/${runid}_${tok}.stopped ) then
        if ( -f $QSHARE/${runid}_${tok}.LOOK ) then
           set look_ready = `grep -i Ready $QSHARE/${runid}_${tok}.LOOK`
           set sts = $status
	   if ( $sts == 0 ) then
	     echo "$INF was already run - $look_ready"
             ask_yesno "  do you want to re-run tr_look on $runid ? "
	     if ( $status == 0 ) then
                exit
             endif
           endif
	endif
       endif
       if ( $user == "pshare" ) then
          if ( $?nomdsplus ) then
            trlook $tok $runid 
            set sts = $status
          else
            trlook $tok $runid mdsplus
            set sts = $status
            if ( $sts == 0 ) then          
	      echo "${PGM}: output was written to MDSplus TRLOOK_${tok} on transpgrid.pppl.gov"
              exit 0
            endif
          endif
          if ( $sts == 0 ) then
            exit 0
          endif
# end user = pshare
        endif
#
# queue request
#      set tmpfil = "${runid}_${tok}.look" 
#      (umask 002; whoami > $tmpfil)
#      qdate >> $tmpfil
#      if ( $?nomdsplus ) then
#         echo "nomdsplus" >> $tmpfil
#      endif
#      (umask 002; $QCP $tmpfil)
      
      echo ${PGM}: run transp_look "$runid" "$tok" "$mdsplus"
      nohup ssh tr_`whoami`@$HOST $LOOK "$runid" "$tok" "$mdsplus" & 
      if ( $status == 0 ) then
        echo "${PGM}: request to fetch interim output for $tok $runid ... is queued."

        if ( $?nomdsplus ) then
           echo "   CDF file will be in /u/tr_`whoami`/transp/look/$tok/"
        else
           echo "   output to be written to "
           echo "     MDSplus TRLOOK_${tok} on transpgrid.pppl.gov"
           echo "     CDF file will be in /u/tr_`whoami`/transp/look/$tok/"
        endif
#        rm -f $tmpfil
      else
         echo "$ERR Error fetching interim output for $tok $runid"
#        echo "$ERR Error requesting to fetch interim output for $tok $runid"
#        rm -f $tmpfil
        exit 1
      endif
      exit 0
    endif  # tr_look only
endif  # PPPL

use_globus:
# Check Globus proxy
#$GLOBUS_LOCATION/bin/grid-proxy-info -exists >& /dev/null
tr_ckproxy FIND
if ( $status != 0 ) then
  echo "$ERR Globus not initialized and/or no valid grid proxy"
  exit 1
endif
#globusrun -o -r $HOST '&(executable='$LOOK') (arguments='$runid' '$tok')'
if ( $?archive ) then
  echo globus-job-submit $HOST $LOOK "$runid" "$tok" "archive" "$mdsplus"
  set URL=`globus-job-submit $HOST $LOOK "$runid" "$tok" "archive" "$mdsplus"`
  set stat = $status
else
  echo globus-job-submit $HOST $LOOK "$runid" "$tok" "$mdsplus"
  set URL=`globus-job-submit $HOST $LOOK "$runid" "$tok" "$mdsplus"`
  set stat = $status
endif 
# record URL
  set string="\n<FG>$LOOK $URL "
  echo $string | tr_log.pl 

exit 
