#!/bin/tcsh
#------------------------------------------------------------------------
#                          REVISION HISTORY                             -
#                                                                       -
#------------------------------------------------------------------------
#  MOD            PR                                                    -
# LEVEL   DATE   NUMBER  Who     Description                            -
# ----- -------- ------  ------ -----------------------------------------
#  000   06/20/97 33431  WMiller Initial version                        -
#  001   07/24/97 34781  WMiller Run PSF updater                        -
#  002   11/14/97 34811  WMiller Modify for new startup procedure       -
#  003   01/27/98 36125  WMiller Delete password file if necessary      -
#  004   04/28/98 36608  WMiller Password sent via command line         -
#  005   09/10/99 39673  MSwam   Add eval to TASK line                  -
#  006   02/15/00 40675  WMiller Echo version info                      -
#  007   07/13/00 40985  WMiller Handle logfile now                     -
#  008   11/27/00 42874  WMiller Translate time stamp for log file      -
#  009   11/16/01 44741  WMiller Add -u flag                            -
#  010   06/03/04 50605  Sontag  'which' is more universal in /usr/bin  -
#  011   06/21/04 51503  Sontag  Redirect odcl_get_* errors to log file -
#  012   07/27/04 51754  Sontag  Add OPUS_LOG_FILE env var              -
#  013   12/21/04 52373  Sontag  Add -opus_ts arg pair to TASK          -
#  014   01/14/05 52462  Sontag  Handle normal versus "stranger" nodes  -
#  015   05/26/05 53713  Sontag  Ensure hexpid defined even w/ MSG_NONE -
#  016   08/09/05 53803  MSwam   Ensure full path in PATH_FILE          -
#  017   10/20/09 63754  MSwam   Replace JAVAWILDCARD if present in TASK -
#  018   10/21/09 47751  MSwam   Add COMMAND echo for xpoll tasks       -
#  019   12/21/09 47751  MSwam   Tweak COMMAND echo to handle ^CL parm  -
#        01/03/11 65360  MSwam   Change to tcsh for Word too long problem
#------------------------------------------------------------------------
#
# Module:      opus_run_process.csh
#
#-----------------------------------------------------------------------------
# INPUT PARAMETERS:
#
#    NODE_NAME, PROCESS_NAME, PATH_FILE, TIME_STAMP, [PASSWORD]
#
#-----------------------------------------------------------------------------
# DESCRIPTION:
#
#  This shell script will create the process status file, run the requested
#  OPUS pipeline process directing its output to its log, then cleanup.
#
#---------------------------------------------------------------------------
#---------------------------------------------------------------------------
onintr abexit                            # exit on stop
#
#---------------------------------------------------------------------------
#
#                  Define the hostname and task and path file.
#
#---------------------------------------------------------------------------
# arg1: NODE_NAME
if ($1 == "0") then
   # This is a "normal" run on this normal node where OPUS is fully installed
   set junk = `uname -n | awk -F. '{print $1}'`
   set hostname = `opus_id -n $junk`
else
   # The first arg ($1) is an actual node name, so this is going to be
   # a remote run on a "stranger" node where OPUS may not be installed.
   set junk = `echo $1 | awk -F. '{print $1}'`
   set hostname = `opus_id -n $junk`
endif

# arg2: 
setenv PROCESS_NAME $2

# arg3: PATH_FILE (could be full filespec, logical filespec, root.ext, or root)
set path_argument = $3
#
# test for full filespec by seeing if first char is a /
set slashtest = `echo $path_argument | /bin/awk '{a=substr($0,1,1); print a}'`
if ("$slashtest" != "/") then
  # not a full filespec, check for logical filespec
  echo $path_argument | /bin/grep -l "OPUS_DEFINITIONS_DIR:" >& /dev/null
  if ($status != 0) then
     # prepend OPUS_DEFINITIONS_DIR so we can generate a full filespec
     set path_argument = "OPUS_DEFINITIONS_DIR:"${path_argument}
     # append ".path" if not present
     echo $path_argument | /bin/grep -l ".path" >& /dev/null
     if ($status != 0) then
        set path_argument = ${path_argument}".path"
     endif
  endif
endif
set res_path = `osfile_stretch_file $path_argument`
setenv PATH_FILE $res_path                                      # full filespec
setenv PATH_BASENAME `basename $PATH_FILE`                     # root.ext
setenv PATH_FILE_NAME $PATH_BASENAME  # leaving the old name for compatibility
setenv PATH_BASEROOT `echo $PATH_BASENAME | /bin/awk -F'.' '{print $1}'`   
                                                                 # root only

# arg4:
setenv TIME_STAMP $4
#
#---------------------------------------------------------------------------
#
#                  Create the process log file
#
#---------------------------------------------------------------------------
set ohome = `osfile_stretch_file OPUS_HOME_DIR:`
set log = "blank"
if ( $?MSG_REPORT_LEVEL)  then
  if ( $MSG_REPORT_LEVEL == "MSG_NONE" ) then
   set log = /dev/null
  endif
endif 
set hexpid = `opus_id -p $$`
if ( $log == "blank") then 
   set log = $ohome/${PROCESS_NAME}-${TIME_STAMP}-${hostname}-${hexpid}.log
   touch $log
   setenv OPUS_LOG_FILE $log
   echo "OPUS: +++ opus_run_process.csh started +++" >>&! $log
   echo "OPUS:" >>&! $log
   echo "OPUS: $OPUS_VERSION" >>&! $log
   echo "OPUS:" >>&! $log
   echo "OPUS: Input parameters:" >>&! $log
   echo "OPUS:     PROCESS_NAME = $PROCESS_NAME" >>&! $log
   echo "OPUS:     PATH_FILE    = $PATH_FILE" >>&! $log
   echo "OPUS:     PATH_BASENAME = $PATH_BASENAME" >>&! $log
   echo "OPUS:     PATH_BASEROOT = $PATH_BASEROOT" >>&! $log
   set cvts = `time_stamp $TIME_STAMP`
   echo "OPUS:     TIME_STAMP   = $TIME_STAMP ($cvts)" >>&! $log
   if ( $5 == "") then
      echo "OPUS:     PASSWORD? No" >>&! $log
   else
      echo "OPUS:     PASSWORD? Yes" >>&! $log
   endif
endif
#
#---------------------------------------------------------------------------
#
#                  Fetch the TASK line from the process resource file
#
#---------------------------------------------------------------------------
echo "OPUS:" >>&! $log
echo "OPUS: Fetching TASK line from process resource file...(odcl_get_resource_command)" >>&! $log
echo "OPUS:" >>&! $log
if ( $5 == "" ) then
   eval `/bin/sh -c "odcl_get_resource_command -p $PROCESS_NAME -u ${hexpid}:${hostname} 2>> $log"` # this setenv's $TASK
else
   eval `/bin/sh -c "odcl_get_resource_command -p $PROCESS_NAME -v $5 -u ${hexpid}:${hostname} 2>> $log"` # this setenv's $TASK
endif
setenv TASK "$TASK -opus_ts $TIME_STAMP"
set which_task = ($TASK)
echo "OPUS:" >>&! $log
echo "OPUS: Task to be run: $which_task[1] (`/usr/bin/which $which_task[1]`)" >>&! $log
#
# IF this task uses xpoll, then also print the COMMAND that will be run
echo "$TASK" | grep -i "xpoll "
if ($status == 0) then
   # grab the command, trimming any parameters
   set command=`get_resource_values -p $PATH_BASEROOT -r $PROCESS_NAME COMMAND | awk '{print $1}'`
   echo "OPUS: COMMAND= `which $command`" >>&! $log
endif
echo "OPUS:" >>&! $log
#
#---------------------------------------------------------------------------
#
#                  Create the process status file
#
#---------------------------------------------------------------------------
echo "OPUS:" >>&! $log
echo "OPUS: Creating process PSTAT...(odcl_create_psffile)" >>&! $log
echo "OPUS:" >>&! $log
odcl_create_psffile -p $PATH_BASENAME -r $PROCESS_NAME -t $TIME_STAMP -i $$ -c $hostname >>&! $log
if ( $status != "0" ) then
  echo "OPUS:" >>&! $log
  echo "OPUS: PSTAT creation error *************************" >>&! $log
  exit 1
endif
#
#---------------------------------------------------------------------------
#
#                  Run the process.
#
#---------------------------------------------------------------------------
echo "OPUS:" >>&! $log
echo "OPUS: Running the process..." >>&! $log
echo "OPUS:" >>&! $log
if ($1 == "0") then
   # this tool replaces any occurrence of JAVAWILDCARD in TASK with '*'
   eval "`replace_javawildcard`" >>&! $log
   set save_status = $status
else
   echo "OPUS: on a REMOTE node..." >>&! $log
   eval "$OPUS_REMOTE_SHELL $1 `replace_javawildcard`" >>&! $log
   set save_status = $status
endif
echo "OPUS:" >>&! $log
echo "OPUS: Process exited. Cleaning up...(odcl_cleanup)" >>&! $log
echo "OPUS:" >>&! $log
#
#---------------------------------------------------------------------------
#
#                  Cleanup PSF and OSF files if necessary.
#
#---------------------------------------------------------------------------
if ( $save_status != "0" ) then         # Process terminated abnormally.
  set option = "absent"
else
  set option = "delete"
endif
#
odcl_cleanup -p $PATH_FILE -r $$ -n $hostname -o $option >>&! $log
if ( $status != "0" ) then
  echo "OPUS:" >>&! $log
  echo "OPUS: PSTAT cleanup error *************************" >>&! $log
  exit 1
endif
#
#---------------------------------------------------------------------------
#
#                  Exit.
#
#---------------------------------------------------------------------------
exit 0
#
#---------------------------------------------------------------------------
#
#                  ERROR HANDLERS.
#
#---------------------------------------------------------------------------
abexit:
  exit 1
#
