#!/bin/sh
#
# $FreeBSD: ports/print/cups-base/files/ulpt-cupsd.sh.in,v 1.1 2011/01/07 21:24:58 dinoex Exp $

usage() {
	echo "$0: usage:" >&2
	echo "$0 [enable|disable] devicename" >&2
	exit 1
}

if [ -z "$2" ]; then
	usage
fi

printer="$(awk -v DEVICENAME="$2" '
/^<(Default)?Printer[[:space:]]/ { 
	if (match($0, /[^[:space:]>]+>/)) {
		PRINTER=substr($0, RSTART, RLENGTH - 1);
	}
}
/^<\/(Default)?Printer/ {
	PRINTER=""
}
match($0, "DeviceURI[[:space:]]+usb:/dev/" DEVICENAME "\$") {
	print PRINTER
}
' /usr/local/etc/cups/printers.conf)"

if [ -z "$printer" ]; then
	# nothing to do
	exit 0
fi

case "$1" in
enable)
	chown cups:cups "/dev/$2"
	/usr/local/sbin/cupsenable "$printer"
	;;
disable)
	/usr/local/sbin/cupsdisable "$printer"
	;;
*)
	usage
	;;
esac
exit 0
# eof
