#!/bin/bash
#
#  HTCFlasherGUI
# 
#  Copyright (C) 2007-2008 Pau Oliva Fora - <pof@eslack.org>
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License version 3 as published
#  by the Free Software Foundation.
# 

ZEN="zenity --name=HTCFlasher --title=HTCFlasher"
TERMINAL="xterm -T HTCFlasher -e"
#TERMINAL="gnome-terminal --hide-menubar --title=HTCFlasher -e"

TMPF=/tmp/tmpfile.$$

device="/dev/ttyUSB0"
extracted=""
flashed=""
debug="NO"
donate=""

OPT1="Extract NBH file"
OPT2="Flash NBH file"
OPT3="Get device info"
OPT4="Bootloader Cmd prompt"
OPT5="Serial port"
OPT6="Show debug info"
OPT7="Donate                      "

options() {

	FLA="HTCFlasher -z"

	if [ "$debug" = "YES" ]; then 
		FLA="$FLA -v"
	fi

	ans=$($ZEN --width 500 --height 340 --list  --text "HTCFlasher v3.0\nOpen Source RUU for HTC devices\n(c) 2007-2008 Pau Oliva Fora" --radiolist --column "" --column "Option" --column "" TRUE "$OPT1" "$extracted" FALSE "$OPT2" "$flashed" FALSE "$OPT3" "$devinfo" FALSE "$OPT4" "" FALSE "$OPT5" "[ $device ]" FALSE "$OPT6" "[ $debug ]" FALSE "$OPT7" "$donate")

	if [ -z "$ans" ]; then
		echo "bye :)"
		exit 0
	fi

	if [ "$ans" = "$OPT1" ]; then
		filename=$($ZEN --file-selection)
		if [ -z "$filename" ]; then
			return 1
		fi
		(( $FLA -X "$filename" 3>&2 2>&1 1>&3 |tee ) 3>&2 2>&1 1>&3 ) 2>&1 |tee $TMPF | $ZEN --progress --auto-close --text="$OPT1" --width 400
		cat $TMPF |grep -v "^===" |grep -v "ZENITY" | $ZEN --text-info --width 500 --height 430
		rm $TMPF
		filename=$(basename $filename)
		extracted="Extracted '$filename'"
	fi

	if [ "$ans" = "$OPT2" ]; then
		filename=$($ZEN --file-selection)
		if [ -z "$filename" ]; then
			return 1
		fi
		$ZEN --question --text="Flash file '$filename'?"
		if [ $? == 0 ]; then
			(( $FLA -D $device -F "$filename" 3>&2 2>&1 1>&3 |tee ) 3>&2 2>&1 1>&3 ) 2>&1 |tee $TMPF |$ZEN --progress --auto-close --text="$OPT2, please wait..." --width 400
			cat $TMPF |grep -v "^===" |grep -v "ZENITY" | $ZEN --text-info --width 500 --height 430
			rm $TMPF
			filename=$(basename $filename)
			flashed="Flashed '$filename'"
		fi
	fi

	if [ "$ans" = "$OPT3" ]; then
		(( $FLA -D "$device" -i 3>&2 2>&1 1>&3 |tee ) 3>&2 2>&1 1>&3 ) 2>&1 |tee $TMPF | $ZEN --progress --auto-close --text="$OPT3" --width 400
		cat $TMPF |grep -v "^===" |grep -v "ZENITY" | $ZEN --text-info --width 400 --height 300
		cid=$(cat $TMPF |grep "\[\] CID" |cut -f 3 -d " ")
		modelid=$(cat $TMPF |grep "\[\] ModelID" |cut -f 3 -d " ")
		rm $TMPF
		devinfo=""
                if [ -n "$cid" ] && [ -n "$modelid" ]; then
                        devinfo="CID: [$cid] - ModelID: [$modelid]"
                fi
                if [ -n "$cid" ] && [ -z "$modelid" ]; then
                        devinfo="CID: [$cid]"
                fi
                if [ -z "$cid" ] && [ -n "$modelid" ]; then
                        devinfo="ModelID: [$modelid]"
                fi
		if [ -z "$devinfo" ]; then
			devinfo="UNKNOWN DEVICE!"
		fi
	fi

	if [ "$ans" = "$OPT4" ]; then
		$TERMINAL "HTCFlasher -D $device -p ; read -p 'Press enter to close.'"
		if [ $? != 0 ]; then
			$ZEN --warning --text="TERMINAL is not properly set"
		fi
	fi

	if [ "$ans" = "$OPT5" ]; then
		oldport=$device
		export device=$(zenity --entry --entry-text="$device" --text="Select serial port device to use")
		if [ -z "$device" ]; then
			device="$oldport"
		fi
		if [ ! -e "$device" ]; then
			$ZEN --warning --text="Device '$device' doesn't exist"
		fi
	fi

	if [ "$ans" = "$OPT6" ]; then
		$ZEN --question --text="Enable verbose mode?"
		if [ $? == 0 ]; then
			debug="YES"
		else
			debug="NO"
		fi
	fi

	if [ "$ans" = "$OPT7" ]; then
		$ZEN --info --text="Opening PayPal donation page in Firefox\n\nThank you!"
		firefox "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=pau%40eslack%2eorg&item_name=HTCFlasher%20donation&no_shipping=1&cn=Comments&tax=0&currency_code=EUR&bn=PP%2dDonationsBF&charset=UTF%2d8"
		donate=":)"
	fi
}

while true
do
	options
done
