# cups - CUPS Printing spooler and server

description     "CUPS printing spooler/server"
author          "Michael Sweet <msweet@apple.com>"

start on (filesystem
          and (started dbus or runlevel [2345]))
stop on runlevel [016]

respawn
respawn limit 3 12

pre-start script
    [ -x /usr/sbin/cupsd ]
    
    # load modules for parallel port support
    if [ -r /etc/default/cups ]; then
	. /etc/default/cups
    fi
    if [ "$LOAD_LP_MODULE" = "yes" -a -f /usr/lib/cups/backend/parallel \
	 -a -f /proc/modules -a -x /sbin/modprobe ]; then
	modprobe -q -b lp || true
	modprobe -q -b ppdev || true
	modprobe -q -b parport_pc || true
    fi
    
    mkdir -p /var/run/cups/certs
    if [ -x /lib/init/apparmor-profile-load ]; then
	/lib/init/apparmor-profile-load usr.sbin.cupsd
    fi
end script

exec /usr/sbin/cupsd -f

post-start script
    # wait until daemon is ready
    timeout=60
    while [ ! -e /var/run/cups/cups.sock ]; do 
        sleep 0.5
	timeout=$((timeout-1))
	if [ "$timeout" -eq 0 ]; then
	    echo "cupsd failed to create /var/run/cups/cups.sock, skipping automatic printer configuration" >&2
	    exit 0
	fi
    done
    
    # coldplug USB printers
    if ! /lib/udev/udev-configure-printer enumerate 2>/dev/null; then
        if type udevadm > /dev/null 2>&1 && [ -x /lib/udev/udev-configure-printer ]; then
            for printer in `udevadm trigger --verbose --dry-run --subsystem-match=usb \
                    --attr-match=bInterfaceClass=07 --attr-match=bInterfaceSubClass=01 2>/dev/null || true; \
                            udevadm trigger --verbose --dry-run --subsystem-match=usb \
                    --sysname-match='lp[0-9]*' 2>/dev/null || true`; do
                /lib/udev/udev-configure-printer add "${printer#/sys}"
            done
        fi
    fi
end script
