#
# getptr/gethead:
#	Prompt for printer/letterhead name, with optional default
#	Written by Leor Zolman, 9/91
#
# Usage:
#	getptr [default-printer]
#	gethead [default-letterhead]
#	Returns corresponding "lp" command line args on standard output.
#

PTRDIR=/u/leor/docs/Cols/SA/Printers			# Printer control directory

case $0 in
  getptr)	CODECHAR=G							# for restricting selections
			WHAT="Printer";;
  gethead)	CODECHAR=H
			WHAT="Letterhead";;
esac

[ $# -gt 1 ] && echo "Usage: $0 [default-$WHAT]" >&2 && exit

. $PTRDIR/initmap.sh		# initialize and handle special-case mappings

# choose printer area:

saveptype=$ptype
while true
do
	echo "\n" >&2
	header="----------- ${WHAT}s ($DEPTDESC Area) -----------"
	echo $header >&2
	sed -n 's/^[^,]*, *\([^,]*\),[^:]*:\([^:]*\):.*'\
$CODECHAR'[A-Z]*$/\1 (\2)/p' paper.map
	echo "$header" | tr '[ -z]' '[-*]' >&2
	echo "Choose a $WHAT, 'A' to change area, \c" >&2
	echo "Return for default [$ptype] ?\c">&2
	read code
	[ "$code" != "" ] && ptype=$code

	if [ "$ptype" = a -o "$ptype" = A ]; then
	 while true
	 do
		echo "\n   DEPARTMENTAL AREAS:" >&2
		sed -n 's/^.*"\(.\)\(.*\)".*$/\1\2  (\1)/p' dept.map
		while true
		do
			echo "\nSelect an area (by single-letter code): \c">&2
			read code
			code=`echo "$code\c" | tr '[a-z]' '[A-Z]'`
 			if tmp=`grep $code'$' dept.map`
			then
				DEPT=`echo $tmp | cut -d, -f2 | tr -d "\011 "`
				break
			else
				echo "Please choose from the displayed list only!" >&2
			fi
		done								
		[ $DEBUG = Y ] && echo "after dept loop, DEPT = $DEPT" && exit
		ptype=$saveptype
		. $PTRDIR/initmap.sh
		break
	 done
	 continue
	else
		if grep ":$ptype:" $PTRDIR/paper.map >/dev/null
		then
			break
		else
			echo "\nPlease choose from the listed options only." >&2
			ptype=$saveptype
		fi
	fi
done

#
# Map $ptype and $DEPT into appropriate lp text in $lpopts:
#

. $PTRDIR/proxmap.sh

echo "$lpopts"
exit 0
