#! /bin/sh
#
# @(#)setDNETADDR	1.6	LPS_UNX_COM	2/19/95
#
# Copyright 1994   Digital Equipment Corporation, Maynard, MA
#
# setDNETADDR
#
# A script that produces a value for attribute DNETADDR.
#
# Parameters:
#    $1 - LPS class identifier, one of {PC, MC, PS, ENV}
#    $2 - Operating system type identifier (eg: SV2, BSD, etc)
#    $3 - Print client type identifier (eg: AIX, BSD, SV3, etc)
#    $4 - PrintServer printer model (eg: LPS17, LPS20)
#    $5 - Default value for the attribute
#    $6 - Path of the output file to receive the final attribute value
#
# Global variables:
#    ECHON
#    FUNCS
#    PAGER
#    TR
#
# Exit values:
#    0 - Success, output file contains newly set attribute value.
#    1 - Error or interruption occurred, error messages go to stderr,
#	 contents of the output file are undefined.
###

CLASS=$1
OSTYPE=$2
PCTYPE=$3
PSMODEL=$4
DEFVAL=$5
OUTFILE=$6

. $FUNCS   # Import the standard LPS shell functions

showhelp true "
What is the DECnet address of the printer?

If you want the printer to provide access by DECnet nodes, then
the printer must be told of its DECnet address when the printer
is loaded by a Management Client running on your system.

The format used to specify an DECnet address is:  a.n

That is, two integer numbers separated by a period.  The first number
is the \"area\" address, which can be a value between 0 and 63.  The
second number is the \"node\" number, which can be a value between
1 and 1023.

For example, a valid DECnet address is:  27.856

Your network administrator should be able to help you determine the
proper DECnet address for your printer.

If DECnet is not running on your local area network, or you do not
wish to allow access by DECnet nodes on your local area network,
then simply press RETURN when prompted for the DECnet address.

For more information about DECnet addresses, please refer to the
Management Guide.
"

testaddr ()
{
    if [ -z "`echo $RESPONSE | $TR -d '0123456789.'`" ] # just digits & periods
    then
	if [ "." = "`echo $RESPONSE | $TR -d '0123456789'`" ] # just one period
	then
	    if [ "`echo $RESPONSE | cut -c1`" != "." ] # no leading period
	    then
		cnt=`expr "$RESPONSE" : '.*'`

		if [ "`echo $RESPONSE | cut -c$cnt`" != "." ] # no trailing .
		then
		    area=`echo $RESPONSE | cut -d. -f1`
		    node=`echo $RESPONSE | cut -d. -f2`

		    if [ $area -ge 0 -a $area -le 63 ] # valid area number
		    then
			if [ $node -ge 1 -a $node -le 1023 ] # valid node num
			then
			    return 0    # whew!
			fi
		    fi
		fi
	    fi
	fi
    fi

    return 1
}

# See if the default value is usable or not, then set the question
# text based on the results.

RESPONSE="$DEFVAL"

if testaddr
then
    QUESTION="DECnet address [${RESPONSE}]: "
else
    QUESTION="DECnet address: "
    DEFVAL=""
fi

while true
do
    echo
    $ECHON "$QUESTION"
    read RESPONSE

    # If the user entered a null response (ie, just pressed RETURN),
    # then if there is no default value, simply write out a null value
    # to the output file and be done with it.
    #
    # Otherwise, if a null response was entered, but there *is* a default
    # value, then ask the user if this variable should be "blanked out",
    # or whether the default value should indeed be used.

    if [ -z "$RESPONSE" ]
    then
	if [ -z "$DEFVAL" ]
	then
	    break	# Leave this value NULL, then
	else
	    if yesno y "Do you wish to retain the current value"
	    then
	    	RESPONSE="$DEFVAL"
		break	# Break out and write out the default value
	    fi

	    if yesno y "Do you want to leave this value blank"
	    then
		break	# Break out and write out a NULL value
	    fi
	fi

    else	# User entered some sort of value

	if testaddr
	then
	    break	# Break out and write out this entered value
	fi

	cat << END_OF_INPUT

    Sorry, but "$RESPONSE" is an invalid DECnet address.

    A valid DECnet address consists two integer numbers separated by a
    period.  The first number (the area address) must be between 0 and 63,
    and the second number (the node number) must be between 1 and 1023.
    Do not use space or tab characters at any time to enter this address.

    An example of a valid DECnet address is:  4.67
END_OF_INPUT

    fi

done

# Come here only when we've broken out of the while loop above

echo "$RESPONSE" > $OUTFILE

exit 0
