#!/bin/sh
#******************************************************************************
#                       ISTP LZPR SOFTWARE
#               Property of the U.S. Government
#                       NASA/GSFC/Code 560
#******************************************************************************
#
# UNIT NAME: plog
#
# PURPOSE: To display DPS software error logs on screen
#
# INVOCATION METHOD: plog [-ftqh] [-n #files] [-a logfile] [missions]
#
# CHANGE HISTORY:
# Author        Change ID  Build/Release   Date      Description of Change
# ------        ---------  ------------- ---------   ---------------------
# R. Wiechert   ISTP       R1.2          04/22/98    Enhancement of PacorII code
# R. Wiechert   ISTP       R1.2A         05/09/98    More enhancements for ISTP
# R. Wiechert   ISTP       R1.2B         06/01/98    Remove missing logs check
#
# NOTES:
# see help section below
#
#******************************************************************************
# default options
etc_passwd=/etc/passwd
banner_line="========================================"
banner_line="$banner_line$banner_line"
user_name=`/usr/ucb/whoami 2>/dev/null`
log_file_name=`hostname`-Log
log_table_name=db_EventLog
default_mission=false
alternate=false
maxlognum=-1
file=true
table=true
quiet=false
more=more
help=null
 
# determine runtime options
while getopts n:a:ftqh option
do
   case $option in
      n) maxlognum=$OPTARG ;;
      a) file=false;  table=false;  alternate=true;  log_file_name=$OPTARG ;;
      f) file=true;   table=false ;;
      t) file=false;  table=true ;;
      q) quiet=true;  more=cat ;;
      h) help=true ;;
      *) help=true ;;
   esac
done
 
# validate mission list arguments
shift `expr $OPTIND - 1`
if [ $# = 0 ]
then
   default_mission=true
   set -- "$user_name"
fi

# display help if needed
if [ "$help" = "true" ]
then
more <<END_USAGE_TEXT
 
USAGE:     plog [-ftqh] [-n #files] [-a logfile] [missions]

PURPOSE:   Automatically displays the specified number of
           software error logs located within the mission
           opslog directories.

OPTIONS:   -h      display help screen
           -n      display recent #files
           -a      specify alternate log
           -f      display log files only
           -t      display log tables only
           -q      disable interactive prompts

SYNOPSIS:  The traditional DPS software error log files
           and database tables are displayed on screen,
           including live versions whenever possible.
           Live versions are available only when logged
           in using the proper mission account.  The -a
           option may be used to specify and display an
           alternate set of log files.  The -q option
           may be used to disable interactive prompts
           when redirecting output to a file.

END_USAGE_TEXT
exit 9
fi
 
printf "\n"
while [ $# -gt 0 ]
do
   mission_name="$1"
   account=`grep "^$mission_name:" $etc_passwd`
   mission_home=`expr "$account" : '.*:.*:.*:.*:.*:\(.*\):.*'`
   if [ "$mission_home" ]
   then
      logdir="$mission_home/opslog"
   else
      printf "ERROR:  Unknown mission:  $mission_name"
      shift 1
      continue
   fi

   # parse alternate log location
   if [ "$alternate" = "true" ]
   then
      log_file_path=`dirname $log_file_name`
      if [ "$log_file_path" != "." ]
      then
         logdir="$log_file_path"
      fi
      log_file_name=`basename $log_file_name`
   fi

   if [ "$default_mission" = "false" ]
   then
      answer=null
      while [ "$answer" != "Y" -a "$answer" != "N" -a "$answer" != "Q" ]
      do
         if [ "$quiet" = "false" ]
         then
            printf "Display $mission_name software error logs now (Yes/No/Quit)? "
            read answer
            answer=`echo "$answer" | tr '[a-z]' '[A-Z]' | tr -cd "YNQ"`
         else
            answer="Y"
         fi
      done
      case $answer in
         N) shift 1; continue ;;
         Q) exit 0 ;;
      esac
      printf "\n"
   fi

   thislog="$maxlognum"
   while [ "$thislog" -ge "0" ]
   do
      if [ "$file" = "true" -o "$alternate" = "true" ]
      then
         if [ -f "$logdir/$log_file_name.$thislog" ]
         then
            printf "$banner_line\n\n"
            printf "          `date`\n"
            printf "          $mission_name $log_file_name.$thislog\n\n"
            printf "$banner_line\n\n"
            $more $logdir/$log_file_name.$thislog
            printf "\n"
         fi
      fi
      if [ "$table" = "true" ]
      then
         if [ -f "$logdir/$log_table_name.$thislog" ]
         then
            printf "$banner_line\n\n"
            printf "          `date`\n"
            printf "          $mission_name $log_table_name.$thislog\n\n"
            printf "$banner_line\n\n"
            $more $logdir/$log_table_name.$thislog
            printf "\n"
         fi
      fi
      thislog=`expr $thislog - 1`
   done

   if [ "$mission_name" = "$user_name" ]
   then
      if [ "$file" = "true" ]
      then
         printf "$banner_line\n\n"
         printf "          `date`\n"
         printf "          $mission_name $log_file_name\n\n"
         printf "$banner_line\n\n"
         $more $PACOR2HOME/pdp/$log_file_name
         printf "\n"
      fi

      if [ "$table" = "true" ]
      then
         printf "$banner_line\n\n"
         printf "          `date`\n"
         printf "          $mission_name $log_table_name\n\n"
         printf "$banner_line\n\n"
         answer=null
         while [ "$answer" != "Y" -a "$answer" != "N" -a "$answer" != "Q" ]
         do
            if [ "$quiet" = "false" ]
            then
               printf "Access $mission_name database now (Yes/No/Quit)? "
               read answer
               answer=`echo "$answer" | tr '[a-z]' '[A-Z]' | tr -cd "YNQ"`
            else
               answer="Y"
            fi
         done
         printf "\n"
         if [ "$answer" = "Y" ]
         then

sqlplus `decrypt.sql $PICSHOME` << ENDORACLE | $more
            set sqlprompt ""
            set sqlnumber off
            column MESSAGENUM format 99999999
            column MESSAGEID format 99999999
            column ACQUISITIONSESSIONID heading SESSIONID format 99999999
            column PRODUCTID format 99999999
            column PARM0 format A32
            column PARM1 format A32
            column PARM2 format A32
            set pagesize 1000
            set linesize 160
            select MESSAGENUM, EVENTDATE, MESSAGEID,
            ACQUISITIONSESSIONID, PRODUCTID, PARM0, PARM1, PARM2
            from $log_table_name order by MESSAGENUM;
ENDORACLE
printf "\n"

         elif [ "$answer" = "Q" ]
         then
            exit 0
         fi
      fi
   fi

   shift 1
done

printf "\n"
exit 0
