#! /bin/sh
#
# @(#)setMAILADMADDR	1.4	LPS_UNX_COM	04/01/95
#
# Copyright 1995   Digital Equipment Corporation, Maynard, MA
#
# setMAILADMADDRS
#
# A script that produces a value for attribute MAILADMADDRS.
#
# 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
#    HELP
#    PAGER
#    TMPDIR
#
# 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

if [ "$CLASS" = "ENV" ]
then
   NOTES="
By default, the account name associated with the user running this
script is assigned to receive such failed mail messages."
fi

showhelp true "
Which account should receive error-related mail messages?

Under certain circumstances, an LPS component (a program or a script)
will need to send an electronic mail message to an account associated
with the management of the PrintServer system software.

For example, occasionally a mail message can not be sent to the user
associated with a problematic print request, in which case the mail
message is sent to this account.

You are now being asked to specify the name of this user account.

If you enter a local account name, then every attempt will be made to
verify that the specified account does indeed exist on this system.
If your system currently uses Network Information Services (NIS)
(formerly called \"Yellow Pages,\" abbreviated as \"YP\"), then an
attempt to validate the account within that service will also be
performed.

                             NOTE
     If this system uses a nonstandard form of NIS, or if the
     NIS system programs exist in nonstandard directories, then
     the account validation mechanism may not work correctly.
$NOTES
"

# First check to see if NIS/YP is being used on this system

ypused=false

if [ -x /usr/bin/ypwhich -a -x /usr/bin/ypcat ]
then
    if /usr/bin/ypwhich > /dev/null 2>&1
    then
	ypused=true
	showhelp false "NIS exists and is in use on this system."
    else
	showhelp false "NIS exists, but does not appear to be in use."
    fi
else
    showhelp false "NIS does not appear to exist on this system."
fi

# Get the local account name and validate (even if the default is chosen)

while true
do
    echo
    $ECHON "Account to receive LPS mail messages [$DEFVAL]? "
    read resp
    if [ -z "$resp" ]
    then
	resp=$DEFVAL
    fi

    tfile=$TMPDIR/acctnam.$$
    rm -f $tfile

    if egrep "^${resp}:" /etc/passwd > $tfile
    then
	:
    elif $ypused
    then
	/usr/bin/ypcat passwd | egrep "^${resp}:" > $tfile
    fi

    # Here $tfile should contain the entry(s) for $resp

    case `grep -c ":" $tfile` in
	0 )  echo
	     echo "Sorry, but \"$resp\" does not appear to be a user on this system." 
             echo
             if yesno y "Do you still want to use \"$resp\" "
             then
		RESPONSE="$resp"
                break
             fi
	     ;;

	1 )  RESPONSE=`cut -d: -f1 $tfile`
	     if $HELP
	     then
	         name=`cut -d: -f5 $tfile`
		 echo
		 echo "    [Account \"$RESPONSE\" ($name) is valid]"
		 sleep 2
	     fi
	     break
	     ;;

	* )  echo "Sorry, there are multiple account entries for \"$resp\"." ;;
    esac

    echo "Please enter a different account name."
done

rm -f $tfile

echo "$RESPONSE" > $OUTFILE

exit 0
