#!/bin/bash

if [ `whoami` != "root" ]; then
	echo "Please run this as root."
	exit 1
fi

if ! mount | grep -E 'usbfs|usbdevfs' > /dev/null; then
    echo "usbfs is not mounted on /proc/bus/usb/"
    echo "mount it and try again..."
    exit 1
fi

bus=`lsusb | grep 093b:a00 | cut -d " " -f 2`
device=`lsusb | grep 093b:a00 | cut -d " " -f 4 | sed 's/://'`
type=`lsusb | grep 093b:a00 | cut -d " " -f 6`
[ $type == "093b:a002" ] && hex="/lib/firmware/ezusb/hpi_PX-M402U.hex"
[ $type == "093b:a004" ] && hex="/lib/firmware/ezusb/hpi_PX-TV402U.hex"

if /sbin/fxload -t fx2 -I $hex -D /proc/bus/usb/"$bus"/"$device"; then
	echo 'Firmware loaded successfully!'
else
	echo 'Firmware not loaded :-('
	exit 1
fi
