#!/bin/sh
# This script changes the permissions and ownership of a USB device under
# /proc/bus/usb to grant access to this device to users in the scanner group.
#
# Ownership is set to root:scanner, permissions are set to 0660.

# The following environment variable is used:
# DEVICE=/proc/bus/usb/BUSNUM/DEVNUM

if [ "$ACTION" = "add" ]
then
	BUSANDDEV=${DEVICE#/proc/bus/usb/}
	BUSNUM=${BUSANDDEV%/*}
	DEVNUM=${BUSANDDEV#*/}
	if sane-find-scanner -q "libusb:$BUSNUM:$DEVNUM" | grep -q "^found"
	then
		chown root:scanner "$DEVICE"
		chmod 0660 "$DEVICE"
		# That's an insecure but simple alternative
		# chmod 0666 "$DEVICE"
	fi
fi
