#! /bin/bash -f
# tr_cleanup
# if run as pshare or pshr####: run do_cleanup right now
# if run by user: run tr_send_cleanup
#
# 04/25/2009 CLF: optional argument "-w": run tr_status
#
trap `rm -f $tmpfile; exit 1` INT TERM
PGM=tr_cleanup
USAGE="usage: $PGM <runid> <tok> [year] [-w]"
ERR="$PGM -E- "
INF="$PGM -I- "
domain=`hostname | tr '[A-Z]', '[a-z]' | sed s/.*\.pppl.gov/pppl.gov/`
zident=`whoami`

# Check arguments
#----------------
let n=0
for arg
do
  case "$arg" in
  -w) 
     wait=1
     shift;;
  -t) 
     shift; tree=$1; shift;;
   *)
      if [ "$1" = "AUTO" ]; then
        auto=true
      else
        if [ -n "$1" ]; then
          essential[$n]=$1
          let n=n+1
        fi
      fi
      shift
      ;;
  esac
done

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

if [ -z "$auto" ]; then
# if not a TRANSP Developer: set environment QSHARE
 if [ -z "$TRANSP_DEVELOPER" ]; then
   if test -f /u/pshare/globus/.userrc; then
     . /u/pshare/globus/.userrc
   fi
 fi
fi

if [ "$domain" = "pppl.gov" ]; then
#            GRID was set in .userrc
   if [ -n "$GRID" -o "$zident" = "pshare" ]; then
      USAGE="usage: $PGM <runid> <tok> <year> [-t tree] [-w]"
   fi
fi
# 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 "$tok" -o -z "$runid" ]; then
   echo "$USAGE" 1>&2
   exit 1
fi


# PPPL
if [ "$domain" = "pppl.gov" ]; then
   if [ -f  $QSHARE/${runid}_$tok.year ]; then
      year=`cat $QSHARE/${runid}_$tok.year`
   fi
   if [ -z "$year" ]; then
      echo -n "Can't find run, please enter year: "
      read year
   fi
   o_file=$TRINF/$tok/$year/${runid}TR.OWNER
   if [ -f $o_file -o -f $ARCDIR/$tok/$year/${runid}.CDF ]; then
      echo "Do you want to delete an archived run?"
      ask_yesno \
        "Verify request to cleanup run $tok ${runid}:"
      if [ $? != 1 ]; then
         echo "$INF $runid NOT deleted"
         exit 1
      fi
   fi


# if run on PPPL cluster from priv account can delete right now
# User = pshare or pshr####
   if [ -n "$GRID" -o "$zident" = "pshare" ]; then
      if [ -z "$tree" ]; then
         tree="TRANSP_$tok"
      fi
      owner=$zident
#Completed run
      if [ -f $o_file -o -f $ARCDIR/$tok/$year/${runid}.CDF ]; then
        if [ -f $o_file ]; then
	  run_owner=`cat $o_file`
          CLEANUP="do_cleanup $runid $tok $year ALL $run_owner $tree"
        else
          CLEANUP="do_cleanup $runid $tok $year ALL $zident $tree"
        fi
# Aborted/suspended run
      else
###        CLEANUP="do_cleanup $runid $tok $year AUTO $tree"
        if [ -f $QSHARE/${runid}_${tok}.owner ]; then
          run_owner=`cat $QSHARE/${runid}_${tok}.owner` 
        fi
        CLEANUP="do_cleanup $runid $tok $year ALL $tree"
      fi
      UPDATETEMP=$CODESYSDIR/sbin/transp__updatetemp
   else
# PPPL User
# Verify owner
      owner=$zident
# Completed run
      if [ -f $o_file ]; then
	 run_owner=`cat $o_file`
# Aborted/suspended run
      elif [ -f $QSHARE/${runid}_${tok}.owner ]; then
         run_owner=`cat $QSHARE/${runid}_${tok}.owner`
      else
         echo "Can not find owner file - will not delete $runid $tok $year"
	 exit 1
      fi
      if [ "$owner" != "$run_owner" ]; then
         echo "you, $owner,  can not delete $runid $tok $year"
         echo "run is owned by $run_owner"
         exit 1
      fi
      CLEANUP="tr_send_cleanup $runid $tok $year"
    fi
# NON PPPL
else
    CLEANUP="tr_send_cleanup $runid $tok "
fi

echo "$INF $CLEANUP"
$CLEANUP
status=$?

if [ -n "$wait" ]; then
   tr_status $runid $tok
fi

# $owner is only defined if PPPL
if [ $status -eq 0 ]; then
  if [ -n "$owner" -a ! -f $QSHARE/${runid}_${tok}.owner ]; then
     echo `date` > $QSHARE/${runid}_${tok}.canceled
     if [ -n "$run_owner" ]; then
        echo $run_owner > $QSHARE/${runid}_${tok}.owner
     else
        echo $owner > $QSHARE/${runid}_${tok}.owner
     fi
  fi
  if [ -n "$UPDATETEMP" ]; then
     $UPDATETEMP "$runid" "tr_cleanup" "do_cleanup AUTO by $zident"
  fi
  exit 0
elif [ $status -eq 2 ]; then
  exit 1
else 
   echo " "
   echo "$ERR Error running $CLEANUP"
   exit 1
fi

