#! /bin/sh
#
# Brother Print filter
# 
# Copyright Brother Industries,Ltd 2006-2014


BRPRINTER_NAME=$1
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/filter_${BRPRINTER_NAME}:
EOF
	;;
	-e)
	set +o noclobber
	cp $PRINTCAP_NAME "$PRINTCAP_NAME".tmp
	cat ${PRINTCAP_NAME}.tmp | eval sed "/${BRPRINTER_NAME}:/,/filter$BRPRINTER_NAME:/d" > ${PRINTCAP_NAME}
	rm -f ${PRINTCAP_NAME}.tmp

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