#!/bin/sh
#	SID	"@(#)lplist.sh	3.3 - 95/04/11"

progName=`basename $0`
errFile=/tmp/lpl$$
touch $errFile

usage() {
	cat >&$1 <<_END_USAGE_
$progName [-x] [-s]
_END_USAGE_
}

showexpl() {
	usage
	cat <<_END_EXPL_

list printers available on a system

-x		display this explanation
-s		silent - don't show errors

Where /usr/bin/lpstat or /bin/lpstat is available, that is used.
Otherwise, /etc/printcap is checked for printers.
_END_EXPL_
}

silentOpt=No

while [ $# -ge 1 ] ; do
	case $1 in
	-x)		showexpl; exit;;
	-s)		silentOpt=Yes;;
	*) 	echo "Invalid flag ($1)" >&2
		usage 2
		exit 1;;
	esac
	shift
done

if [ -f /usr/bin/lsallq ]; then
	# AIX can do it for us
	lsallq | sort | uniq
elif [ -f /usr/bin/smit ]; then
	# AIX strangeness
	lpstat -v |tail +3|cut -c1-7|grep -v ':'|grep '[a-zA-Z]'|sort|uniq
elif [ -f /usr/bin/lpstat -o -f /bin/lpstat ]; then
	# ATT lp spooler
	lpstat -v | sed -n -e "s/...... for \([^ :]*\).*$/\1/p" 2>$errFile
elif [ -f /etc/printcap ]; then
	# BSD lpd spool
	sed -n -e '/^[ \t]*#/d; /^[ \t]*$/d; s/^\([^|:]*\).*$/\1/p' \
		</etc/printcap | grep '[a-zA-Z]' 2>$errFile
else
	echo ${progName}: Cannot determine spooler type >$errFile
fi

if [ $silentOpt = No ]; then
	cat $errFile >&2
fi
rm -f $errFile
