#! /bin/sh
#
# @(#)showlogfile	1.2	LPS_UNX_COM	2/20/95
#
# Copyright 1995   Digital Equipment Corporation, Maynard, MA
#
# showlogfile
#
# A script to examine the contents of a log file associated with an
# object of the specified class.
#
# Parameters:
#    $1 - Object class descriptor (eg: PC, PS, MC, etc)
#    $2 - Pager path (optional, default defined by PAGER variable)
#    $3 - Tailer path (optional, default defined by TAIL variable, or "tail")
#
# Global variables:
#    FUNCS
#    LPSODB
#    LPSODBLIST
#    PAGER
#    TAIL
#    TMPDIR
#
# Exit values:
#    0 - Success, output file contains newly set attribute value.
#    1 - Error or interruption occurred, error messages go to stderr,
#	 contents of the output file are undefined.
###

# Due to the nature of this script, SIGINT is trapped to provide
# a clean exit.

trap 'exit 0'  2

# Validate the cmdline args as best we can

if [ $# -lt 1 -o $# -gt 3 ]
then
    echo 2>&1
    echo "${0}:  invalid command line parameters: \"$*\"" 2>&1
    echo 2>&1
    exit 1
fi

CLASS=$1

case $CLASS in
    PC ) CLASSOPT=p ;;
    MC ) CLASSOPT=m ;;
    PS ) CLASSOPT=s ;;

    * ) echo 2>&1
	echo "${0}: unknown LPS class specified: \"$CLASS\""
	echo 2>&1
	exit 1
esac

# Set PAGER and TAIL according to cmdline args or variables or defaults

if [ "$2" ]
then
    PAGER="$2"
else
    if [ -z "$PAGER" ]
    then
	PAGER=more
    fi
fi

if [ "$3" ]
then
    TAIL="$3"
else
    if [ -z "$TAIL" ]
    then
	    TAIL="tail -f"	# Note the hardcoded "-f" option here...
    fi
fi

export PAGER TAIL

# Import the standard shell function library and export all
# common kit variables for use by our subprocesses.
 
if . $FUNCS
then
    expkitvars
else
    exit 1
fi

# Clear the display and present a title line

OBJDESC="`getobjdesc $CLASS`"
 
clear

echo
echo "Examine the log file of an existing ${OBJDESC}..."
 
# Ask the user to select the object to display.  If a null
# response is entered, it implies a return to the top-level
# menu, so simply return success.
 
OUTFILE=$TMPDIR/logobj.$$

selectobject $CLASS "Which $OBJDESC log file is to be displayed" $OUTFILE ""

case $? in
    0 ) OBJNAME=`cat $OUTFILE` ;;
    * ) exit 1                 ;;
esac

# If the target class is PS, then determine which (if any) existing
# MC objects are related to this PS that have logfiles.  If more than
# one MC object is associated with this PS (unlikely), then find out
# which of them are setup for event logging (highly unlikely) and
# allow the user to select which one to examine.
#
# If only a single MC is defined for the target PS, and that MC has
# a logfile defined, then tell the user of this fact, then display
# the logfile.
#
# If no MC objects exist (either none actually exist, or none of them
# are associated with the PS object, or none associated with the PS
# have event logging enabled), then sadly tell the user, then exit.

GETODB="$LPSODBLIST -f $LPSODB"		# Convenient command symbol

if [ $CLASS = PS ]
then
    echo
    echo "Searching for a suitable Management Client log file..."

    allmclist=`$GETODB -c mc`		# Get list of all defined MC's
    mclist=""				# Will contain candidate MC's

    if [ "$allmclist" ]			# At least one MC defined?
    then
	for mc in $allmclist
	do
	    psname=`$GETODB -m $mc -v psname`

	    if [ $OBJNAME = "$psname" ]	# Associated with this PS object?
	    then
		logenabled=`$GETODB -m $mc -v offerelog`

		if boolval $logenabled
		then
		    mclist="${mclist}$mc "   # Add to list of candidate MC's
		fi
	    fi
	done
    fi

    if [ "$mclist" ]			# One or more andidate MC's found?
    then
	mccnt=`echo $mclist | wc -w`	# Get number of MC names in the list

	if [ $mccnt -eq 1 ]		# Only a single MC defined?  Great!
	then
	    OBJNAME=$mc			# Reset target object name to the MC
	    echo
	    echo "Using log file from Management Client \"$OBJNAME\""
	else
	    echo
	    echo "More than one Management Client currently exist that"
	    echo "have an event log for this printer."

	    choose "" "Which Management Client to use" $mclist

	    OBJNAME=$RESPONSE		# Reset target object name to the MC
	fi

	CLASSOPT=m			# Reset the class option to "MC"
    else
	echo
	echo "Sorry, but there are no Management Clients currently defined"
	echo "on $LOCALHOST that are configured to provide"
	echo "Event Logging Services to the printer named \"$OBJNAME\"."
	echo
	echo "Without a Management Client offering Event Logging Services"
	echo "for the printer, there is no event log you can examine."
	exit 0
    fi
fi

# Derive the log file associated with the relevant object,
# then display it, if it exists.

LOGFILE=`$GETODB -$CLASSOPT $OBJNAME -v logfile`

if [ -f $LOGFILE ]
then
    # Ask the user to choose which program to use to examine the log file;
    # use the TAIL program as the default, as it's usually the most useful.

    echo
    echo "Available programs to perform the examination:"
    echo
    echo "   TAIL:   $TAIL"
    echo "   PAGER:  $PAGER"

    choose TAIL "Which program to use" TAIL PAGER

    case "$RESPONSE" in
	PAGER ) PROGRAM="$PAGER"
		;;

	    * ) PROGRAM="$TAIL"
		echo
		echo "Press the interrupt key (CTRL/C) to quit..."
		echo
		;;
    esac

    # Display the log file using the selected program

    $PROGRAM $LOGFILE

else
    echo
    echo "Log file does not exist:  $LOGFILE"
    echo
fi

exit 0
