#!/bin/sh
#
# Copyright (c) 1994, 1995, 1996 by Sun Microsystems, Inc.
# All Rights Reserved
#
#	@(#)lpadmin	1.33	99/08/06 SMI
#
PATH=/bin:/usr/bin:/usr/sbin export PATH

TEXTDOMAIN="SUNW_OST_OSCMD"
export TEXTDOMAIN

LPSET=/usr/bin/lpset
LPGET=/usr/bin/lpget

HOST=`/bin/uname -n`
PID=$$

cmd_name=`basename $0`
exit_code=0

usage() {
	gettext "Usage:\n" 1>&2
	gettext "	lpadmin -p (printer) (options)\n" 1>&2
	gettext "	lpadmin -x (dest)\n" 1>&2
	gettext "	lpadmin -d (dest)\n" 1>&2
	gettext "	lpadmin -S print-wheel -A alert-type [ -W minutes ]\n" 1>&2
	gettext "		[ -Q requests ]\n" 1>&2
	gettext "	lpadmin -M -f form-name [ -a [ -o filebreak ]\n" 1>&2
	gettext "		[ -t tray-number ]]\n" 1>&2
	exit 1
}

# 
# Delete entries in /etc/printers.conf for local printers/classes that no longer
# exist in the /etc/lp database
#

delete_local() {

# Get printer names for each local printer
# grep /etc/printers.conf for each bsdaddr for this server
# get printer name from that line

for LINE in `grep bsdaddr=${HOST}, /etc/printers.conf`
do
        PRINTER=`echo ${LINE} | sed -e 's/^:bsdaddr='$HOST',//' -e 's/[,:].*//'`

# If there is not an entry for this printer in
#       /etc/lp/printers or /etc/lp/classes
# Then delete the entry for this printer in /etc/printers.conf

        if [ ! -d /etc/lp/printers/${PRINTER} -a ! -f /etc/lp/classes/${PRINTER} ] ;
        then
                logger -p lpr.debug -t "lpadmin[${PID}]" \
                         "Removing $PRINTER entry from /etc/printers.conf"
                ${LPSET} -x ${PRINTER}
                status=$?

                if [ ${status} -ne 0 ] ; then
                        gettext "Warning: error removing ${PRINTER} " 1>&2
			gettext "entry from /etc/printers.conf\n" 1>&2
                        logger -p lpr.debug -t "lpadmin[${PID}]" \
				"Call to lpset -x $PRINTER exits with ${status}"
			exit_code=1
                fi
        fi
done
}

delete_entries() {
if [ ! -f /etc/printers.conf ] ; then
        logger -p lpr.debug -t "lpadmin[${PID}]" \
		"System error: Cannot access /etc/printers.conf"
        gettext "lpadmin: System error; Cannot access /etc/printers.conf\n" 1>&2
        exit 1
fi

# remove _default 

DEFAULTP=`${LPGET} _default | grep use | sed -e 's/[    ]*use=//'`
${LPGET} -k bsdaddr ${DEFAULTP} >/dev/null 2>&1
status=$?

if [ ${status} -eq 0 ] ; then
	${LPSET} -x _default
	status=$?
	if [ ${status} -ne 0 ] ; then
		gettext "Warning: error removing _default entry from /etc/printers.conf\n" 1>&2
		logger -p lpr.debug -t "lpadmin[${PID}]" \
			"Call to lpset -x _default exits with ${status}"
		exit_code=1
	fi
fi

# delete entries in /etc/printers.conf for printers/classes that have
# been deleted

delete_local

# Delete all the remote printers using bsdaddr

for LINE in `grep bsdaddr /etc/printers.conf | grep -v ${HOST}`
do
        PRINTER=`echo $LINE | sed -e 's/^:bsdaddr=[^,]*,//' -e 's/[,:].*//'`
        ${LPSET} -x $PRINTER
	status=$?

	if [ ${status} -ne 0 ] ; then
		gettext "Warning: error removing ${PRINTER} entry from /etc/printers.conf\n" 1>&2
		logger -p lpr.debug -t "lpadmin[${PID}]" \
			"Call to lpset -x $PRINTER exits with ${status}"
		exit_code=1
	fi
done
}

if [ $# -lt 1 ] ; then
	usage
	exit 1
fi

# Deal with the -d option independently since getopts does not handle
# options that may or may not have arguments
#
first=$1
second=$2
third=$3

if [ ${first} = "-d" ] ; then
	# check that there are no extra arguments
	if [ -n "${third}" ] ; then
		usage
		exit 1
	fi


	# be sure we have lpset and lpget
	if [ ! -f ${LPSET} -o ! -f ${LPGET} ] ; then
		gettext "lpadmin: System error; cannot set default printer\n" 1>&2
		exit 2
	fi

	if [ ! -n "${second}" ] ; then
		# delete _default entry in /etc/printers.conf
		${LPSET} -n system -x _default
		exit_code=$?
		if [ ${exit_code} -ne 0 ] ; then
			gettext "lpadmin: System error while trying to delete default printer\n" 1>&2
		fi
	else
		# add/change  _default entry in /etc/printers.conf
		${LPGET} -k bsdaddr ${second} >/dev/null 2>&1
		exit_code=$?
		if [ $exit_code -eq 0 ] ; then
			${LPSET} -n system -a use=${second} _default
			exit_code=$?
		else
			echo "${second}: " 1>&2
			gettext "undefined printer\n" 1>&2
		fi

	fi
	exit ${exit_code}
fi

#		Strip off legal options
while getopts "A:ac:D:e:f:F:H:hi:I:lm:Mo:p:Q:r:S:s:T:u:U:v:W:x:t:P:" arg
do
	case $arg in
	D)
		description="${OPTARG}"
	;;
	p)
		if [ -n "${delete}" ] ; then
			usage
		fi
		printer=${OPTARG}
	;;
	s)
		server=${OPTARG}
	;;
	v|U)
		device=${OPTARG}
		server=`uname -n`
	;;
	x)
		if [ -n "${printer}" -o -n "${server}" -o \
		     -n "${device}" -o -n "${description}" ] ; then
			usage
		fi
		delete=${OPTARG}
		printer=${OPTARG}
		if [ ${printer} = "all" ] ; then
			local="true"
		fi
	;;
	S|M|A)
		local="true"
	;;
	c)
		class=${OPTARG}
		local="true"
		if [ ! -f ${LPGET} ] ; then
			gettext "lpadmin: System error; cannot set class\n " 1>&2
			exit 2
		fi

		${LPGET} "${class}" > /dev/null 2>&1
		lpget_class=$?
		if [ ${lpget_class} -eq 0 -a ! -r /etc/lp/classes/"${class}" ] ; then
			gettext "lpadmin: ERROR: Can't create class ${class}.\n" 1>&2
			gettext "           TO FIX: This is an existing printer name;\n" 1>&2
			gettext "                   choose another name.\n" 1>&2
			exit 1
		fi
	;;
	r)
		pconflocalclean="true"
		local="true"
	;;
	esac
done

#
# We don't have anything to do; let user know and bail
#
if [ ! -n "${printer}" -a ! -n "${delete}" -a ! -n "${local}" ] ; then
	gettext "lpadmin: ERROR: Nothing to do.\n" 1>&2
	gettext "        TO FIX: You must give one of these options:\n" 1>&2
	gettext "		      -p, -d, -x -S\n" 1>&2
	exit 1
fi

#
#       Printer does not exist
#       To be consistent with 2.5, assume adding local printer
#
if [ ! -n "${device}" -a ! -n "${server}" -a ! -n "${delete}" \
					 -a ! -n "${local}" ] ; then
	${LPGET} "${printer}" > /dev/null 2>&1
	lpget_stat=$?
	if [ ${lpget_stat} -ne 0 ] ; then
		gettext "lpadmin: ERROR: Missing -U or -v option.\n" 1>&2
		gettext "           TO FIX: Local printers must have\n" 1>&2
		gettext "                   a port defined (-v option) or\n" 1>&2
		gettext "                   have dial-out instructions (-U option).\n" 1>&2
		exit 1
	fi
fi

#
#	Do the LP configuration for a local printer served by lpsched
#
if [ -f /usr/lib/lp/local/$cmd_name ] ; then
	if [ -f /etc/lp/printers/${printer}/configuration -o -n "${device}" -o \
	     -f /etc/lp/classes/${printer} -o -n "${local}" ] ; then
		# to deal with multi-word arguments
		CMD="/usr/lib/lp/local/$cmd_name"
		while [ -n "$*" ] ; do
			CMD="$CMD \"$1\""
			shift
		done
		case "$CMD" in
			*\"-D\")
				CMD="$CMD \"\""
			;;
		esac
		eval $CMD
		exit_code=$?
	fi
fi

if [ $exit_code != 0 ] ; then
	exit $exit_code
fi


#	split the "server" into printer and server
if [ -n "${server}" ] ; then
	if [ `echo ${server} | grep -c !` = 1 ] ; then
		rem_printer=`echo ${server} | cut -d! -f2`
	fi
	server=`echo ${server} | cut -d! -f1`
fi

if [ -z "${rem_printer}" ] ; then
	rem_printer=${printer}
fi



#
#	Do the Solstice Print Configuration in /etc
#
if [ ! -f ${LPSET} -o ! -f ${LPGET} ] ; then
	exit_code=2
else
	if [ -n "${delete}" ] ; then
		if [ "${delete}" = "all" ] ; then
			delete_entries
   		else
   			${LPSET} -n system -x ${delete}
   			exit_code=$?
			delete_local
   		fi
	fi

	if [ -n "${printer}" -a -n "${server}" ] ; then
		${LPSET} -n system \
			-a "bsdaddr=${server},${rem_printer},Solaris" \
			${printer}
		exit_code=$?
	fi
	if [ -n "${printer}" -a -n "${description}" ] ; then
		${LPSET} -n system -a "description=${description}" ${printer}
		exit_code=$?
	fi

#	Add class for local printers only

	if [ -n "${class}" -a -n "${printer}" \
		-a -f /etc/lp/printers/${printer}/configuration ] ; then

		${LPGET} "${class}" > /dev/null 2>&1
		lpget_class=$?

#		If the class doesn't already exist in printers.conf, add it.

		if [ ${lpget_class} -ne 0 ] ; then
			this_server=`uname -n`
			${LPSET} -n system \
				-a "bsdaddr=${this_server},${class},Solaris" ${class}
			exit_code=$?
		fi
	fi
fi

# /usr/lib/lp/local/lpadmin has changed the database. This cleans up cruft in the 
# /etc/printers.conf file that refers to deleted objects.

	if [ -n "${pconflocalclean}" ] ; then
		delete_local
	fi

exit $exit_code
