#!/bin/sh

. /lib/functions.sh
. /lib/functions/system.sh

NOTYFY_FW_UPGRADE_CAT="D0004"
NOTYFY_FW_UPGRADE_EVT_INIT="I0011"
NOTYFY_FW_UPGRADE_EVT_DONE_SUCCESS="I0012"
NOTYFY_FW_UPGRADE_EVT_DONE_FAILURE="W000F"

. /lib/rg/sfp/defines.sh

v() {
	[ "$VERBOSE" -ge 1 ] && echo "$@"
}

# initialize defaults
export VERBOSE=1
export PROGRESS=0
export HELP=0
export CHECK=0
export RESET=0

# parse options
while [ -n "$1" ]; do
	case "$1" in
		-v) export VERBOSE="$(($VERBOSE + 1))";;
		-q) export VERBOSE="$(($VERBOSE - 1))";;
		-i|--progress) export PROGRESS=1;;
		-c|--check) export CHECK=1;;
		-r|--reset) export RESET=1;;
		-h|--help) export HELP=1; break;;
		-*)
			echo "Invalid option: $1"
			exit 1
		;;
		*) break;;
	esac
	shift;
done

APP_IMAGE="$1"
MAX_TRIES=5


tms_reset() {
	# force RST low
	rst_low
	usleep 100000

	# force RST high
	rst_high
	usleep 1000000
}


sfp_instance=$(pidof rg-sfp)

if [ "x$sfp_instance" != "x" ]; then
	echo ":i 0 2 Another upgrade in progress"
	exit 2
fi

# reset only
if [ $RESET -eq 1 ]; then
	/etc/init.d/rg-mcu-uim stop >/dev/null 2>&1  && rmmod rg_mcu_ldisc >/dev/null 2>&1

	gpio_init
	tms_reset
	sleep 1
	gpio_deinit

	sleep 10
	modprobe rg_mcu_ldisc >/dev/null 2>&1 && /etc/init.d/rg-mcu-uim start >/dev/null 2>&1

	exit 0;
fi

[ -z "$APP_IMAGE" -o $HELP -gt 0 ] && {
	cat <<EOF
Usage: $0 [<upgrade-option>...] <image file or URL>

upgrade-option:
	-c | --check
	             Verify image but do not actually flash.
	-r | --reset
	             Reset TMS but do not actually flash.
	-q           less verbose
	-v           more verbose
	-i           show progress indicator
	-h | --help  display this help

EOF
	exit 1
}

case "$APP_IMAGE" in
	http://*|\
	https://*)
		wget -O/tmp/tms_fw.txt "$APP_IMAGE"
		APP_IMAGE=/tmp/tms_fw.txt
		;;
esac

APP_IMAGE="$(readlink -f "$APP_IMAGE")"

case "$APP_IMAGE" in
	'')
		echo "Firmware image file not found."
		exit 1
		;;
	/tmp/*)	;;
	*)
		v "Image not in /tmp, copying..."
		cp -f "$APP_IMAGE" /tmp/tms_fw.txt
		APP_IMAGE=/tmp/tms_fw.txt
		;;
esac

( rg-sfp -q -c -k "$KERN_IMAGE" -a "$APP_IMAGE" ) || {
	echo "Firmware image(s) check failed."
	exit 1
}

if [ $CHECK -eq 1 ]; then
	exit 0
fi

OPTS="-p $UART -b $BAUD"
if [ $PROGRESS -eq 1 ]; then
	OPTS="$OPTS -i"
fi
if [ $VERBOSE -ge 1 ]; then
	OPTS="$OPTS -v"

	if [ $VERBOSE -ge 2 ]; then
		OPTS="$OPTS -d"
	fi
else
	OPTS="$OPTS -q"
fi

/sbin/rg-notify.sh "$NOTYFY_FW_UPGRADE_CAT" "$NOTYFY_FW_UPGRADE_EVT_INIT"

if [ $PROGRESS -eq 1 ]; then
	echo ":i $retry 0 Acquire transport"
fi

# sent Ping Of Deatch to TMS
if [ -f /sys/devices/mci0/mcu/_pod ]; then
	mcu_validity=$(cat /sys/devices/mci0/mcu/validity 2>/dev/null)
	if [ $mcu_validity -eq 1 ]; then
		echo 1 > /sys/devices/mci0/mcu/_pod
		sleep 1
	fi
fi

/etc/init.d/rg-mcu-uim stop >/dev/null 2>&1  && rmmod rg_mcu_ldisc >/dev/null 2>&1

gpio_init

retry=0
while [ 1 -eq 1 ]; do

	# force TDO low
	tdo_low
	usleep 1000000

	tms_reset

	echo rg-sfp $OPTS -k "$KERN_IMAGE" -a "$APP_IMAGE" -r "$retry"
	rg-sfp $OPTS -k "$KERN_IMAGE" -a "$APP_IMAGE" -r "$retry"
	rc=$?

	# force TDO high
	tdo_high
	usleep 1000000

	tms_reset

	if [ $rc -eq 0 ]; then
		/sbin/rg-notify.sh "$NOTYFY_FW_UPGRADE_CAT" "$NOTYFY_FW_UPGRADE_EVT_DONE_SUCCESS"
	else
		/sbin/rg-notify.sh "$NOTYFY_FW_UPGRADE_CAT" "$NOTYFY_FW_UPGRADE_EVT_DONE_FAILURE"
	fi

	if [ $PROGRESS -eq 1 ]; then
		echo ":q $retry $rc Finished"
	fi

	if [ $rc -eq 0 ]; then
		break
	else
		retry=$((retry + 1))
		if [ $retry -ge $MAX_TRIES ]; then
			break
		fi
	fi
done
gpio_deinit

sleep_time=10
while [ $sleep_time -ge 0 ]; do
	if [ $PROGRESS -eq 1 ]; then
		echo ":w $retry $rc $sleep_time"
	fi
	sleep_time=$((sleep_time - 1))
	sleep 1
done

modprobe rg_mcu_ldisc >/dev/null 2>&1 && /etc/init.d/rg-mcu-uim start >/dev/null 2>&1

exit $rc
