#! /usr/local/bin/tcsh -f
# -----------------------------------------------------------------------------
#
# Name       : process.tcsh
#
# Purpose    : Make ntuple for a requested tape.
#
# Usage      : process.tcsh $tape
#
# Options    :
#
# Arguements : $1   tape
#
# Created    : ~5-JAN-1996   Adam Para
# Updated    : 24-MAY-1996   Rob Snihur For running d0user on qms msta.
# Updated    : 15-DEC-1996   John Krane Use variables.txt and allow hsm robot. 
# Updated    : 10-JAN-1997   Rob Snihur Use tcsh and fmss.
# Updated    : 28-JAN-1997   Rob Snihur /usr/local/bin/tcsh & spool_hsm.tcsh
#
# -----------------------------------------------------------------------------
set tape_no = $1
#
# read in info from variables.txt 
#
set input_stage = `grep input_stage variables.txt | grep -v '^#' | cut -f2 -d=`
set purpose_value = `grep purpose_value variables.txt | grep -v '^#' | cut -f2 -d=`
set out_dir = `grep out_dir variables.txt | grep -v '^#' | cut -f2 -d=`
set exe_setup = `grep exe_setup variables.txt | grep -v '^#' | cut -f2 -d=`
set executable = `grep executable variables.txt | grep -v '^#' | cut -f2 -d=`
set sleep_ls = `grep sleep_ls variables.txt | grep -v '^#' | cut -f2 -d=`
set file_max = `grep file_max variables.txt | grep -v '^#' | cut -f2 -d=`
set tape_robot = `grep tape_robot variables.txt | grep -v '^#' | cut -f2 -d=`
set ntup_file = `grep ntup_file variables.txt | grep -v '^#' | cut -f2 -d=`
set fmss_dir = `grep fmss_dir variables.txt | grep -v '^#' | cut -f2 -d=`

#
echo "input file is ${tape_no}"
echo "getting files from ${tape_robot}"
echo "the executable is: ${executable}"
#
# other definititons    (create work_area if doesn't already exist)
setenv FERMINT_DPOOL_DIR ${input_stage}/${tape_no}

if ( -e ${out_dir}/run${tape_no} ) then
    echo 'file already processed??   ABORT if you want'
    sleep 3
    echo 'You didnt abort...OK, continuing...'
else
    mkdir /${out_dir}/run${tape_no}
    echo "created work area ${out_dir}/run${tape_no}"
endif
if ( -e ${input_stage}/${tape_no} ) then
    echo "using ${FERMINT_DPOOL_DIR} as staging area"
else
    mkdir /${input_stage}/${tape_no}
    echo "created stage area ${input_stage}/${tape_no}"
endif
#
set work_area = /${out_dir}/run${tape_no}
#
# assign logfile names
#
if (! -e ${out_dir}/log) then
    mkdir ${out_dir}/log
    echo "created log area ${out_dir}/log"
endif
set lognt = ${out_dir}/log/${tape_no}"_nt.log"
set logls = ${work_area}/${tape_no}"_d0ls.log"
set logspool = ${work_area}/${tape_no}"_spool.log"
set logstat = ${work_area}/${tape_no}"_status.log"
#
#      clean up the input area
if (-e $FERMINT_DPOOL_DIR/stop) then
    rm $FERMINT_DPOOL_DIR/stop
endif
if (-e $logspool) then
    echo "file ${logspool} exists, delete it"
    rm $logspool
endif
if (-e $logstat) then
    echo "file ${logstat} exists, delete it"
    rm $logstat
endif
if (-e $lognt ) then
    echo "file ${lognt} exists, delete it "
    rm $lognt
endif
if (-e $logls ) then
    echo "file ${logls} exists, delete it "
    rm $logls
endif
########################################## get to work!

if ( $tape_robot == "needfile" ) then
#                                    ##### Needfile system:
#   ----    now manufacture needfile command, will append all files into the range
#           prepare the command template
    set command = "needfile -vsn ${tape_no} -tape 8mmda -stage"
#           append all requested files 
    echo $command
#           send request to stage files from the tape
    $command
    echo "submit spooler and ntuple maker for tape $tape_no in the $FERMINT_DPOOL_DIR"
    spool_tape.tcsh $tape_no $file_max  >& $logspool &
# end of needfile setup
#                                    ##### HSM system:
else
    if ( $tape_robot == "hsm" ) then
	spool_hsm.tcsh $tape_no $file_max >& $logspool &
	echo "submitting spool tape to ${tape_robot}"
# end of hsm setup
    else
	echo 'invalid tape_robot designation...  ABORT'
	exit 1
    endif
endif

cd ${work_area}

echo "defining d0user links"
echo "DT" | ${exe_setup}
echo '              ...done '
echo " "
echo " "
sleep 5

d0ls.tcsh ${sleep_ls} ${purpose_value}| \
 tee $logls | \
 ${executable}  | \
 tee $lognt | \
 d0user_back.tcsh | tee $logstat

echo `pwd` 
# echo `ls *.hst4`
sleep 30

if ( ${purpose_value} == 1 ) then   # Make ntuples.
  set nt_info = `ls -l /${work_area}/$ntup_file`
  echo $nt_info
  echo "Transfering $ntup_file to ${work_area}/${tape_no}.hbook"
  mv /${work_area}/${ntup_file} /${work_area}/${tape_no}.hbook

  # Transfer to fmss mass storage system.
  
  echo $fmss_dir
  set fmss_cmd = `fmss query $fmss_dir`
  #echo $status
  if ( $status != 4 ) then  # Object is a directory.
    echo "Fmss directory $fmss_dir does not exist."
    echo "Please create it before disk fills up!"
    echo $fmss_cmd
    echo "Status = $status"
  else                      # Object is not a directory.
    set fmss_cmd = `fmss query $fmss_dir/${tape_no}.hbook`
    if ( $status == 0 ) then  # Object is a file.
      echo "Ntuple file $fmss_dir/${tape_no}.hbook exists.  Will not overwrite."
    else                      # File does not exist.
      set fmss_cmd = `fmss cp disk:${work_area}/${tape_no}.hbook $fmss_dir/${tape_no}.hbook`
      echo $fmss_cmd
      if ( $status != 0 ) then   # Copy unsuccessful.
	echo "Problem with copying ntuple fmss directory $fmss_dir."
	echo "Status = $status"
      else                       # Copy successful.
	echo "Deleting ${work_area}/${tape_no}.hbook"
	rm ${work_area}/${tape_no}.hbook
      endif                      # Copy unsuccessful.
    endif                     # Object is a file.
  endif                      # Object is not a directory.

endif                        # Make ntuples.

echo "done"