#
# proxmap.sh:
# Map $ptype and $DEPT into specific lp options
# ("Sourced" by llp, getptr and gethead)
#
# Uses tables: paper.map, proximity.map, master.map
# Uses $0 to show name of invoking script in error messages.
# Returns: lp option string in $lpopts.
#

if tmp=`grep ":$ptype:" $PTRDIR/paper.map`
then
	ptype=`echo $tmp | cut -d, -f1`
else
	echo "$0: Error: bad $WHAT type \"$ptype\""
	exit 1
fi

if echo $tmp | grep "^$ptype,.*$CODECHAR[A-Z]*$" >/dev/null
then
	:
else
	echo "$0: $WHAT type \"$ptype\" is not accepted by this command."
	exit 1
fi

if tmp=`grep "^$ptype,.*[^a-zA-Z0-9]$DEPT," $PTRDIR/proximity.map`
then
	ptr=`echo $tmp | cut -f3 -d, | tr -d "\011 "`
else
	echo "$0 error: undefined mapping of Dept=$DEPT, ptype=$ptype" >&2
	exit 1
fi

[ $DEBUG = Y ] && echo "Department description = $DEPTDESC"
[ $DEBUG = Y ] && echo "ptr = \"$ptr\""

#
# Now, map $ptr and paper type into appropriate lp command text in $lpopts:
#

if tmp=`grep "^$ptr,.*[^a-zA-Z0-9]$ptype," $PTRDIR/master.map`
then
	lpopts="-d`echo $tmp | cut -f2 -d, | tr -d '\011 '`"
	field=4
	[ "`echo $tmp | cut -d, -f5 | tr -d '\011 '`" = "$ptype" ] && field=6
	lpopts="$lpopts `echo $tmp | cut -d, -f$field | tr -d '\011 '`"
else
	echo "$0: Error: bad paper spec (ptr = $ptr, ptype = $ptype)" >&2
	exit 1
fi

[ $DEBUG = Y ] && echo "lpopts = \"$lpopts\"" 


