#!/bin/sh
####################################################################################################################
#                       ISTP LZPR SOFTWARE
#               Property of the U.S. Government
#                       NASA/GSFC/Code 560
####################################################################################################################
#
# UNIT NAME: lzpshow
#
# PURPOSE: To display various ISTP LZP system parameters and statistics
#
# CHANGE HISTORY:
# Author        Change ID  Build/Release   Date      Description of Change
# ------        ---------  ------------- ---------   ---------------------
# R. Wiechert   ISTP       R1.1          12/01/97    Original code
# R. Wiechert   ISTP       R1.2A         05/06/98    Use find for maintenance,
#                                                     report processing mode
# R. Wiechert   ISTP       R1.3          06/16/98    Drop oracle from maintenance,
#                                                     hardcode days to age values.
#
# NOTES:
# See the help section embedded in the code for more details.
#
####################################################################################################################
mission_ops_list="wind:windftp:WIND polar:polarftp:POLAR geotail:geoftp:GEOTAIL"
mission_dev_list="winddev:windftp:WINDDEV polardev:polarftp:POLARDEV geodev:geoftp:GEODEV"
mission_all_list="$mission_ops_list $mission_dev_list"
etc_passwd=/etc/passwd
sqlprompt="IGNORESQLPROMPT "
tail_script="tail -10"
ftp_rename_script="/opt/local/bin/lzp_rename -d -n" # produces standard filenames
dps_time_script="lzptime" # converts rawtohex sctime to human format
tmp_file=/usr/tmp/lzpshow$$
trap 'rm -f $tmp_file.[0-9]; exit 0' 1 2 3 15
underline="--------------------------------------------------------------------------------------------------------"
FTP_REPLAY_SUFFIX=${FTP_REPLAY_SUFFIX:="blk"}
ftp_suffix_active="_replay"
ftp_suffix_pending=$FTP_REPLAY_SUFFIX
ftp_suffix_hold="_hold"
ftp_suffix_error="_replay_error"
ftp_suffix_done="_replay_done"
ftp_suffix_list="$ftp_suffix_active $ftp_suffix_pending $ftp_suffix_hold $ftp_suffix_error $ftp_suffix_done"
dps_session_file="[0-9]*"
dps_cas_file="1/CAS*"
dps_log_file="*.*_log"
dps_amf_file="*.amf*"
dps_product_file="product[0-9]*"
dps_dan_file="DAN[0-9]* dan[0-9]*"
bannerline="########################################################################################################"

# determine default options
mission_list=""
show_ftp=false
show_replay=true
show_session=false
show_product=false
show_logs=false
show_archive=false
show_maintenance=true
show_times=false
show_ground=false
show_specific=false
show_help=false
show_alarm=true

# determine runtime options
while getopts hodatgfrsplbmqQn: option
do
   case $option in
      o) mission_list="$mission_ops_list" ;;
      d) mission_list="$mission_dev_list" ;;
      a) mission_list="$mission_all_list" ;;
      t) show_times=true ;;
      g) show_ground=true; show_times=true ;;
      f) show_specific=true; show_ftp=confirmed ;;
      r) show_specific=true; show_replay=confirmed ;;
      s) show_specific=true; show_session=confirmed ;;
      p) show_specific=true; show_product=confirmed ;;
      l) show_specific=true; show_logs=confirmed ;;
      b) show_specific=true; show_archive=confirmed ;;
      m) show_specific=true; show_maintenance=confirmed ;;
      n) tail_script="tail -$OPTARG" ;;
      q) show_alarm=false ;;
      Q) show_alarm=false ;;
      *) show_help=true ;;
   esac
done

# determine mission if none specified
shift `expr $OPTIND - 1`
if [ ! "$mission_list" -a $# = 0 ]
then
   set -- `/usr/ucb/whoami`
fi

# determine parameters for each mission
while [ $# -gt 0 ]
do
   option=`expr "$1" : '\(-\).*'`
   if [ ! "$option" ]
   then
      for mission in $mission_all_list
      do
         mission_requested=`echo $1 |tr '[A-Z]' '[a-z]'`
         mission_user=`expr "$mission" : '\(.*\):.*:.*'`
         if [ "$mission_user" = "$mission_requested" ]
         then
            mission_list="$mission_list $mission"
            break
         fi
      done
   fi
   shift
done

# force help if no missions found
if [ ! "$mission_list" ]
then
   show_help=true
fi

# determine which reports to generate
if [ $show_specific = true ]
then
   tail_script="cat"
   if [ $show_ftp != confirmed ]
   then
      show_ftp=false
   fi
   if [ $show_replay != confirmed ]
   then
      show_replay=false
   fi
   if [ $show_session != confirmed ]
   then
      show_session=false
   fi
   if [ $show_product != confirmed ]
   then
      show_product=false
   fi
   if [ $show_logs != confirmed ]
   then
      show_logs=false
   fi
   if [ $show_archive != confirmed ]
   then
      show_archive=false
   fi
   if [ $show_maintenance != confirmed ]
   then
      show_maintenance=false
   fi
fi

# determine whether to display help
if [ $show_help != false ]
then
myname=`expr "$0" : '.*/\(.*\)'`
more << END_USAGE_TEXT
 
USAGE:     $myname [-hodatgfrsplbmq] [-n #] [missions]
 
PURPOSE:   Reports the current state of the ISTP Level Zero Processor.
 
OPTIONS:   -h      display help screen
           -o      display operational missions
           -d      display development missions
           -a      display all missions
           -t      display spacecraft times
           -g      display ground UTC times
           -f      generate FTP Staging Area Report
           -r      generate FTP Replay Area Report
           -s      generate Session Capture Area Report
           -p      generate Product Staging Area Report
           -l      generate FTP Replay Log Report
           -b      generate Block Archive Area Report
           -m      generate Routine Maintenance Report
           -n #    truncate reports, keeping last # lines
           -q      disable maintenance alarms
 
SYNOPSIS:  The set of missions to be displayed is the union of missions
           specified using the -oda options together with those listed
           explicitly on the command line.  If no missions are specified,
           then the mission is defaulted to the current user id.

           The -tg options are used to display spacecraft and ground
           times respectively whenever possible.  This is particularly
           useful when generating the Session Capture Area Reports and
           the Product Staging Area Reports.  The -tg options require
           access to the mission database in order to function properly
           and may report multiple NULL values when mission resources
           are not available.  In the case of the FTP Replay Log Reports,
           the -tg options are used to display the ground time difference
           between the file receipt time and the replay completion time.

           The -frsplb options are used to limit the number of reports
           generated for each mission such that only the requested
           reports will be generated.  The -frsplb options request the
           the FTP Staging Area Report, the FTP Replay Area Report,
           the Session Capture Area Report, the Product Staging Area
           Report, the FTP Replay Log Report, and the Block Archive
           Area Report respectively.  Neither the FTP Replay Log Report
           nor the Block Archive Area Report is generated by default.

           The -m option is used to generate the Routine Maintenance
           Report in order to alert the operator to conditions which
           may require manual intervention.  Typically this report will
           simply suggest expired files for deletion, if any exist.
 
           The -n option is used to truncate certain reports such that
           only the last few lines of each report are displayed.  Only
           the FTP Staging Area Report, the Session Capture Area Report,
           the Product Staging Area Report, the Block Archive Area Report,
           and the Routine Maintenance Report support the -n option.

           The -q option is used to quiet or disable maintenance alarms.
           This will not fix the source of the problem;  it simply stops
           the alarms from being generated so that work can be done.
 
NOTES:     The following example shows how to generate a complete
           mission status report for the wind mission:  $myname wind
 
           The following example shows how to generate the FTP Staging
           Area Report for all operational missions, truncating the
           report to show only the last 10 lines:  $myname -ofn 10

           Some script functions require access to remote/shared files
           and database instances.  It may not be possible to access
           all resources for a given mission from the current machine;
           therefore it may be necessary to execute this script on the
           particular host which has access to those mission resources.
 
END_USAGE_TEXT
kill $$
sleep 60
fi

for mission in $mission_list
do
   mission_user=`expr "$mission" : '\(.*\):.*:.*'`
   mission_ftp=` expr "$mission" : '.*:\(.*\):.*'`
   mission_dbms=`expr "$mission" : '.*:.*:\(.*\)'`

   account=`grep "^$mission_user:" $etc_passwd`
   mission_user_home=`expr "$account" : '.*:.*:.*:.*:.*:\(.*\):.*'`
   account=`grep "^$mission_ftp:" $etc_passwd`
   mission_ftp_home=`expr "$account" : '.*:.*:.*:.*:.*:\(.*\):.*'`

   ftp_staging_area="$mission_ftp_home"
   ftp_replay_area="$mission_user_home/blks"
   dps_session_area="$mission_user_home/data1"
   dps_product_area="$mission_user_home/data3"
   blk_archive_area="$mission_user_home/archive"

   dps_database_user=""
   if [ "$ORACLE_SID" = "$mission_dbms" ]
   then
      dps_database_user=`decrypt.sql $PICSHOME`
   fi

   capacity=`df -k $mission_user_home 2>/dev/null | awk '{ print $5 }' | sed s/"%"// | tail -1`
   capacity=${capacity:=100}
   automode=`crontab -l $mission_user 2>/dev/null | tail -1`
   if [ "$automode" ]
   then
      automode="automatic processing mode"
   else
      automode=`/usr/ucb/whoami`
      if [ "$automode" = "$mission_user" ]
      then
         automode="manual processing mode"
      else
         automode="unknown processing mode"
      fi
   fi

   echo ""
   echo ""
   echo $bannerline
   echo ""
   echo "     ISTP LZP MISSION STATUS REPORT"
   echo "     $mission_user_home is $capacity% full"
   echo ""
   echo "     `uname -a`"
   echo "     `id` reporting on $mission_user mission"
   echo "     `date` $automode"
   echo ""
   echo $bannerline
   echo ""

   if [ "$capacity" -gt 79 ]
   then
      if [ "$capacity" -gt 89 ]
      then
         while [ "$capacity" -gt 94 -a "$show_alarm" = "true" ]
         do
            clear
            echo "************************************************************"
            echo " - DANGER - WARNING - DANGER - WARNING - DANGER - WARNING -"
            echo "************************************************************"
            echo ""
            echo "  MAINTENANCE EMERGENCY"
            echo "  $mission_user_home reached $capacity% full capacity"
            echo "  Manually delete files now or risk mission failure"
            echo ""
            echo "************************************************************"
            echo " - DANGER - WARNING - DANGER - WARNING - DANGER - WARNING -"
            echo "************************************************************"
            sleep 5
            clear
            echo "************************************************************"
            echo " - DANGER - WARNING - DANGER - WARNING - DANGER - WARNING -"
            echo "************************************************************"
            echo ""
            echo "  DPS hash tables may be corrupted at 100% full capacity"
            echo "  With DPS software down, use hashinit to repair the disk"
            echo "  Remember to preprocess existing sessions again manually"
            echo ""
            echo "************************************************************"
            echo " - DANGER - WARNING - DANGER - WARNING - DANGER - WARNING -"
            echo "************************************************************"
            sleep 5
            clear
            echo "************************************************************"
            echo " - DANGER - WARNING - DANGER - WARNING - DANGER - WARNING -"
            echo "************************************************************"
            echo ""
            echo ""
            echo "      THIS ALARM CAN BE DISABLED USING THE -Q OPTION"
            echo ""
            echo ""
            echo "************************************************************"
            echo " - DANGER - WARNING - DANGER - WARNING - DANGER - WARNING -"
            echo "************************************************************"
            sleep 5
         done
         echo "MAINTENANCE REQUIRED"
         echo "$mission_user_home reached $capacity% full capacity"
         echo "Manually delete files now or risk mission failure"
         if [ "$show_alarm" = "true" ]
         then
            printf "\nPress enter to acknowledge and continue... "
            read answer
         fi
      else
         echo "MAINTENANCE ADVISORY"
         echo "$mission_user_home reached $capacity% full capacity"
         echo "Please monitor disk status and delete expired files"
      fi
   fi

   if [ $show_maintenance != false ]
   then
      echo ""
      echo "ROUTINE MAINTENANCE REPORT"
      echo "Files expired in various locations"
      echo ""
      echo "GMT Accessed    State        Bytes  Absolute File Name"
      echo $underline

      maintenance_list="" # append {<days to age>, <path name>} pairs
      maintenance_list="$maintenance_list 90 $mission_user_home/opslog"
      maintenance_list="$maintenance_list 14 $mission_user_home/amf"
      maintenance_list="$maintenance_list 14 $mission_user_home/msg"
      maintenance_list="$maintenance_list 3 $mission_user_home/hold"
      maintenance_list="$maintenance_list 3 $mission_user_home/instpkt"
      maintenance_list="$maintenance_list 3 $mission_user_home/stat"
      maintenance_list="$maintenance_list 3 $ftp_replay_area"
      maintenance_list="$maintenance_list 1 $mission_user_home/ttcd"
      maintenance_list="$maintenance_list 1 $mission_user_home/trend"
      maintenance_list="$maintenance_list 1 $mission_user_home/archive"
      maintenance_list="$maintenance_list 1 $ftp_staging_area"
      maintenance_list="$maintenance_list 1 $mission_user_home/config/Assembly_Decom_Startup.*"
      maintenance_list="$maintenance_list 1 $mission_user_home/config/Frame_Sync_Startup.*"

      shift $#
      set -- $maintenance_list
      while [ $# -gt 1 ]
      do
         maintenance_list="$*"
         find $2 -mtime "+"$1 -ls 2>/dev/null | grep -v "/\.\.\." \
         | awk '{ printf "%3s %2s %5s %8s %12s  %s\n", $8, $9, $10, "EXPIRED", $7, $11 }'
         shift 2
      done
   fi

   if [ $show_ftp != false ]
   then
      echo ""
      echo "FTP STAGING AREA REPORT"
      if [ -x $ftp_staging_area ]
      then

         cd $ftp_staging_area
         echo "Files pending in `pwd`"
         echo ""
         echo "GMT Received    State  Block Bytes  Raw File Name        Standard File Name"
         echo $underline

         shift $#
         set -- `ls -ltrd * 2>/dev/null | $tail_script \
         | awk '{ printf "%s %s %s %s %s\n", $5, $6, $7, $8, $9 }'`
         raw_file_list=`ls -tr 2>/dev/null | $tail_script`

         if [ "$raw_file_list" ]
         then
            renamed_file_list=`$ftp_rename_script $raw_file_list`
            search_pattern=""
            for renamed_file in $renamed_file_list
            do
               if [ "$search_pattern" ]
               then
                  if [ "$renamed_file" = "$search_pattern" ]
                  then
                     search_pattern=""
                  fi
                  continue
               fi

               case "$renamed_file" in
                  UNK)  renamed_file=unknown; search_pattern=skipping ;;
                  file) continue ;;
               esac

               if [ $# -gt 4 ]
               then
                  echo "$1 $2 $3 $4 $5 INPUT $renamed_file" \
                  | awk '{ printf "%3s %2s %5s %8s %12s %20s  %s\n", $2, $3, $4, $6, $1, $5, $7 }'
                  shift 5
               fi
            done
         fi

      else
         echo "ERROR:  Unable to access $ftp_staging_area"
      fi
   fi

   if [ $show_replay != false ]
   then
      echo ""
      echo "FTP REPLAY AREA REPORT"
      if [ -x $ftp_replay_area ]
      then

         cd $ftp_replay_area
         echo "Files pending in `pwd`"
         echo ""
         echo "GMT Replayed    State  Block Bytes  Standard File Name"
         echo $underline

         for ftp_suffix in $ftp_suffix_list
         do
            case $ftp_suffix in
               $ftp_suffix_active  ) ftp_state="ACTIVE"  ;;
               $ftp_suffix_pending ) ftp_state="PENDING" ;;
               $ftp_suffix_hold    ) ftp_state="HOLD"    ;;
               $ftp_suffix_error   ) ftp_state="ERROR"   ;;
               $ftp_suffix_done    ) ftp_state="DONE"    ;;
               *                   ) ftp_state="UNKNOWN" ;;
            esac

            shift $#
            set -- `ls -ltrd *.*$ftp_suffix 2>/dev/null \
            | awk '{ printf "%s %s %s %s %s\n", $5, $6, $7, $8, $9 }'`
            while [ $# -gt 4 ]
            do
               echo "$1 $2 $3 $4 $5 $ftp_state" \
               | awk '{ printf "%3s %2s %5s %8s %12s  %s\n", $2, $3, $4, $6, $1, $5 }'
               shift 5
            done
         done

      else
         echo "ERROR:  Unable to access $ftp_replay_area"
      fi
   fi

   if [ $show_session != false ]
   then
      echo ""
      echo "SESSION CAPTURE AREA REPORT"
      rm -f $tmp_file.1 $tmp_file.2
      touch $tmp_file.1 $tmp_file.2
      if [ $? -eq 0 -a -x $dps_session_area ]
      then
         cd $dps_session_area
         dps_mission=`ls -tr 2>/dev/null | tail -1`
         if [ -x $dps_mission ]
         then

            cd $dps_mission
            if [ $show_times != false ]
            then
               echo "Files pending in `pwd`"
               if [ ! "$dps_database_user" ]
               then
                  echo "WARNING:  Unable to access mission database"
               fi
               if [ $show_ground != false ]
               then
                  echo ""
                  echo "GMT Captured  Session  Earliest UTC Time      Latest UTC Time        Standard File Name"
                  echo $underline
               else
                  echo ""
                  echo "GMT Captured  Session  Earliest SC Time       Latest SC Time         Standard File Name"
                  echo $underline
               fi
            else
               echo "Files pending in `pwd`"
               echo ""
               echo "GMT Captured    State  Session         MAFs      Packets  Standard File Name"
               echo $underline
            fi

            ls -ltrd $dps_session_file 2>/dev/null | $tail_script | sed 's/session//' \
            | awk '{ printf "%s -1 %s %s %s\n", $9, $6, $7, $8 }' >> $tmp_file.1
            dps_session_list=`cat $tmp_file.1 | awk '{ printf ",%s\n", $1 }'`

            if [ "$dps_session_list" -a "$dps_database_user" ]
            then

               if [ $show_ground != false ]
               then
                  time_earliest_name=GTIMEEARLIEST
                  time_latest_name=GTIMELATEST
               else
                  time_earliest_name=SCTIMEEARLIEST
                  time_latest_name=SCTIMELATEST
               fi

sqlplus $dps_database_user << END_ORACLE_ARGUMENTS 1>/dev/null 2>/dev/null
set pagesize 10000
set linesize 120
set sqlprompt "$sqlprompt"
set sqlcontinue "$sqlprompt"
set sqlnumber OFF
set heading OFF
set feedback OFF
spool $tmp_file.2
select distinct  SESSIONID, APID,
       rawtohex( $time_earliest_name ),
       rawtohex( $time_latest_name ),
                 NUMBEROFDURECEIVED
from             DB_PDPSESSVCAPPIDACCT
where            SESSIONID in ( 0$dps_session_list )
order by         SESSIONID, APID desc;
END_ORACLE_ARGUMENTS

               grep -v "$sqlprompt" $tmp_file.2 2>/dev/null \
               | $dps_time_script 3 4 >> $tmp_file.1
               sort -k 1nb,1 -k 2nbr,2 -o $tmp_file.2 $tmp_file.1 2>/dev/null
            else
               cat $tmp_file.1 2>/dev/null \
               | awk '{ printf "%s -1 NULL NULL 0 %s %s %s %s %s\n", \
               $1, $1, $2, $3, $4, $5 }' >> $tmp_file.2
            fi

            shift $#
            set -- `cat $tmp_file.2 2>/dev/null`
            rm -f $tmp_file.1 $tmp_file.2

            dps_session_start=NULL
            dps_session_stop=NULL
            dps_session_pkts=0

            while [ $# -gt 4 ]
            do
               if [ "$1" = "$6" ]
               then
                  dps_session_start=$3
                  dps_session_stop=$4
                  dps_session_pkts=$5
               else
                  dps_session_id=$1
                  dps_session_date="$3 $4 $5"
                  dps_session_state=ERROR
                  dps_session_name=NULL
                  dps_session_frames=0

                  if [ -x $dps_session_area/$dps_mission/$dps_session_id ]
                  then
                     cd $dps_session_area/$dps_mission/$dps_session_id
                     if [ -f $dps_cas_file ]
                     then
                        if [ -f $dps_log_file ]
                        then
                           dps_session_state=PREP
                           dps_session_name=`ls -td $dps_log_file 2>/dev/null | tail -1`
                           if [ "$dps_session_pkts" = 0 ]
                           then
                              dps_session_pkts=`grep "#packets sent = " \
                              $dps_session_name 2>/dev/null | tail -1 | awk '{ print $4 }'`
                              dps_session_pkts=${dps_session_pkts:=0}
                           fi
                           dps_session_frames=`grep "Total MJ Frame read from amf = " \
                           $dps_session_name 2>/dev/null | tail -1 | awk '{ print $8 }'`
                           dps_session_frames=${dps_session_frames:=0}
                        else
                           dps_session_state=ACTIVE
                           dps_session_name=`ls -td $dps_amf_file 2>/dev/null | tail -1`
                        fi
                     else
                        if [ -f $dps_log_file ]
                        then
                           dps_session_state=COMPLETE
                           dps_session_name=`ls -td $dps_log_file 2>/dev/null | tail -1`
                           if [ "$dps_session_pkts" = 0 ]
                           then
                              dps_session_pkts=`grep "#packets sent = " \
                              $dps_session_name 2>/dev/null | tail -1 | awk '{ print $4 }'`
                              dps_session_pkts=${dps_session_pkts:=0}
                           fi
                           dps_session_frames=`grep "Total MJ Frame read from amf = " \
                           $dps_session_name 2>/dev/null | tail -1 | awk '{ print $8 }'`
                           dps_session_frames=${dps_session_frames:=0}
                        else
                           dps_session_state=ERROR
                           dps_session_name=`ls -td $dps_amf_file 2>/dev/null | tail -1`
                        fi
                     fi

                     if [ "$dps_session_name" ]
                     then
                        dps_session_name=`expr "$dps_session_name" : '\(.*\)\..*'`".$FTP_REPLAY_SUFFIX"
                     else
                        dps_session_name=NULL
                     fi
                  fi

                  if [ $show_times != false ]
                  then
                     echo "$dps_session_date $dps_session_id " \
                     "$dps_session_start $dps_session_stop $dps_session_name" \
                     | awk '{ printf "%3s %2s %5s %8s  %21s  %21s  %s\n", \
                     $1, $2, $3, $4, $5, $6, $7 }'
                  else
                     echo "$dps_session_date $dps_session_state $dps_session_id " \
                     "$dps_session_frames $dps_session_pkts $dps_session_name" \
                     | awk '{ printf "%3s %2s %5s %8s %8s %12s %12s  %s\n", \
                     $1, $2, $3, $4, $5, $6, $7, $8 }'
                  fi

                  dps_session_start=NULL
                  dps_session_stop=NULL
                  dps_session_pkts=0
               fi

               shift 5
            done

         else
            echo "ERROR:  Unable to access mission $dps_mission"
         fi
      else
         echo "ERROR:  Unable to access $dps_session_area"
      fi
   fi

   if [ $show_logs != false ]
   then
      echo ""
      echo "FTP REPLAY LOG REPORT"
      rm -f $tmp_file.1 $tmp_file.2
      touch $tmp_file.1 $tmp_file.2
      if [ $? -eq 0 -a -x $ftp_replay_area ]
      then
       if [ -x $dps_session_area ]
       then
        cd $dps_session_area
        dps_mission=`ls -tr 2>/dev/null | tail -1`
        if [ -x $dps_mission ]
        then
 
            echo "Files pending in `pwd`"
            echo "Files pending in $ftp_replay_area"
            echo ""
            echo "                                                                            NASCOM         Sync   Drop     Fly       Sync   Pct       Good    Bad   Corr    Pct"
            if [ "$show_times" = "true" ]
            then
               echo "Date   Delay    State  Session  First UTC Time       Last UTC Time          Blocks        Bytes  Locks  Wheels       MIFs  MIFs       MAFs   MAFs   MAFs   MAFs  Standard File Name"
            else
               echo "GMT Received    State  Session  First UTC Time       Last UTC Time          Blocks        Bytes  Locks  Wheels       MIFs  MIFs       MAFs   MAFs   MAFs   MAFs  Standard File Name"
            fi
            echo $underline"--------------------------------------------------------------------------------------------"

            replay_log_list=`ls -trd $ftp_replay_area/$dps_log_file 2>/dev/null`
            session_log_list=`ls -trd $dps_session_area/$dps_mission/*/$dps_log_file 2>/dev/null`
            for replay_log in $session_log_list $replay_log_list
            do
               replay_file=`expr "$replay_log" : '.*/\(.*\)\..*'`".$FTP_REPLAY_SUFFIX"
               replay_stat=`ls -ld $replay_log 2>/dev/null | tail -1`
               replay_size=`echo "$replay_stat" | awk '{ printf $5 }'`
               replay_date=`echo "$replay_stat" | awk '{ printf $6" "$7" "$8 }'`
               if [ "$replay_size" -lt "100000" ]
               then

                  replay_start=`grep "FTP REPLAY RECEIVED" $replay_log 2>/dev/null | tail -1 | awk '{ printf $2" "$3" "$4 }'`
                  if [ ! "$replay_start" ]
                  then
                     replay_start=`grep "FTP REPLAY STARTED" $replay_log 2>/dev/null | tail -1 | awk '{ printf $2" "$3" "$4 }'`
                  fi
                  replay_stop=`grep "FTP REPLAY COMPLETED" $replay_log 2>/dev/null | tail -1 | awk '{ printf $2" "$3" "$4 }'`

                  replay_start=`expr "$replay_start" : '\(.*\):..'`
                  replay_start=${replay_start:="$replay_date"}
                  replay_stop=`expr "$replay_stop" : '\(.*\):..'`
                  replay_stop=${replay_stop:="$replay_date"}
                  replay_first=`grep "Ground Time of First Frame" $replay_log 2>/dev/null | tail -1 | awk '{ printf $6 }'`
                  replay_first=${replay_first:="000-00:00:00.000000"}
                  replay_last=`grep "Ground Time of Last Frame" $replay_log 2>/dev/null | tail -1 | awk '{ printf $6 }'`
                  replay_last=${replay_last:="000-00:00:00.000000"}
                  replay_blocks=`grep "Number of Input blocks" $replay_log 2>/dev/null | tail -1 | awk '{ print $5 }'`
                  replay_blocks=${replay_blocks:=0}
                  replay_sync=`grep "Number of Sync Bytes" $replay_log 2>/dev/null | tail -1 | awk '{ print $5 }'`
                  replay_sync=${replay_sync:=0}
                  replay_unsync=`grep "Number of UnSync Bytes" $replay_log 2>/dev/null | tail -1 | awk '{ print $5 }'`
                  replay_unsync=${replay_unsync:=0}
                  replay_droplock=`grep "Number of Sync Drop Locks" $replay_log 2>/dev/null | tail -1 | awk '{ print $6 }'`
                  replay_droplock=${replay_droplock:=0}
                  replay_flywheel=`grep "Number of F8  ( " $replay_log 2>/dev/null | tail -1 | awk '{ print $8 }'`
                  replay_flywheel=${replay_flywheel:=0}
                  replay_mifs=`grep "Number of Frames Output" $replay_log 2>/dev/null | tail -1 | awk '{ print $5 }'`
                  replay_mifs=${replay_mifs:=0}
                  replay_session=`grep "DPS Session ID = " $replay_log 2>/dev/null | tail -1 | awk '{ print $5 }'`
                  replay_session=${replay_session:=0}
                  replay_good_mafs=`grep "#Good MJ Frames = " $replay_log 2>/dev/null | tail -1 | awk '{ print $5 }'`
                  replay_good_mafs=${replay_good_mafs:=0}
                  replay_bad_mafs=`grep "#Bad MJ Frames = " $replay_log 2>/dev/null | tail -1 | awk '{ print $5 }'`
                  replay_bad_mafs=${replay_bad_mafs:=0}
                  replay_corr_mafs=`grep "#reconstructed = " $replay_log 2>/dev/null | tail -1 | awk '{ print $3 }'`
                  replay_corr_mafs=${replay_corr_mafs:=0}

                  replay_total=`expr $replay_sync + $replay_unsync`
                  replay_total=${replay_total:=0}
                  if [ "$replay_total" -lt "100" ]
                  then
                     replay_total=100
                  fi

                  replay_pct_mifs=`expr $replay_sync / \( $replay_total / 100 \)`
                  if [ "$replay_pct_mifs" -gt "100" ]
                  then
                     replay_pct_mifs=100
                  fi

                  replay_total=`expr $replay_good_mafs + $replay_bad_mafs`
                  replay_total=${replay_total:=0}
                  if [ "$replay_total" -lt "1" ]
                  then
                     replay_total=1
                  fi

                  replay_pct_mafs=`expr \( $replay_good_mafs + $replay_corr_mafs \) \* 100 / $replay_total`
                  if [ "$replay_pct_mafs" -gt "100" ]
                  then
                     replay_pct_mafs=100
                  fi

                  replay_error="*"
                  replay_state="-SH-"
                  message=`tail -5 $replay_log 2>/dev/null | grep "FTP REPLAY COMPLETED"`
                  if [ "$message" ]
                  then
                     replay_error=""
                     replay_state="-OK-"
                     message=`grep "Warning 207 occurred" $replay_log 2>/dev/null`
                     if [ "$message" ]
                     then
                        replay_error="*"
                     fi
                  else
                     message=`tail -5 $replay_log 2>/dev/null | grep "FRAME SYNCHRONIZER FAILED"`
                     if [ "$message" ]
                     then
                        replay_error="*"
                        replay_state="-FS-"
                     else
                        message=`tail -5 $replay_log 2>/dev/null | grep "PACKETIZER FAILED"`
                        if [ "$message" ]
                        then
                           replay_error="*"
                           replay_state="-AD-"
                        else
                           message=`tail -5 $replay_log 2>/dev/null | grep "ABORTED"`
                           if [ ! "$message" ]
                           then
                              replay_error=""
                              replay_state="-SH-"
                           fi
                        fi
                     fi
                  fi

                  replay_manual=`grep "MANUAL REPLAY STARTED" $replay_log 2>/dev/null`
                  if [ "$replay_manual" ]
                  then
                     replay_manual="M"
                  else
                     replay_manual="A"
                  fi

                  replay_trend=`grep "TREND TRANSFER STARTED" $replay_log 2>/dev/null`
                  if [ "$replay_trend" ]
                  then
                     replay_trend="T"
                  else
                     replay_trend="-"
                  fi

               else

                  replay_start="$replay_date"
                  replay_stop="$replay_date"
                  replay_error="*"
                  replay_state="-SH-"
                  replay_manual="*"
                  replay_trend="*"
                  replay_session=0
                  replay_first="000-00:00:00.000000"
                  replay_last="000-00:00:00.000000"
                  replay_blocks=0
                  replay_sync=0
                  replay_unsync=0
                  replay_droplock=0
                  replay_flywheel=0
                  replay_mifs=0
                  replay_pct_mifs=0
                  replay_good_mafs=0
                  replay_bad_mafs=0
                  replay_corr_mafs=0
                  replay_pct_mafs=0

               fi

               if [ "$show_times" = "true" ]
               then
                  # calculate replay duration, i.e. Jun 23 00:10 - Jun 22 22:54
                  replay_time=`expr "$replay_stop" : '.* \(.*\) ..:..' 2>/dev/null`
                  replay_parse=`expr "$replay_start" : '.* \(.*\) ..:..' 2>/dev/null`
                  replay_time=`expr $replay_time - $replay_parse 2>/dev/null`
                  replay_time=${replay_time:=0}
                  if [ "$replay_time" != "0" ]
                  then
                     # assume up to 1 day difference at most
                     replay_time=1440 # 24 hours times 60 minutes
                  fi
                  replay_parse=`expr "$replay_stop" : '.* .* ..:\(..\)' 2>/dev/null`
                  replay_time="$replay_time + $replay_parse"
                  replay_parse=`expr "$replay_start" : '.* .* ..:\(..\)' 2>/dev/null`
                  replay_time="$replay_time - $replay_parse"
                  replay_parse=`expr "$replay_stop" : '.* .* \(..\):..' 2>/dev/null`
                  replay_time="$replay_time + $replay_parse \* 60"
                  replay_parse=`expr "$replay_start" : '.* .* \(..\):..' 2>/dev/null`
                  replay_time="$replay_time - $replay_parse \* 60"
                  replay_parse=`eval expr $replay_time 2>/dev/null`
                  replay_time=${replay_parse:=0}"m" # time shown in minutes
                  replay_parse=`expr "$replay_start" : '\(.* .*\) .*' 2>/dev/null`
                  replay_time="$replay_parse $replay_time"
               else
                  # show file receipt time, i.e. Jun 22 22:54
                  replay_time="$replay_start"
               fi

               echo "$replay_time $replay_error$replay_manual$replay_state$replay_trend $replay_session " \
               "$replay_first $replay_last $replay_blocks $replay_sync $replay_droplock $replay_flywheel " \
               "$replay_mifs $replay_pct_mifs% " \
               "$replay_good_mafs $replay_bad_mafs $replay_corr_mafs $replay_pct_mafs% " \
               "$replay_file" >> $tmp_file.1
            done

            sort -k 5nb,5 -o $tmp_file.2 $tmp_file.1 2>/dev/null
            cat $tmp_file.2 2>/dev/null \
            | awk '{ printf "%3s %2s %5s %8s %8s %20s %20s %9s %12s %6s %7s %10s %5s %10s %6s %6s %6s  %s\n", \
            $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18 }'
            rm -f $tmp_file.1 $tmp_file.2

        else
        echo "ERROR:  Unable to access mission $dps_mission"
        fi
       else
        echo "ERROR:  Unable to access $dps_session_area"
       fi
      else
        echo "ERROR:  Unable to access $ftp_replay_area"
      fi
   fi

   if [ $show_product != false ]
   then
      echo ""
      echo "PRODUCT STAGING AREA REPORT"
      rm -f $tmp_file.1 $tmp_file.2
      touch $tmp_file.1 $tmp_file.2
      if [ $? -eq 0 -a -x $dps_product_area ]
      then
         cd $dps_product_area
         dps_mission=`ls -tr 2>/dev/null | tail -1`
         if [ -x $dps_mission ]
         then

            cd $dps_mission
            if [ $show_times != false ]
            then
               echo "Files pending in `pwd`"
               if [ ! "$dps_database_user" ]
               then
                  echo "WARNING:  Unable to access mission database"
               fi
               if [ $show_ground != false ]
               then
                  echo ""
                  echo "GMT Produced  Product  Earliest UTC Time      Latest UTC Time        Spanning Sessions"
                  echo $underline
               else
                  echo ""
                  echo "GMT Produced  Product  Earliest SC Time       Latest SC Time         Spanning Sessions"
                  echo $underline
               fi
            else
               echo "Files pending in `pwd`"
               echo ""
               echo "GMT Produced    State  Product      Service  Total Bytes  Standard File Name"
               echo $underline
            fi

            ls -ltrd $dps_product_file 2>/dev/null | $tail_script | sed 's/product//' \
            | awk '{ printf "%s %s %s %s\n", $9, $6, $7, $8 }' >> $tmp_file.1
            dps_product_list=`cat $tmp_file.1 | awk '{ printf ",%s\n", $1 }'`

            if [ "$dps_product_list" -a "$dps_database_user" ]
            then
 
               if [ $show_ground != false ]
               then
                  time_earliest_name=GROUNDTIMEFIRSTDU
                  time_latest_name=GROUNDTIMELASTDU
               else
                  time_earliest_name=SCTIMEFIRSTDU
                  time_latest_name=SCTIMELASTDU
               fi

sqlplus $dps_database_user << END_ORACLE_ARGUMENTS 1>/dev/null 2>/dev/null
set pagesize 10000
set linesize 120
set sqlprompt "$sqlprompt"
set sqlcontinue "$sqlprompt"
set sqlnumber OFF
set heading OFF
set feedback OFF
spool $tmp_file.2
select distinct  psq.PRODUCTID,
                 psq.SESSIONID,
       rawtohex( pmq.$time_earliest_name ),
       rawtohex( pmq.$time_latest_name )
from             DB_PRODUCTSESSIONQA psq,
                 DB_PRODMESSAGEQUALITYANDACCT pmq
where            psq.PRODUCTID in ( 0$dps_product_list )
and              psq.PRODUCTID = pmq.PRODUCTID
order by         PRODUCTID, SESSIONID;
END_ORACLE_ARGUMENTS

               grep -v "$sqlprompt" $tmp_file.2 2>/dev/null \
               | $dps_time_script 3 4 >> $tmp_file.1
               sort -k 1nb,1 -o $tmp_file.2 $tmp_file.1 2>/dev/null
            else
               cat $tmp_file.1 2>/dev/null \
               | awk '{ printf "%s NULL NULL NULL %s %s %s %s\n", \
               $1, $1, $2, $3, $4 }' >> $tmp_file.2
            fi

            shift $#
            set -- `cat $tmp_file.2 2>/dev/null`
            rm -f $tmp_file.1 $tmp_file.2

            dps_session_list=NULL
            dps_product_start=NULL
            dps_product_stop=NULL

            while [ $# -gt 3 ]
            do
               if [ "$1" = "$5" ]
               then
                  if [ "$dps_session_list" = NULL ]
                  then
                     dps_session_list=""
                     dps_session_last=""
                     dps_product_start=$3
                     dps_product_stop=$4
                  fi
                  if [ "$2" != "$dps_session_last" ]
                  then
                     dps_session_list="$dps_session_list$2,"
                     dps_session_last="$2"
                  fi
               else

                  dps_product_state=ERROR
                  dps_product_id="$1"
                  dps_product_date="$2 $3 $4"
                  dps_product_type=NULL
                  dps_product_size=0
                  dps_product_name=NULL
                  dps_session_list=`expr "$dps_session_list" : '\(.*\),'`
                  dps_session_list=${dps_session_list:=NULL}

                  if [ -x $dps_product_area/$dps_mission/product$dps_product_id ]
                  then
                     cd $dps_product_area/$dps_mission/product$dps_product_id
                     dps_product_type=`grep -h "DATA_SERVICE= " $dps_dan_file 2>/dev/null \
                     | tail -1 | awk '{ print $2 }' | sed 's/;//'`
                     dps_product_type=${dps_product_type:=NULL}
                     dps_product_size=`grep -h "AGGREGATE_LENGTH= " $dps_dan_file 2>/dev/null \
                     | tail -1 | awk '{ print $2 }' | sed 's/;//'`
                     dps_product_size=${dps_product_size:=0}
                     dps_product_name=`grep -h "       FILE_ID= " $dps_dan_file 2>/dev/null \
                     | tail -1 | awk '{ print $2 }' | sed 's/;//'`
                     dps_product_name=${dps_product_name:=NULL}
                  fi

                  if [ "$dps_product_name" != NULL ]
                  then
                     dps_product_state=COMPLETE
                  fi

                  if [ $show_times != false ]
                  then
                     echo "$dps_product_date $dps_product_id $dps_product_start" \
                     "$dps_product_stop $dps_session_list" \
                     | awk '{ printf "%3s %2s %5s %8s  %21s  %21s  %s\n", \
                     $1, $2, $3, $4, $5, $6, $7 }'
                  else
                     echo "$2 $3 $4 $dps_product_state $1 $dps_product_type" \
                     "$dps_product_size $dps_product_name" \
                     | awk '{ printf "%3s %2s %5s %8s %8s %12s %12s  %s\n", \
                     $1, $2, $3, $4, $5, $6, $7, $8 }'
                  fi

                  dps_session_list=NULL
                  dps_product_start=NULL
                  dps_product_stop=NULL

               fi
               shift 4
            done

         else
            echo "ERROR:  Unable to access mission $dps_mission"
         fi
      else
         echo "ERROR:  Unable to access $dps_product_area"
      fi
   fi

   if [ $show_archive != false ]
   then
      echo ""
      echo "BLOCK ARCHIVE AREA REPORT"
      if [ -x $blk_archive_area ]
      then

         cd $blk_archive_area
         echo "Files pending in `pwd`"
         echo ""
         echo "GMT Archived    State  Block Bytes  Standard File Name"
         echo $underline

         ls -ltrd * 2>/dev/null | $tail_script \
         | awk '{ printf "%3s %2s %5s %8s %12s  %s\n", $6, $7, $8, "ARCHIVED", $5, $9 }'

      else
         echo "ERROR:  Unable to access $blk_archive_area"
      fi
   fi

done
kill $$
sleep 60
