#!/bin/sh
#
# $FreeBSD: head/print/cups-base/files/ulpt-cupsd.sh.in 340872 2014-01-24 00:14:07Z mat $

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
