#!/bin/sh

set -e

#DEBHELPER#

if [ "$1" = configure ]; then
	if [ -z `getent group vboxusers` ]; then
		addgroup --system --quiet vboxusers
	fi

	# Build usb device tree
	for i in /sys/bus/usb/devices/*; do
		if test -r "$i/dev"; then
			dev="`cat "$i/dev" 2> /dev/null || true`"
			major="`expr "$dev" : '\(.*\):' 2> /dev/null || true`"
			minor="`expr "$dev" : '.*:\(.*\)' 2> /dev/null || true`"
			class="`cat $i/bDeviceClass 2> /dev/null || true`"
			/lib/udev/VBoxCreateUSBNode.sh "$major" "$minor" "$class" vboxusers 2>/dev/null || true
		fi
	done

	# only restart if VirtualBox isn't running
	if test -x /etc/init.d/virtualbox && ! pidof VBoxSVC > /dev/null; then
		invoke-rc.d virtualbox restart || true
	fi
fi
