#! /bin/sh
#
# @(#)selectobject	1.3	LPS_UNX_COM	02/19/95
#
# Copyright 1993   Digital Equipment Corporation, Maynard, MA
#
# selectobject
#
# Common script to have the user select the name of a defined object
# of a specified class, with an optionally specified default name.
# The name is written into the specified output file for use by the
# caller.
#
# Parameters:
#    $1 - Class identifier, one of {ENV, PS, PC, MC}.
#    $2 - A question string that serves as a title above the object list.
#    $3 - Path of the file to receive the selected object name.
#    $4 - Optional default object name, can be null (see Notes below).
#
# Notes:
# This is a somewhat complex script due to the nature of its use within
# higher-level support scripts, where selection of a target object is
# sometimes required.  Here is how this script behaves:
#
#    - The specified output file is immediately emptied upon startup
#      by coping /dev/null to it.
#
#    - The signals 1, 2, 3 and 15 are trapped; if the user invokes one
#      of these signals, the script immediately exits with a value of 2,
#      leaving the output file empty (implying a null user response, if
#      necessary).
#
#    - If no objects are defined for the specified class, then the user
#      is notified of that fact, and the script exits with a value of 1,
#      leaving the output file empty.
#
#    - If NO default name is specified:
#	  * If the resulting object list contains only a single name,
#	    then that name automatically becomes the default name.
#
#	  * If the resulting object list contains more than one name,
#	    then the default value is NULL (implications described below).
#
#    - If a default name IS specified, then the name is checked for
#      inclusion in the resulting object name list:
#	  * If name exists in the list, then the name remains the default.
#	  * If name DOES NOT exist in the list, then the default is set NULL.
#
#    - If the user selects the default response (ie, user enters RETURN):
#	  * If a default name exists (ie, not NULL), then that name is
#	    written to the output file, and the script exits with a value of 0.
#
#	  * If the default name is NULL, then the user is told that a name
#	    MUST BE SELECTED, and that CTRL/C can be used to terminate the
#	    procedure.
#
# Got that?  Good...
#
# Global variables:
#    ECHON
#    FUNCS
#    LOCALHOST
#    LPSODB
#    LPSODBLIST
#    TMPDIR
#    TR
#
# Exit values:
#    0 - User either selected a valid name, or selected the default name
#	 when a non-NULL default name was specified; the output file
#	 contains the resulting object name.
#    1 - No objects of the specified class are currently defined,
#	 the output file is empty.
#    2 - User signaled termination, the output file is empty.
#    3 - An internal error has occurred (probably with the LPSODB),
#	 the output file is empty.
###

CLASS=$1
QUESTION="$2"
OUTFILE=$3
DEFVAL=$4

DESC="`getobjdesc $CLASS`"

ODBERRS=$TMPDIR/odberrs.$$
ODBLIST=$TMPDIR/odblist.$$

# Immediately zero the output file, then setup a trap of all
# required signals.

if cp /dev/null $OUTFILE
then
    trap 'exit 2' 1 2 3 15
else
    exit 3   # Error???
fi

rm -f $ODBERRS $ODBLIST

# Import the standard shell function library

. $FUNCS

# Get a sorted list of the object names into the list file, then
# count the entries.  If there are no objects currently defined,
# then tell the user and exit with the appropriate exit value.

if $LPSODBLIST -f $LPSODB -c $CLASS 1> $ODBLIST 2>> $ODBERRS
then
    if COUNT="`wc -l < $ODBLIST`"   # Count the number of names returned
    then
	if [ $COUNT -eq 0 ]
	then
	    echo
	    echo "    No ${DESC}s are currently defined on $LOCALHOST"
	    rm -f $ODBERRS $ODBLIST
	    exit 1
	fi
    else
	exit 3   # Leave tmp files, they might be useful
    fi
else
    badodb $ODBERRS
    exit 3	 # Leave tmp files, they might be useful
fi

# At this point we know there is at least one object defined for
# the specified class.  Fetch the list into a local variable, then
# resolve exactly what is supposed to be the default name, if any.

NAMELIST="`sort $ODBLIST`"

rm -f $ODBLIST $ODBERRS	   # No longer needed these tmp files

DEFAULT=""		   # Will contain the resolved default name (or NULL)

if [ "$DEFVAL" ]	   # Validate the default, if specified
then
    for name in $NAMELIST
    do
	if [ $name = $DEFVAL ]   # Is the default name in this list?
	then
	    DEFAULT=$DEFVAL	 # Yes, so its a valid default name
	    break
	fi
    done
else   # No default specified, so make a default if only one name exists
    if [ $COUNT -eq 1 ]
    then
	DEFAULT=$NAMELIST
    fi
fi

# Present the object name list to the user and have the user select
# the desired name.  If there is no default name, but the user selects
# the default response (ie, simply presses RETURN at the prompt), then
# inform the user that either a response MUST be entered, or the user
# should signal termination of the procedure.  If a response is entered,
# then the corresponding name is resolved and written to the output file.
#
# We keep looping until either a response is entered, or a termination
# signal is generated.

while true
do
    choose "$DEFAULT" "$QUESTION" $NAMELIST

    if [ "$RESPONSE" = "" ]
    then
	echo
	echo "    Sorry, but you MUST select an object name from the above"
	echo "    list by entering the number next to the name.  If you do"
	echo "    want to select any of the above names, simply press the"
	echo "    CTRL/C key to terminate the procedure."
    else
	echo $RESPONSE > $OUTFILE
	rm -f $ODBERRS
	exit 0
    fi
done
