#! /bin/sh
#


BRPRINTER_NAME=$1
if ! expr "$BRPRINTER_NAME" : '^[A-Za-z0-9_-]*$' >/dev/null; then
    exit 1
fi
PRINTCAP_NAME=/etc/printcap.local
SPOOLER_NAME=/var/spool/lpd/$BRPRINTER_NAME
DEVICE_IF=/dev/lp0

if [ ! -f $PRINTCAP_NAME ]; then
	PRINTCAP_NAME=/etc/printcap
fi

case "$2" in
	-i)
	if [ ! -d $SPOOLER_NAME ]; then
	        mkdir $SPOOLER_NAME
	fi
	chown lp $SPOOLER_NAME
	chgrp lp $SPOOLER_NAME
	chmod 700 $SPOOLER_NAME

	if [ "$3" = "USB" ]; then
		DEVICE_IF=/dev/usb/lp0
	fi
	
	cat <<EOF >>$PRINTCAP_NAME
${BRPRINTER_NAME}:\\
        :mx=0:\\
        :sd=/var/spool/lpd/$BRPRINTER_NAME:\\
        :sh:\\
        :lp=$DEVICE_IF:\\
        :if=/opt/brother/Printers/$BRPRINTER_NAME/lpd/lpdfilter:
EOF
	;;
	-e)
	set +o noclobber
	cp $PRINTCAP_NAME "$PRINTCAP_NAME".tmp
        sed "\#${BRPRINTER_NAME}:#,\#lpdfilter:#d" "${PRINTCAP_NAME}.tmp" > "${PRINTCAP_NAME}"
	rm -f ${PRINTCAP_NAME}.tmp

	rm -Rf $SPOOLER_NAME
	;;
	*)
	;;
esac
