#! /bin/sh
#
# @(#)listobjects	1.4	LPS_UNX_COM	02/19/95
#
# Copyright 1994   Digital Equipment Corporation, Maynard, MA
#
# listobjects
#
# This script is used by all top-level management scripts to display
# a list of all defined objects of a target class within the LPS
# Object Database.
#
# Parameters:
#    $1 - Class identifier, one of {ENV, PS, PC, MC}.
#
# Note that this script is not really implemented to handle the ENV class.
#
# Global variables:
#    ECHON
#    LOCALHOST
#    LPSODB
#    LPSODBLIST
#    TMPDIR
#
# Exit value:
#    0 - Success, all defined objects were displayed.
#    1 - Failure, some sort of error occurred, either with the local
#	 environment (shell variable assignments) or the LPSODB file.
###
ODBLIST=$TMPDIR/odblist.$$
ODBERRS=$TMPDIR/odberrs.$$

CLASS=$1
DESC="`getobjdesc $CLASS`"

# Display the common header lines

clear

echo
echo "List all defined ${DESC}s..."
echo

# 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.  Otherwise, use the count value to
# determine the grammatically correct header line and display it.

if $LPSODBLIST -f $LPSODB -c $CLASS 1> $ODBLIST 2>> $ODBERRS
then
    if COUNT="`wc -l < $ODBLIST`"   # Count the number of names returned
    then
	COUNT=`expr $COUNT`   # Strips off leading spaces for some systems

	if [ $COUNT -eq 0 ]
	then
	    echo
	    echo "    No ${DESC}s are currently defined on $LOCALHOST"
	    rm -f $ODBERRS $ODBLIST
	    exit 0
	else
	    if [ $COUNT -gt 1 ]
	    then
		s1="are"
		s2="s"
	    else
		s1="is"
		s2=""
	    fi
	    echo "There $s1 currently $COUNT ${DESC}${s2} defined on ${LOCALHOST}:"
	    echo
	fi
    else
	exit 1   # Leave tmp files, they might be useful
    fi
else
    badodb $ODBERRS
    exit 1	 # Leave tmp files, they might be useful
fi

# Get the sorted list into a local variable, then display each
# object name.  For each object, display its name (in quotes)
# and some other item of information to better inform the user
# of what the object is used for or associated with.

NAMELIST="`sort $ODBLIST`"

case $CLASS in
    PC ) opt="-p" ;;
    MC ) opt="-m" ;;
    PS ) opt="-s" ;;
esac

rm -f $ODBLIST		   # No longer needed this tmp file
BADODB=false

for name in $NAMELIST
do
    $ECHON "          \"$name\"  ["

    # Display the object's DESCRIPTION if set; otherwise produce
    # some mildly useful descriptive text based on the target class.

    if DESC="`$LPSODBLIST -f $LPSODB $opt $name -v description 2>> $ODBERRS`"
    then
	if [ -z "$DESC" ]
	then
	    if [ $CLASS != PS ]   # For Print Clients & Management Clients
	    then
		if PSNAME="`$LPSODBLIST -f $LPSODB $opt $name -v psname 2>> $ODBERRS`"
		then
		    $ECHON "assigned to printer \"$PSNAME\", "
		else
		    BADODB=true
		    break
		fi
	    else
		PSNAME=$name
	    fi

	    if MODEL="`$LPSODBLIST -f $LPSODB -s $PSNAME -v model 2>> $ODBERRS`"
	    then
		case $MODEL in
		    LPS17 ) DESC="PrintServer 17" ;;
		    LPS20 ) DESC="PrintServer 20" ;;
		    LPS32 ) DESC="PrintServer 32" ;;
		    LPS40 ) DESC="PrintServer 40" ;;
			* ) DESC="PrintServer $MODEL" ;;  # ???
		esac
	    else
		BADODB=true
		break
	    fi

	    if [ $CLASS = PS ] ; then DESC="$DESC Printer" ; fi
	fi

	echo "${DESC}]"
    else
	BADODB=true
	break
    fi
done

# Check to see if we encountered any LPSODB problems, and if so,
# notify the user and exit with an error status value.

if $BADODB
then
    echo
    badodb $ODBERRS
    exit 1	 # Leave tmp files, they might be useful
fi

# All done, so cleanup and get out

rm -f $TMPDIR/*.$$
exit 0
