#!/bin/sh

# probe device's VIDs and PIDs


version()
{
	echo "$VERSION"
	exit 0
}

commandlinehelp()
{
	echo "Usage:"
	echo "          $BASE [<switch>..] [dir]"
	echo "Switches:"
	echo "          --try-all              try all the synch .bin even if one is OK"
	echo "          --usb-init             reset modem between each synch .bin test"
	echo "          --no-prompt            don't prompt before loading firmware (only enabled if --usb-init is set)"
	echo "          --batch                disable default interactive mode"
	echo "          --version or -v        show version number then exit"
	echo "          --help or -h           show this help then exit"
	exit $1
}

prompt()
{
	local dummy
	echo
	echo -n "$1 your modem now, press ENTER to continue"
	read dummy
}

reset_usb()
{
	lsmod | grep $HUB > /dev/null
	if [ $? -eq 0 ]
	then
		if [ $PROMPT -eq 1 ]
		then
			prompt "Unplug"
		fi
		echo
		echo "Resetting USB HUB.."
		modprobe -r $HUB > /dev/null
		if [ $? -ne 0 ]
		then
			echo "*** unable to unload $HUB, this might be an USB issue and you must reset is manually"
			exit 1
		fi
		sleep 1
		MSG="Replug"
	else
		MSG="Plug in"
	fi
	modprobe $HUB > /dev/null
	if [ $? -ne 0 ]
	then
		echo "*** unable to load $HUB, this might be an USB issue"
		exit 1
	fi
	if [ $PROMPT -eq 1 ]
	then
		prompt "$MSG"
	fi
	sleep 1
}

init_modem()
{
	echo
	echo "Loading firmware.."
	$BIN_DIR/eciadsl-firmware $FIRMWARE_OPTIONS 0x$VID1 0x$PID1 0x$VID2 0x$PID2 "$FIRMWARE" > /dev/null 2>&1
	if [ $? -eq 0 ]
	then
		echo "Firmware loaded"
	else
		echo "*** failed to load firmware, aborting"
		exit 1
	fi
}


# <CONFIG>
BIN_DIR="/usr/local/bin"
ETC_DIR="/etc"
CONF_DIR="/etc/eciadsl"
PPPD_DIR="/etc/ppp"
VERSION=""
# </CONFIG>

BASE=${0##*/}
DIR=""
ALL=0
INTERACTIVE=1
FULL_INIT=0
PROMPT=1

while [ -n "$1" ]
do
	case "$1" in
		"--usb-init")		let FULL_INIT=1;;
		"--no-prompt")		let PROMPT=0;;
		"--try-all")		let ALL=1;;
		"--batch")			let INTERACTIVE=0;;
		"--version"|"-v")	version;;
		"--help"|"-h")		commandlinehelp 0;;
		*)					test -z "$DIR" && DIR=$1 || break;;
	esac
	shift
done

[ -n "$UID" ] || UID="$(id -ru)"
if [ $UID -ne 0 ]; then
then
	echo
	echo "You must be root to run this script!"
#	exit 1
fi

echo
echo "WARNING: if no $CONF_DIR/eciadsl.conf file exists,"
echo "default VID/PID will be assumed. This eciadsl.conf file is generated using"
echo "eciadsl-config-tk or eciadsl-config-text, please read the INSTALL file to"
echo "properly install and configure the driver"
echo "This script requires your modem to be supported by the driver, and that"
echo "you've installed some extra synch .bin (the official synch_bin package for instance)"
echo "It might be also necessary to unplug/wait/replug your modem before your"
echo "this script and to ensure that no pppd/eciadsl-pppoeci instance is running"
echo

FIRMWARE_OPTIONS=""
SYNCH_OPTIONS=""
echo
if [ -f "$CONF_DIR/eciadsl.conf" ]; then
	VID1=`grep -iE "^[ \t]*VID1[ \t]*=" "$CONF_DIR/eciadsl.conf" | tail -n 1 | cut -f 2 -d '=' | tr -d " \t"`
	PID1=`grep -iE "^[ \t]*PID1[ \t]*=" "$CONF_DIR/eciadsl.conf" | tail -n 1 | cut -f 2 -d '=' | tr -d " \t"`
	VID2=`grep -iE "^[ \t]*VID2[ \t]*=" "$CONF_DIR/eciadsl.conf" | tail -n 1 | cut -f 2 -d '=' | tr -d " \t"`
	PID2=`grep -iE "^[ \t]*PID2[ \t]*=" "$CONF_DIR/eciadsl.conf" | tail -n 1 | cut -f 2 -d '=' | tr -d " \t"`
	FIRMWARE=`grep -iE "^[ \t]*FIRMWARE[ \t]*=" "$CONF_DIR/eciadsl.conf" | tail -n 1 | cut -f 2 -d '=' | tr -s "\t" " "`
	FIRMWARE_OPTIONS=`grep -iE "^[ \t]*FIRMWARE_OPTIONS[ \t]*=" "$CONF_DIR/eciadsl.conf" | tail -n 1 | cut -f 2 -d '=' | tr -s " \t" " "`
	SYNCH_OPTIONS=`grep -iE "^[ \t]*SYNCH_OPTIONS[ \t]*=" "$CONF_DIR/eciadsl.conf" | tail -n 1 | cut -f 2 -d '=' | tr -s " \t" " "`
	MODEM_CHIPSET=`grep -E "^[ \t]*MODEM_CHIPSET[ \t]*=" "$CONF_DIR/eciadsl.conf" | tail -n 1 | cut -f 2 -d '=' | tr -s "\t" " "`
	echo "Config read from $CONF_DIR/eciadsl.conf"
else
	echo "Default config assumed"
fi
test -z "$VID1" && VID1="0547"
test -z "$PID1" && PID1="2131"
test -z "$VID2" && VID2="0915"
test -z "$PID2" && PID2="8000"
test -z "$FIRMWARE" && FIRMWARE="$CONF_DIR/firmware00.bin"
test -z "$DIR" && DIR=$CONF_DIR

if [ $INTERACTIVE -eq 1 ]
then
	echo
	echo "Type in a directory where to find the synch .bin"
	echo -n "[default is $DIR]: "
	read USERDIR
	test -n "$USERDIR" && DIR="$USERDIR"
fi

if [ ! -d "$DIR" ]
then
	echo
	echo "*** cannot access to $DIR"
	exit 1
fi

LIST=""
if [ "$MODEM_CHIPSET" = "GS7470" ]; then
    for FILE in $(find "$DIR" -maxdepth 1 -type f -name "gs7470_*.bin" | grep -v "firmware"| grep -v "a.bin"| grep -v "b.bin"); do
	if [ -n "$LIST" ]; then
		LIST="$LIST
${FILE##*/}"
	else
		LIST="${FILE##*/}"
	fi
    done
else
    for FILE in $(find "$DIR" -maxdepth 1 -type f -name "*.bin" | grep -v "firmware"| grep -v "gs7470"| grep -v "a.bin"| grep -v "b.bin"); do
	if [ -n "$LIST" ]; then
		LIST="$LIST
${FILE##*/}"
	else
		LIST="${FILE##*/}"
	fi
    done
fi

if [ -z "$LIST" ]
then
	echo
	echo "*** no synch .bin has been found in $DIR, aborting"
	exit 1
fi

echo
echo "These synch .bin will be tested:"
echo "$LIST"
if [ $INTERACTIVE -eq 1 ]
then
	echo -n "Start the tests now? (Y/n) "
	read CONFIRM
	case $CONFIRM in
	Y|y|"")	;;
	*)		echo "*** cancelled by user, exiting"
			exit 1;;
	esac
fi

# HUB=usb-uhci

if [ $FULL_INIT -eq 0 ]
then
	init_modem
else
	echo
	echo -n "Which USB HUB module is yours (common values are usb-uhci, usb-ohci or uhci)? "
	HUB=""
	while [ -z "$HUB" ]
	do
		read HUB
		if [ -z "$HUB" ]
		then
			echo -n "* you must enter a valid USB HUB module name, try again: "
		fi
	done
	reset_usb
fi

echo "$LIST" |
while read FILE
do
	if [ $FULL_INIT -eq 1 ]
	then
		init_modem
	fi

	echo
	echo "Testing synch with $DIR/$FILE.."
	$BIN_DIR/eciadsl-synch $SYNCH_OPTIONS 0x$VID2 0x$PID2 "$DIR/$FILE"
	if [ $? -eq 0 ]
	then
		test -n "$LISTOK" && LISTOK="$LISTOK\n$FILE" || LISTOK="$FILE"
		echo "$DIR/$FILE seems OK"
		test $ALL -eq 0 && break
	fi
	echo "Trying the next one.."
	sleep 1

	if [ $FULL_INIT -eq 1 ]
	then
		reset_usb
	fi
done

echo
if [ -z "$LISTOK" ]
then
	echo "** No valid synch .bin has been found"
	echo "There are many possible issues"
	echo "and it may be necessary to generate your own one. Please refer to"
	echo "the documentation in both cases."
else
	echo "These synch .bin are supposed to be OK:"
	echo "$LISTOK"
fi
exit
