#! /bin/bash
#
#@usage tr_send <runid> [<tok>]
#@arg <runid>  runid for this shot (e.g. 37065Y04)
#@arg <tok>    tokamak (e.g. TFTR)
#
#@! Main entry point for submitting an execution request to the TRANSP production system.
#@! 05/16/02 C.Ludescher
#
# submit TRANSP run to PBS
#
trap `rm -f $tmpfile; exit 1` INT TERM
PGM=tr_send
USAGE="usage: $PGM <runid> [tok trdat]"

#@! Check arguments
#@! ---------------
if [ $# -lt 1 ] ; then
  echo $USAGE 1>&2
  exit 1
fi

let i=0
while [ -n "$1" ]; do
    if [ $1 = "trdat" ]; then
       trdat="trdat"
    else
       essential[$i]=$1
       let i=i+1
    fi
    shift
done

# Call tr_ckargs
tmpfile="/tmp/${PGM}_`/bin/date +%H%M%S`_$$"
tr_ckargs $tmpfile ${essential[@]}
if [ $? -eq 1 ]; then
   order=`cat $tmpfile`
   runid=`echo $order |cut -f1 -d:`
   tok=`echo $order |cut -f2 -d:`
   year=`echo $order |cut -f3 -d:`
   rm -f $tmpfile
else
   echo "$USAGE" 1>&2
   exit 1
fi
if [ -z "$runid" ]; then
   echo "$USAGE" 1>&2
   exit 1
fi

#@! Find the tokamak
#@! ----------------
#@! tokamak supplied as argument @{$2}
if [ -z "$tok" ]; then
    #@! or find it via ls
    n=`ls ${runid}_*.REQUEST 2> /dev/null | wc -w `
    if [ $n = 1 ]; then
	fname=`ls ${runid}_*.REQUEST`
    elif [ $n = 0 ]; then
        echo " ? can not find ${runid}_<tok>.REQUEST file"
        echo " ? run tr_start first!"
        exit 1
    else
        echo " ? There are multiple ${runid}_*.REQUEST files"
        echo "   specify Tokamak as argument"
        echo "   tr_send $runid <tok>"
      exit 1
    fi
    fname=${fname%.*}
    tok=${fname/*_/}
fi

#@! Check if @{$1}_@{$2}.REQUEST exists
#@! --------------------------------
#@use file @{$1}_@{$2}.REQUEST request file generated in tr_start
#
req=${runid}_${tok}.REQUEST
if test ! -f $req; then
    echo " ? $req does not exist"
    echo " ? run tr_start first!"
    exit 1
fi

#@! Check if user has changed Namelist or Ufiles
#@!---------------------------------------------
#@use file @{$1}TR.DAT namelist file
#
if [ ${runid}TR.DAT -nt $req ]; then
	echo "you have changed the Namelist"
	echo "you must re-run tr_start"
        exit 1
fi

#@use file @{$1}.UFILES list of ufiles, created during tr_start
if [ -f ${runid}.UFNAMES ]; then
	files=`cat ${runid}.UFNAMES` 
elif [ -f ${runid}.UFILES ]; then
	files=`cat ${runid}.UFILES` 
fi
if [ -n "$files" ]; then
	for file in $files; do
	MDS=`echo $file | grep ^MDS`
	if [ -z "$MDS" ]; then
	    if [ $file -nt $req ]; then
	      echo "$file has changed"
	      mod="true"
	    fi
        fi
        done
fi
if [ -n "$mod" ]; then
	echo "you must re-run tr_start"
	exit 1
fi

# 
#@! if not a TRANSP Developer: set environment QSHARE
#@! -----------------------
#@create env .userrc  loads in the standard environment unless developer
#@use    env $TRANSP_DEVELOPER transp developers should define this to avoid using .userrc
#
if [ -z "$TRANSP_DEVELOPER" ]; then
   if test -f /u/pshare/globus/.userrc; then
     . /u/pshare/globus/.userrc
   fi
fi

#@! if run on PPPL cluster, don't need GLOBUS
#@! ----------------------
#@call tr_send_pppl.pl <runid> <tok> <globus_flag>
#
force_grid=`grep MDS_SERVER $req |grep -i _transp`
if [ -n "$force_grid" ]; then
   mv $req ${req}_orig
   cat ${req}_orig |sed 's/_TRANSPGRID/TRANSPGRID/' > $req
   rm ${req}_orig
fi
fast=`grep FAST $req`
if [ -n "$fast" ]; then
   fast="FAST"
else
   fast=`grep SLOW $req`
   if [ -n "$fast" ]; then
      fast="SLOW"
   fi
fi
domain=`hostname | tr [A-Z], [a-z] | sed s/.*\.pppl.gov/pppl.gov/`
if [ "$domain" = "pppl.gov" ] ; then
# check if have tr_ account
   getent passwd tr_`whoami` > /dev/null
   if [ $? = 0 ]; then
      echo "tr_send_pppl.pl $runid $tok NO_GLOBUS $fast"
      tr_send_pppl.pl $runid $tok NO_GLOBUS $fast
      status=$?
   else
     echo "tr_send_pppl.pl $runid $tok $trdat $fast"
     tr_send_pppl.pl $runid $tok $trdat $fast
     status=$?
   fi
else
   echo "tr_send_pppl.pl $runid $tok $trdat $fast"
   tr_send_pppl.pl $runid $tok $trdat $fast
   status=$?
fi

if [ $status -ne 0 ]; then
   echo "Error submitting $runid"
   exit 1
else
  rm -f ${runid}.UFILES
  rm -f ${runid}.UFNAMES
  echo "$runid has been submitted"
  exit 0
fi
