#!/bin/sh

echo $(date '+%Y-%m-%d %H:%M:%S') $@ >> /tmp/hotplug

DEVCLASS=$1
DEVNAME=$2
DEVDMESG=$(dmesg | grep ^$DEVNAME | tail -1)
DEVDESC=$(echo $DEVDMESG | cut -f 2 -d '"')
DEVHUB=$(echo $DEVDMESG | cut -f 3 -d ' ')
DEVUSB=$(dmesg | grep ^$DEVHUB | tail -1 | cut -f 3 -d ' ')
# Determine which USB controller we're plugged into
while true; do
	case $DEVUSB in
		usb*)
			break
			;;
		uhub*)
			DEVUSB=$(dmesg | grep ^$DEVUSB | tail -1 | cut -f 3 -d ' ')
			;;
		*)
			echo "Error: Unknown device: $DEVUSB"
			exit 1
			;;
	esac
done

case $DEVNAME in
	cdce*)
		ifconfig $DEVNAME up
		dhclient $DEVNAME
		;;
	ugen*)
		case $DEVDESC in
			*Scanner)
				chown zwhite:zwhite /dev/${DEVNAME}*
				chgrp zwhite /dev/${DEVUSB}
				;;
		esac
		;;
esac
