#!/bin/bash
			
show_help()
{
	cat << EOF
usage: pommax-prog [OPTION]... [IMAGE]

Options:
	--device=DEV	use specified device instead of /dev/ttyS0
	--list		list all installed images
	--file=FILE	use specified file
	--help		show this help
EOF
}

TEMP=$(getopt -o hC --long device:,file:,list,help -n pommax-prog -- "$@")
if [ $? != 0 ]; then
	echo "Terminating..." >&2
	exit 1
fi

eval set -- "$TEMP"

DEVICE=/dev/ttyS0
IMAGE=pommax3v2-0.2.0
DATADIR=/usr/local/share/tmos

FILE=""
LIST=""

while true; do
	case "$1" in
		--device)
			DEVICE="$2"
			shift 2
			;;
		--file)
			FILE="$2"
			shift 2
			;;
		--list)
			LIST=y;
			shift;
			;;
		--help)
			show_help;
			exit 0;
			;;
		--)
			shift
			break
			;;
		*)
			echo "Internal error!"
			exit 1
			;;
	esac
done

if [ -n "$LIST" ]; then
	(cd "$DATADIR"; ls *.hex | sed 's/\.hex$//')
	exit;
fi

[ -n "$1" ] && IMAGE="$1";

[ -z "$FILE" ] && FILE="$DATADIR/$IMAGE.hex";

aducprog -t -e -p --Device "$DEVICE" --R -r "$FILE"
