#ident	"@(#)nlp.admin:menu/packagemgmt/lpmgmt/printers/add_p	1.7"
## IDOC:   SCREEN 2.24: ADD PRINTER
#menu# Add a new printer

##+++ (help _H46)
#help# You use "add" menu to add a new printer to the print service.
#help# Minimally you'll need to know:
#help#
#help# * which  interface  program should be  used to print files on the printer
#help#   (the default is a standard program supplied with the LP Print Service).
#help#
#help# * which port the  printer  is  connected  to  OR  how  the  printer is to
#help#   be  reached  using the Basic Networking Utilities.
#help#
#help# Better service will be provided to you and the users if you also specify:
#help#
#help# * the type of printer (as listed in the Terminfo database--see the System
#help#   Administrator's Guide for help on this);
#help#
#help# * the default page size and print spacing that  should  be in effect;
#help#
#help# * how you are to be alerted when a problem occurs with the printer;
#help#
#help# * what print wheels, font cartridges,  or  character  sets are  available
#help#   on the printer;
#help#
#help# * what forms can be used on the printer;
##--- (help _H46)

##+++ (general)
echo "
Whenever you want to quit, type \"q\".
If you are not sure how to answer any prompt, type \"?\" for help,
or see the System Administrator's Guide.

If a default appears in the question, press the return key to use it.
"
##--- (general)

trap 'exit 0' 1 2 15
flags="-qq -k$$"

##+++ (functions)
chk_name () {
	RESP=`checkre ${flags} -fe $4 \
		${2:+"-D$2"} -H "$3" \
		"$1${SyStSt:+!name!}" \
		'^[a-zA-Z0-9_]\{1,14\}$' \
			'A name can contain only 1 to 14 letters, digits, and underscores.' \
	`
}
##--- (functions)

##+++ (line 1)
echo \
"When entering more than one answer or value,
separate them with a space or comma.
"
##--- (line 3)

OPT_p=
OPT_onobanner=
OPT_cpi=
OPT_c=
OPT_S=none
OPT_I=simple
OPT_v=
OPT_U=
OPT_F=continue
OPT_i=
OPT_e=
OPT_m=standard
OPT_lpi=
OPT_length=
OPT_width=
OPT_l=
OPT_T=unknown
OPT_D=
OPT_uallow=all
OPT_udeny=none
OPT_fallow=none
OPT_fdeny=all
OPT_W=0
OPT_A="mail lp"
OPT_ostty=

DEF_p=${OPT_p}
##+++ (2.24.4)
while true
do
chk_name \
	"Enter the name of the new printer:${DEF_p:+ (default: ${DEF_p})}" \
	"${DEF_p}" \
	"\
        You are expected to enter the name of  the  printer.   Names
        must  be  made  up  of letters, digits, and underscores, and
        must be no more than 14 characters long.  \
"
OPT_p=${RESP}

	if [ "${OPT_p}" = "any" -o "${OPT_p}" = "all" -o "${OPT_p}" = "none" ]
	then
		echo "You can not use \"any\", \"all\", or \"none\". Try again."
		continue
	fi

	if ls -d /usr/spool/lp/admins/lp/printers/${OPT_p} 2>/dev/null 1>&2
	then
		echo "Printer ${OPT_p} already exists; use the \"change\" command in the \"printers\"
menu to change a printer. Try again."
	else
		break
	fi
done
##--- (2.24.4)

##+++ (line 5)
echo \
""
##--- (line 5)

if ls -d /usr/spool/lp/admins/lp/printers/* 2>/dev/null 1>&2
then

##+++ (2.24.6)
if checkyn ${flags} -f \
	${DEF:+"-D${DEF}"} -H \
	"\
        Enter \"y\" (for yes) if this printer is  largely  similar  to
        another  printer.  The LP Print Service will ask you for the
        name of that printer and will look up it's configuration and
        use  it for default values in the remaining questions. Enter
        \"n\" (for no) if this printer is sufficiently different  that
        it  wouldn't help to use another printer's configuration for
        defaults, or  if  you  don't  know  what  printer  might  be
        similar.  \
" \
	"Is this printer to be similar to one already added?${DEF:+ (default: ${DEF})}"
then
##--- (2.24.6)

	DEF=`cat ${TMPDIR:-/tmp}/PRT.\`basename \\\`tty\\\`\` 2>/dev/null`
	while true
	do
##+++ (2.24.7)
chk_name \
	"Enter the name of the similar printer:${DEF:+ (default: ${DEF})}" \
	"${DEF}" \
	"\
        You are expected to enter the name of a printer that already
        has been added.  The printer's configuration will be used as
        a guide for answering the rest of the questions.  Where  the
        existing  printer's  configuration  is  suitable for the new
        printer you are adding, you  will  be  able  to  just  press
        return to skip through the questions quickly.  \
"
OPT=${RESP}
##--- (2.24.7)

		pfile=/usr/spool/lp/admins/lp/printers/${OPT}/configuration
		if [ -r "${pfile}" ]
		then
			break
		else
			echo "That printer doesn't exist. Try again."
		fi
	done

	OPT_onobanner=`grep 'Banner:.*Always' ${pfile} >/dev/null || echo yes`
	OPT_cpi=`sed -n -e '/CPI: /s///p' ${pfile}`
### 	OPT_c=`egrep "^${OPT}$" /usr/spool/lp/admins/lp/classes/* 2>/dev/null | cut -f8 -d\/ | cut -f1 -d:`
	OPT_ostty=`sed -n -e '/Stty: /s///p' ${pfile}`
	OPT_speed=`sed -n -e '/Speed: /s///p' ${pfile}`
	if [ -n "${OPT_ostty}" -o -n "${OPT_speed}" ]
	then
		OPT_ostty="${OPT_ostty} ${OPT_speed}"
	fi
	if grep \(daisy\) ${pfile} 1>/dev/null
	then
		OPT_S=`sed -n -e '/Character sets: (daisy)  /s///p' ${pfile}`
	else
		OPT_S=`sed -n -e '/Character sets: /s///p' ${pfile}`
	fi
        alert=`/usr/lib/lpadmin -p ${OPT} -A list`
	case "${alert}" in
	"On fault: al"* )
		OPT_Q=0
		OPT_W=`expr "${alert}" : '.*every \(.*\) minute*'`
		if [ -n "${OPT_W}" ]
		then
			:
		else
			OPT_W=0
		fi
		OPT_A=`expr "${alert}" : '.*with "\(.*\)" *'`
		;;
	"On fault: ma"* )
		OPT_Q=0
		OPT_W=`expr "${alert}" : '.*every \(.*\) minute*'`
		if [ -n "${OPT_W}" ]
		then
			:
		else
			OPT_W=0
		fi
		OPT_A=`expr "${alert}" : '.*to \(.*\) *'`
		OPT_A=`echo ${OPT_A} | cut -f1 -d" " `
		OPT_A="mail "${OPT_A}
		;;
	"On fault: wr"* )
		OPT_Q=0
		OPT_W=`expr "${alert}" : '.*every \(.*\) minute*'`
		if [ -n "${OPT_W}" ]
		then
			:
		else
			OPT_W=0
		fi
		OPT_A=`expr "${alert}" : '.*to \(.*\) *'`
		OPT_A=`echo ${OPT_A} | cut -f1 -d" " `
		OPT_A="write "${OPT_A}
		;;
	"On fault: no"* )
		OPT_Q=0
		OPT_W=0
		OPT_A=none
		;;
	esac
	OPT_I=`sed -n -e '/Content types: /s///p' ${pfile}`
	: ${OPT_I:=simple}
	OPT_v=`sed -n -e '/Device: /s///p' ${pfile}`
	OPT_U=`sed -n -e '/Dial: /s///p' ${pfile}`
	OPT_F=`sed -n -e '/Fault: /s///p' ${pfile}`
	: ${OPT_F:=continue}
	OPT_i=`sed -n -e '/Interface: /s///p' ${pfile}`
	if expr "${OPT_i}" : "/usr/spool/lp/model" >/dev/null
	then
		OPT_m=`expr "${OPT_i}" : '/usr/spool/lp/model/\(.*\)'`
		OPT_i=
		OPT_e=
	else
		OPT_e=
		OPT_m=
	fi
	OPT_lpi=`sed -n -e '/LPI: /s///p' ${pfile}`
	OPT_length=`sed -n -e '/Length: /s///p' ${pfile}`
	OPT_width=`sed -n -e '/Width: /s///p' ${pfile}`
	OPT_l=`grep "Login:" ${pfile} >/dev/null && echo "yes"`
	OPT_T=`sed -n -e '/Printer type: /s///p' ${pfile}`
	OPT_D=`cat /usr/spool/lp/admins/lp/printers/${OPT}/comment 2>/dev/null`
	allow=/usr/spool/lp/admins/lp/printers/${OPT}/users.allow
	deny=/usr/spool/lp/admins/lp/printers/${OPT}/users.deny
	if [ -s "${allow}" ]
	then
		OPT_uallow=`sort <${allow} | tr '\012' ','`
		OPT_uallow=`expr "${OPT_uallow}" : '\(.*\),$'`
		OPT_udeny=
	elif [ -f "${allow}" ]
	then
		OPT_uallow=none
		OPT_udeny=all
	elif [ -s "${deny}" ]
	then
		OPT_uallow=
		OPT_udeny=`sort <${deny} | tr '\012' ','`
		OPT_udeny=`expr "${OPT_udeny}" : '\(.*\),$'`
	elif [ -f "${deny}" ]
	then
		OPT_uallow=all
		OPT_udeny=none
	else
		OPT_uallow=none
		OPT_udeny=all
	fi
	allow=/usr/spool/lp/admins/lp/printers/${OPT}/forms.allow
	deny=/usr/spool/lp/admins/lp/printers/${OPT}/forms.deny
	if [ -s "${allow}" ]
	then
		OPT_fallow=`sort <${allow} | tr '\012' ','`
		OPT_fallow=`expr "${OPT_fallow}" : '\(.*\),$'`
		OPT_fdeny=
	elif [ -f "${allow}" ]
	then
		OPT_fallow=none
		OPT_fdeny=all
	elif [ -s "${deny}" ]
	then
		OPT_fallow=
		OPT_fdeny=`sort <${deny} | tr '\012' ','`
		OPT_fdeny=`expr "${OPT_fdeny}" : '\(.*\),$'`
	elif [ -f "${deny}" ]
	then
		OPT_fallow=all
		OPT_fdeny=none
	else
		OPT_fallow=none
		OPT_fdeny=all
	fi
	alert=`/usr/lib/lpadmin -p ${OPT} -A list`
	case "${alert}" in
	When* )
		OPT_Q=`expr "${alert}" : 'When \(.*\) are queued:'`
		OPT_W=`expr "${alert}" : '.*every \(.*\) minute:'`
		alert=`expr "${alert}" : '.*queued: \(.*\) every'`
		case "${alert}" in
		alert* )
			OPT_A=`expr "${alert}" : '.*"\(.*\)".*'`
			;;
		write* )
			OPT_A=write
			;;
		mail* )
			OPT_A=mail
			;;
		esac
		;;
	No* )
		OPT_Q=0
		OPT_W=0
		OPT_A=none
		;;
	esac
fi
fi


echo
OPT_install=edit
firsttime=yes
while [ "edit" = "${OPT_install}" ]
do

	DEF_onobanner=${OPT_onobanner}
	DEF_cpi=${OPT_cpi}
	DEF_S=${OPT_S}
	DEF_I=${OPT_I}
	DEF_v=${OPT_v}
	DEF_U=${OPT_U}
	DEF_F=${OPT_F}
	DEF_i=${OPT_i}${OPT_e}${OPT_m}
	DEF_lpi=${OPT_lpi}
	DEF_length=${OPT_length}
	DEF_width=${OPT_width}
	DEF_l=${OPT_l}
	DEF_T=${OPT_T}
	DEF_D=${OPT_D}
	DEF_uallow=`echo ${OPT_uallow} | tr ' ' ','`
	DEF_udeny=`echo ${OPT_udeny} | tr ' ' ','`
	DEF_fallow=`echo ${OPT_fallow} | tr ' ' ','`
	DEF_fdeny=`echo ${OPT_fdeny} | tr ' ' ','`
	DEF_W=${OPT_W}
	DEF_A=${OPT_A}
	DEF_c=${OPT_c}
	DEF_ostty=${OPT_ostty}

	if [ "yes" != "${firsttime}" ]
	then
		echo "
Edit the printer configuration:
"
	fi
	firsttime=

. ${MENUTOP}/menu/packagemgmt/lpmgmt/_S2.25

	echo

	if [ "skip" = "${OPT_install}" ]
	then
		exit 0
	fi

	if [ "install" = "${OPT_install}" ]
	then
		OPT_install="edit"

		easyopts="
			${OPT_c:+-c ${OPT_c}}
			${OPT_T:+-T ${OPT_T}}
			${OPT_v:+-v ${OPT_v}}
			${OPT_l:+-l}
			${OPT_U:+-U ${OPT_U}}
			${OPT_i:+-i ${OPT_i}}
			${OPT_e:+-e ${OPT_e}}
			${OPT_m:+-m ${OPT_m}}
			${OPT_onobanner:+-o nobanner}
			${OPT_cpi:+-o cpi=${OPT_cpi}}
			${OPT_lpi:+-o lpi=${OPT_lpi}}
			${OPT_length:+-o length=${OPT_length}}
			${OPT_width:+-o width=${OPT_width}}
		"
		/usr/lib/lpadmin -p ${OPT_p} ${easyopts} 2>${TMPDIR:-/tmp}/warnxxx || { cat ${TMPDIR:-/tmp}/warnxxx; continue; }

		echo ${OPT_p} >${TMPDIR:-/tmp}/PRT.`basename \`tty\``

		if [ "none" = "${OPT_D}" ]
		then
			/usr/lib/lpadmin -p ${OPT_p} -D "" || continue
		else
			/usr/lib/lpadmin -p ${OPT_p} -D "${OPT_D}" || continue
		fi
		if [ -n "${OPT_uallow}" ]
		then
			/usr/lib/lpadmin -p ${OPT_p} -u allow:"${OPT_uallow}" || continue
		fi
		if [ -n "${OPT_udeny}" ]
		then
			/usr/lib/lpadmin -p ${OPT_p} -u deny:"${OPT_udeny}" || continue
		fi
		if [ -n "${OPT_fallow}" ]
		then
			/usr/lib/lpadmin -p ${OPT_p} -f allow:"${OPT_fallow}" || continue
		fi
		if [ -n "${OPT_fdeny}" ]
		then
			/usr/lib/lpadmin -p ${OPT_p} -f deny:"${OPT_fdeny}" || continue
		fi
		if [ -n "${OPT_A}" -a "none" != "${OPT_A}" ]
		then
			/usr/lib/lpadmin -p ${OPT_p} -A "${OPT_A}" -W ${OPT_W} -F ${OPT_F} || continue
		fi
		if [ -n "${OPT_I}" ]
		then
			/usr/lib/lpadmin -p ${OPT_p} -I "${OPT_I}" || continue
		fi
		if [ -n "${OPT_ostty}" ]
		then
			/usr/lib/lpadmin -p ${OPT_p} -o stty="'${OPT_ostty}'" || continue
		fi
		if [ -n "${OPT_S}" ]
		then
			/usr/lib/lpadmin -p ${OPT_p} -S "${OPT_S}" || continue
		fi

		rm ${TMPDIR:-/tmp}/warnxxx 2>/dev/null
		break
	fi

done
