#! /bin/sh
#
# @(#)objvarmenu	1.7	LPS_UNX_COM	2/19/95
#
# Copyright 1993   Digital Equipment Corporation, Maynard, MA
#
# objvarmenu
#
# A generic script that provides a display of a set of attributes for
# a given LPS object class, and allows the user to specify which of
# the attributes should be modified.
#
# Parameters:
#    $1 - LPS class identifier, one of {PC, MC, PS, ENV}
#    $2 - The object name, which should be null for the ENV class
#    $3 - Path of the "variable list" file
#    $4 - OPTIONAL "output file" in which to store the name of the
#         variable to be modified.
#
# The "variable list" file is one that contains a list of class attribute
# base names in UPPERCASE, one attribute name per line, for example:
#
#	ACCTUNIT
#	LOGLEVEL
#	MAILADMADDRS
#
# It is important to note that the attribute names MUST be in UPPERCASE.
#
# Global variables:
#    ECHON
#    FUNCS
#    LPSODB
#    LPSODBLIST
#    PAGER
#    TMPDIR
#    TR
#    All variables for the corresponding class (eg, "PC_PSNAME")
#    relating to the list of attributes.
#
# Exit values:
#    The exit value depends on the input from the user.  If the user
#    simply enters RETURN, then the exit value is 0; if the output
#    file is specified, then it is cleared.
#
#    If the user enters a number corresponding to the variable to be
#    modified, then the return value is the number entered; this value
#    implies to the caller that the variable listed on line "n" of the
#    variable list file is the variable the user desires to modify.
#    If the output file is specified, then the name of the variable name
#    is stored in the output file (with no trailing newline character).
#
#    NOTE:  There is NO error exit value associated with this script.
#	    An error that may occur here will probably also occur in
#	    the caller script as well, and probably long before this
#	    script is invoked.
###

CLASS="$1"
OBJNAME="$2"
VARLIST="$3"
OUTFILE="$4"

MENUFILE=$TMPDIR/varmenu.$$

export ECHON FUNCS LPSODB LPSODBLIST PAGER TR

. $FUNCS	   # Import shell functions library for "checknumrange()"
expobjvars $CLASS  # Export all object variables

while true
do
    clear

    if showobjvars $CLASS $OBJNAME $VARLIST > $MENUFILE
    then
	pause	# Some sort of error occurred if exit value is zero
	exit 1
    else
	varcnt=$?	# Exit value is the count of variable lines displayed
    fi

    $PAGER << ENDOFINPUT
`cat $MENUFILE`

Enter the number of the variable to modify from the above list,
ENDOFINPUT
    $ECHON "or enter 'r' to redisplay, or enter RETURN to finish: "
    read resp
    if [ -z "$resp" ]
    then
	if [ "$OUTFILE" ] ; then cat /dev/null > $OUTFILE ; fi
	exit 0
    fi

    if [ "$resp" != "r" -a "$resp" != "R" ]
    then
	if checknumrange 1 $varcnt "" "$resp"
	then
	    # User has entered a valid menu number.  If an output file
	    # was specified, then write out the name of the corresponding
	    # variable from the $ATTLIST file.  Either way, exit with the
	    # menu item number.
	    #
	    # NOTE:  For PS objects, the resulting number may reference
	    #        a variable that does not exist within the $ATTLIST
	    #        file (ie, it refers to a $CONFIG file variable).
	    #        In this case, the caller must resolve what to do.

	    if [ "$OUTFILE" ]
	    then
		sed -n -e "${resp}p" $VARLIST > $OUTFILE
	    fi
	    exit $resp
	else
	    cat << ENDOFINPUT

    Please enter one of the following:
	- The number of the configuration variable to modify as shown
	  in the above list, or
	- The letter "r" (or "R") to redisplay the variable list,
	  or
	- The RETURN key (with no other characters) to indicate that
	  you are done modifying the configuration variables.

ENDOFINPUT
	    pause
	fi
    fi
done
