#! /bin/sh
#
# @(#)setPSvars	1.10	LPS_UNX_COM	3/13/95
#
# Copyright 1994   Digital Equipment Corporation, Maynard, MA
#
# setPSvars
#
# Central script to control user-specified settings of PrintServer printer
# configuration variables.  The net result is the creation of an output
# file that contains ready-to-import shell commands to set all appropriate
# PrintServer printer shell variables.
#
#    NOTE:  Within this script resides *important* relationships
#	    between various configuration variables.  Hence, if
#	    the PS variable list changes, this script should be
#	    updated as appropriate.
#
# This script expects that the working directory is at the top of the
# appropriate directory tree (LPSROOT or LPSKIT).
#
# Parameters:
#    $1 - Variable setting mode, either "add" or "mod"
#    $2 - The name of the PrintServer printer
#    $3 - Path of the attribute variable list file (LPSODB stuff)
#    $4 - Path of the configuration variable list file ($CONFIG file stuff)
#    $5 - Path of the resulting output file
#
# Global variables:
#    ECHON
#    FUNCS
#    LPSODB
#    LPSODBLIST
#    OSTYPE
#    PCTYPE
#    All PS_* variables
#
# Exit values:
#    0 - Success, the output should be imported by the caller.
#    1 - Failure of some kind, so do NOT import the output file.
###

SCRIPTNAME=$0
MODE=$1
PSNAME=$2
VARLIST=$3
CFGLIST=$4
OUTFILE=$5

TMPFILE="$TMPDIR/psvars.$$"

# Perform some basic integrity checks

if [ ! -f "$VARLIST" ]
then
    echo "$SCRIPTNAME: The variable list file \"$VARLIST\" does not exist!"
    exit 1
fi

if [ ! -f "$CFGLIST" ]
then
    echo "$SCRIPTNAME: The configuration variable file \"$CFGLIST\" does not exist!"
    exit 1
fi

if [ -z "$OUTFILE" ]
then
    echo "$SCRIPTNAME: The output file parameter was not specified!"
    exit 1
fi

rm -f $OUTFILE

# Load the common kit shell function library,
# then export the common and client variables.

. $FUNCS
expkitvars
expobjvars PS

# If this is an "add" operation, then set a one-time flag that prevents
# the user from being asked to respond to the same required configuration
# questions when the object was created.

if [ $MODE = add ]
then
    firstadd=true
    defmode="step-by-step"
else
    firstadd=false
    defmode="menu-oriented"
fi

# Handling of the set of query-able variables is quite different here than
# in lpsmpc & lpsmmc.  For the step-by-step technique, we use the COMBINED
# set of attribute/configuration variables as the ordered set of variables
# to present to the user.
#
# For the menu-oriented technique, things get *really wierd.  We hand the
# attribute variable ($VARLIST) path to the `objvarmenu' script, but
# instead of using an output file to fetch the name of the user-selected
# variable, the actual exit value of the objvarmenu script is used to
# index into the combined var list file to extract the variable name.

ALLVARSFILE="$TMPDIR/allvars.$$"

cat $VARLIST $CFGLIST > $ALLVARSFILE	# Order is important here

# -------------------- TOP OF THE CONFIGURATION LOOP --------------------

CONFIGERROR="FAILURE to properly set a PrintServer printer configuration variable!!!"

while true
do
    list="step-by-step menu-oriented"
    quest="How do you want to configure the PrintServer printer"

    if $firstadd
    then
	defmode="step-by-step"
    else
	defmode="menu-oriented"
    fi

    choose "$defmode" "$quest" $list

    if [ "$RESPONSE" = "step-by-step" ]
    then
	for var in `cat $ALLVARSFILE`
	do
	    # First time add operation?  Don't ask for required vars.
	    #
	    # NOTE:  This method only works if the "MODEL" variable
	    #        is the last in the set of required variables.

	    if $firstadd
	    then
		if [ $var = MODEL ]	# Start processing after this var
		then
		    firstadd=false
		fi		
		continue
	    fi

	    eval val="\"\$PS_$var\""         # Resolve the var's current value

	    script=`echo set$var | cut -c1-14`   # Resolve script filename

	    # Special handling must be done to transparently change
	    # the bootfile and description if the model changes.

	    origmodel="$PS_MODEL"

	    if $script PS $OSTYPE $PCTYPE "$PS_MODEL" "$val" $TMPFILE
	    then
		val="`cat $TMPFILE`"
		eval PS_$var="\"$val\""
	    else
		echo
		echo $CONFIGERROR
		exit 1
	    fi

	    # If the model just changed, then possibly reset the bootfile
	    # and the description.  If the original description was either
	    # blank or contained the default description, then reset it to
	    # the default description for the new printer model; otherwise,
	    # leave it intact.

	    if [ "$origmodel" != "$PS_MODEL" ]
	    then
		PS_BOOTFILE=`getbootfile "$PS_MODEL"`

		desc="$PS_DESCRIPTION"	 # for convenience

		if [ -z "$desc" -o "$desc" = "`getPSdesc $origmodel`" ]
		then
		    PS_DESCRIPTION=`getPSdesc "$PS_MODEL"`
		fi
	    fi
	done

	echo
	pause "Press RETURN to view the complete configuration... "

	expobjvars PS
	showobjvars PS $PSNAME $VARLIST

    else   # ----- Menu-oriented configuration starts here -----

	while true
	do
	    if objvarmenu PS $PSNAME $VARLIST
	    then
		break   # Break out of inner loop
	    else
		varindex=$?	# The menu number of the selected variable

		var="`sed -n -e ${varindex}p $ALLVARSFILE`"
		eval val="\"\$PS_$var\""

		script=`echo set$var | cut -c1-14`   # Resolve script filename

		# Special handling must be done to transparently change
		# the bootfile and description if the model changes.

		origmodel="$PS_MODEL"

		if $script PS $OSTYPE $PCTYPE "$PS_MODEL" "$val" $TMPFILE
		then
		    val="`cat $TMPFILE`"
		    eval PS_$var="\"$val\""
		else
		    echo
		    echo $CONFIGERROR
		    exit 1
		fi

		# If the model just changed, then possibly reset the bootfile
		# and the description.  If the original description was either

		if [ "$origmodel" != "$PS_MODEL" ]
		then
		    PS_BOOTFILE=`getbootfile "$PS_MODEL"`

		    desc="$PS_DESCRIPTION"	 # for convenience

		    if [ -z "$desc" -o "$desc" = "`getPSdesc $origmodel`" ]
		    then
			PS_DESCRIPTION=`getPSdesc "$PS_MODEL"`
		    fi
		fi
	    fi
	done
    fi

    firstadd=false

    if yesno x "Are you finished configuring PrintServer printer $PSNAME"
    then
	break   # Break out of outer loop
    fi
done

# The user has indicated the configuration is complete, so write all
# PS attribute and configuration variables to the specified output file,
# then cleanup and get out.

touch $OUTFILE

for attr in `cat $ALLVARSFILE`
do
    var="PS_$attr"
    eval val="\"\$$var\""

    if echo "${var}=\"${val}\"" >> $OUTFILE
    then
	:
    else
	echo
	echo "FAILURE to copy complete PrintServer printer variables to $OUTFILE !!!"
	exit 1
    fi

done

#
# NOTE:  We must manually output the final value of PS_BOOTFILE, since
#        it is not part of the formal vars file definition.

if echo "PS_BOOTFILE=$PS_BOOTFILE" >> $OUTFILE
then
    :
else
    echo
    echo "FAILURE to copy printer boot filename to $OUTFILE !!!"
    exit 1
fi

rm -f $ALLVARSFILE $TMPFILE

exit 0
