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

TMPFILE="$TMPDIR/mcvars.$$"

# 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 MC

# Assignment of the important "configuration variable groups".  A variable
# is the list of *base* attribute names for all variables interrelated in
# some fashion (eg: logging, accounting, etc).  If a new object variable is
# 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 logging group (G_LOGGING), is dependent on the control
# variable OFFERELOG.  If this variable is set, then variables in this
# group should be used in the step-by-step technique.
#
# The GROUPS variable should always contain the list of G_* variables.

G_ACCTING="ACCTFILE"			  # Control: OFFERACCT
G_LOGGING="LOGFILE LOGLEVEL DEBUGLEVEL"   # Control: OFFERELOG

GROUPS="G_ACCTING G_LOGGING"

# Before launching into the configuration loop, make sure that this
# Management 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 "$MC_PSNAME" ]
then
    # If the Get the name of the PrintServer printer object to be assigned
    # to this new Management Client.

    if setPSNAME MC $OSTYPE $PCTYPE "" $MCNAME $TMPFILE
    then
	MC_PSNAME="`cat $TMPFILE`"
	rm -f $TMPFILE
    else
	exit 1
    fi
    MC_DESCRIPTION="$MC_DESCRIPTION for Print Server $MC_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 $MC_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 Management
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 Management Client,
such as log and accounting files, 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 MC $MCNAME $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 Management Client configuration variable!!!"

while true
do
    list="step-by-step menu-oriented"
    quest="How do you want to configure the Management 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

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

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

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

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

		# If the PrintServer printer association was just changed,
		# then fetch the model type.

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

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

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

	expobjvars MC
	showobjvars MC $MCNAME $VARLIST

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

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

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

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

		    # If the PrintServer printer association was changed,
		    # fetch the model type.

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

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

    firstadd=false

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

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

touch $OUTFILE

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

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

done

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

exit 0
