#! /bin/sh
#
# @(#)setPCvars	1.10	LPS_UNX_COM	3/18/95
#
# Copyright 1995   Digital Equipment Corporation, Maynard, MA
#
# setPCvars
#
# Central script to control user-specified settings of Print Client
# configuration variables.  The net result is the creation of an output
# file that contains ready-to-import shell commands to set all appropriate
# Print Client shell variables.
#
#    NOTE:  Within this script resides *important* relationships
#	    between various configuration variables.  Hence, if
#	    the PC 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 Print Client
#    $3 - Variable list file path (already set to be PCTYPE-dependent)
#    $4 - Attribute list file path (already set to be PCTYPE-dependent)
#    $5 - Path of the resulting output file
#
# Global variables:
#    ECHON
#    FUNCS
#    LPSODB
#    LPSODBLIST
#    OSTYPE
#    PCTYPE
#    All PC_* 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
PCNAME=$2
VARLIST=$3
ATTRLIST=$4
OUTFILE=$5

TMPFILE="$TMPDIR/pcvars.$$"

# Perform some basic integrity checks

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

if [ ! -f "$ATTRLIST" ]
then
    echo "$SCRIPTNAME: The attribute list file \"$ATTRLIST\" 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 PC

# Assignment of the important "configuration variable groups".  A variable
# is the list of *base* attribute names for all variables interrelated in
# some fashion (eg: banners, logging, accounting, etc).  If a new variable
# is created or deleted, then these variable assignments must be updated.
#
# For each group, one variable serves as the "control variable" that controls
# the query of the variables within that group.  The control variable is NOT
# included in the variable group list; only the *dependent* variables are
# described by the group list variable (one that is prefixed with "G_").
#
# Note also that the banner images group (G_IMAGES), is actually doubly
# dependent: this group's control variable (PC_ENABLEIMAGES) is a member of
# the banners group (G_ENABLEBANNERS), resulting in the fact that if
# banners are disabled (PC_ENABLEBANNERS=NO), then none of the variables in
# the banner images group are part of the step-by-step query process.  For
# example, if banners are enabled, then the user is queried for enabling
# banner images; if images are enabled, then all variables relating to
# banner images are presented to the user.
#
# The GROUPS variable should always contain the list of G_* variables.

G_BANNERS="BANNERFILE ENABLEIMAGES BANNERTRAY"   # Control: ENABLEBANNERS
G_IMAGES="LPSIMAGES IMAGESUFFIX"		 # Control: ENABLEIMAGES
G_LOGGING="LOGFILE LOGLEVEL DEBUGLEVEL"		 # Control: ENABLELOG
G_ACCTING="ACCTFILE ACCTUNIT"			 # Control: ENABLEACCT

GROUPS="G_BANNERS G_IMAGES G_LOGGING G_ACCTING"

# Before launching into the configuration loop, make sure that this
# Print Client has an assigned PrintServer printer (PS) object.
# If it doesn't, then have the user assign one now, before we have
# the user choose the configuration technique.

if [ -z "$PC_PSNAME" ]
then
    # If the Get the name of the PrintServer printer object to be assigned
    # to this new Print Client.

    if setPSNAME PC $OSTYPE $PCTYPE "" $PCNAME $TMPFILE
    then
	PC_PSNAME="`cat $TMPFILE`"
	rm -f $TMPFILE
    else
	exit 1
    fi
    PC_DESCRIPTION="$PC_DESCRIPTION for Print Server $PC_PSNAME"
fi

# Fetch the PS object's "MODEL" attribute for use in passing to any
# one of the setXXXX subscripts that may be subsequently invoked.

if PSMODEL=`getPSmodel $PC_PSNAME`
then
    exit 1
fi

CONTINUE=true
if [ $MODE = add ]
then
    # Allow the user to bail out at this point if default values are OK

    showhelp true "
At this point, all configuration information required by the Print
Client has been entered.  Default values for the remainder of the
configuration variables have been provided for you.

You can now choose to accept these default values, or you can go on
and supply specific configuration attributes for this Print Client,
such as ANSI translation, log file, logging and debugging levels.
"
    if yesno y "Do you want to examine the current configuration"
    then
	# This may take a bit of time, so encourage the user to be patient...

	echo
	echo "One moment please..."

	showobjvars PC $PCNAME $VARLIST | $PAGER
    fi

    if yesno x "Accept defaults for the remaining configuration questions"
    then
	CONTINUE=false   # Great, then we're all set
    else
       :
    fi
fi

# 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 [ $CONTINUE = true ]
then
if [ $MODE = add ]
then
    firstadd=true
    defmode="step-by-step"
else
    firstadd=false
    defmode="menu-oriented"
fi

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

CONFIGERROR="FAILURE to properly set a Print Client configuration variable!!!"

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

    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 $VARLIST`
	do
	    # First time add operation?  Don't ask for required vars.

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

	    # See if this var should be skipped due to the disabling
	    # of a related control var.

	    for group in $GROUPS
	    do
		eval grouplist="\$$group"
		if expr "$grouplist" : ".*$var" > /dev/null 2>&1
		then
		    case $group in
			G_BANNERS ) if boolval $PC_ENABLEBANNERS
				    then break ; else continue 2; fi ;;
			G_IMAGES  ) if boolval $PC_ENABLEBANNERS &&
				       boolval $PC_ENABLEIMAGES
				    then break ; else continue 2; fi ;;
			G_LOGGING ) if boolval $PC_ENABLELOG
				    then break ; else continue 2; fi ;;
			G_ACCTING ) if boolval $PC_ENABLEACCT
				    then break ; else continue 2; fi ;;
		    esac
		fi
	    done

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

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

	    if $script PC $OSTYPE $PCTYPE "$PSMODEL" "$val" $TMPFILE
	    then
		val="`cat $TMPFILE`"

		# If the PrintServer printer association was just changed,
		# fetch the model type and reset the banner input tray to
		# default iff the current tray value has now become invalid.
		# tray due to conflicts in tray name and model type.

		if [ $var = PSNAME -a "$val" != "$PC_PSNAME" ]
		then
		    if PSMODEL=`getPSmodel "$val"`
		    then
			exit 1	# Error!
		    else
			if validPStray input "$MC_BANNERTRAY" "$val"
			then
			    :   # OK, the new model has the same tray name
			else
			    MC_BANNERTRAY="default"
			fi
		    fi
		fi
	    else
		echo
		echo $CONFIGERROR
		exit 1
	    fi

	    var="PC_$var"
	    eval $var="\"$val\""
	done

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

	expobjvars PC
	showobjvars PC $PCNAME $VARLIST

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

	while true
	do
	    if objvarmenu PC $PCNAME $VARLIST $TMPFILE
	    then
		break   # Break out of inner loop
	    else
		var="`cat $TMPFILE`"
		eval val="\"\$PC_$var\""

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

		if $script PC $OSTYPE $PCTYPE "$PSMODEL" "$val" $TMPFILE
		then
		    val="`cat $TMPFILE`"

		    # If the PrintServer printer association was just set,
		    # determine if the user must change the banner input
		    # tray due to conflicts in tray name and model type.

		    if [ $var = PSNAME -a "$val" != "$PC_PSNAME" ]
		    then
			if PSMODEL=`getPSmodel "$val"`
			then
			    exit 1	# Error!
			fi

			if validPStray input "$PC_BANNERTRAY" "$val"
			then
			    :   # OK, the new model has the same tray name
			else
			    cat << END_OF_INPUT

    By changing the assigned PrintServer printer for Print Client ${PCNAME}
    (from "$PC_PSNAME" to "$val"), the current assignment for your banner
    input tray ("$PC_BANNERTRAY") has now become invalid.  The printer you
    have just assigned is a PrintServer model ${PSMODEL}, and that model
    does not have a "$PC_BANNERTRAY" input tray, or at least an input tray
    with that designation.  You must now select a new input banner tray
    for this Print Client for use with this new printer.

END_OF_INPUT
			    pause "Press RETURN to assign the input tray... "

			    if setBANNERTRAY PC $OSTYPE $PCTYPE "$PSMODEL" "" $TMPFILE
			    then
				PC_BANNERTRAY="`cat $TMPFILE`"  # Thank you!
			    else
				echo $CONFIGERROR
				exit 1
			    fi
			fi
		    fi

		    eval var="PC_$var"
		    eval $var="\"$val\""
		else
		    echo
		    echo $CONFIGERROR
		    exit 1
		fi
	    fi
	done
    fi

    firstadd=false

    if yesno x "Are you finished configuring Print Client $PCNAME"
    then
	break   # Break out of outer loop
    fi
done
fi

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

touch $OUTFILE

for attr in `cat $ATTRLIST`
do
    var="PC_$attr"
    eval val="\"\$$var\""

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

done

rm -f $TMPFILE $TMPDIR/*.$$

exit 0
