#!/bin/sh -e
# NAME=USB presence
# DESCRIPTION=Tests presence of designated USB device
# DESTROYS_HDD=false
# IS_INTERACTIVE=false
# POWEROFF_DURING_TEST=false
# VERSION=0.1
# TAGS=usb
# VAR=IDVENDOR:string:0403:Filter in only devices with this idVendor (match all if empty)
# VAR=IDPRODUCT:string:6001:Filter in only devices with this idProduct (match all if empty)
# VAR=COUNT:int:1:There should be this many devices

. /usr/share/inquisitor/functions-test

get_usb_devices()
{
	for I in /sys/bus/usb/devices/*; do

		# Check that it's readable
		[ -r "$I/idVendor" ] || continue
		[ -r "$I/idProduct" ] || continue

		# Match
		if [ -n "$IDVENDOR" ]; then
			if [ "$IDVENDOR" != "`cat $I/idVendor`" ]; then
				continue
			fi
		fi
		if [ -n "$IDPRODUCT" ]; then
			if [ "$IDPRODUCT" != "`cat $I/idProduct`" ]; then
				continue
			fi
		fi

		echo $I
	done
}

FOUND=`get_usb_devices | wc -l`

[ "$FOUND" -eq "$COUNT" ] && test_succeeded || \
	test_failed "Found $FOUND devices instead of $COUNT"
