#!/bin/csh -xf 
#--------------------------------------------------------------------
#
#   Program:
#
#       mscxtr.csh
#
#   Description:
#
#       Script that runs the mission schedule parser.
#       OPUS_DB and DSQUERY must set in resource file.
#--------------------------------------------------------------------
#                          REVISION HISTORY
#--------------------------------------------------------------------
#               PR
#    DATE   NUMBER  User     Description
#  -------- ------  -------  ------------------------------------
#  04/22/02 46416   Heller   Delete temp files when successful
#  01/09/02 46948   Sontag   Filter our the 'ridiculous' PATH len warning
#  07/15/03 47162   Heller   Remove detailed messaging from log file.
#  08/10/05 53889   J.Baum   Use OPUS_CLASSPATH
#  10/21/09 63755   MSwam    support Java classpath wildcarding
#  09/02/10 64432   MSwam    add DADS_CLASSPATH for MSSQL lib access
#--------------------------------------------------------------------
#
#
# define exit statuses
# 
set EXIT_SUCCESS = 0
set EXIT_PARSER_ERROR = 1
set EXIT_DB_ERROR = 2
set EXIT_SCRIPT_ERROR = 3
#
# Set default to working directory
#
cd $INPATH
if ($status != 0) then
   echo "ERROR: Failed to set directory to $INPATH"
   exit EXIT_SCRIPT_ERROR
endif
#
# Clean out Database for this time period...
#
mscxtr_prep.pl $OSF_DATASET
if ( $status != 0 ) then 
   echo "ERROR running database clean script."
   exit $EXIT_SCRIPT_ERROR
endif
echo "Finished mscxtr_prep.pl"
#
# This is just until fgs pipeline is created
set pod_name = ${INPATH}${OSF_DATASET}.pod
set temp_log_file = ${INPATH}${OSF_DATASET}.log
set temp_sql_name = ${INPATH}${OSF_DATASET}.sql
set temp_sql_outname = ${INPATH}${OSF_DATASET}.sql_log
#
# Get the MSC file's replan time start and end times.
#
setenv replan `mscxtr_get_filetime.pl $OSF_DATASET replan_time`
if ( $status != 0 ) then 
   echo "ERROR getting replan time from file_time relation."
   exit $EXIT_SCRIPT_ERROR
endif
setenv window_st `mscxtr_get_filetime.pl $OSF_DATASET window_stop`
if ( $status != 0 ) then 
   echo "ERROR getting window_stop time from file_time relation."
   exit $EXIT_SCRIPT_ERROR
endif
#
#  Parse events from the MSC file and create an SQL file that will update
#  the database
#
# java  missionSchedule/MscMain input_file replan_time  window_stop
eval java -classpath "`replace_javawildcard OPUS_CLASSPATH`":"`replace_javawildcard DADS_CLASSPATH`" FGS.MISSIONSCHEDULE.MscMain $pod_name $replan $window_st >& $temp_log_file
if ($status != 0) then
  echo "Error parsing file "
  date
  exit $EXIT_PARSER_ERROR
endif
#
# Run ISQL to update database and check status
#
isql -i $temp_sql_name  -o $temp_sql_outname -D $OPUS_DB -S $DSQUERY
#
# Check for database update error
#
setenv count `grep "Database update ERROR" $temp_sql_outname | wc -l`
if ( $count != 0 ) then 
   echo "ERROR updating dabase."
   date
   exit $EXIT_DB_ERROR 
endif
date 
\rm -f $temp_sql_name
\rm -f $temp_sql_outname
\rm -f $temp_log_file
exit $EXIT_SUCCESS
