#!/bin/sh

# Copyright (C) 2006-2012 Bart Martens <bartm@knars.be>
# Copyright (C) 2013 Bdale Garbee <bdale@gag.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -e

return_0() {
	return 0
}

trap "return_0" 0

die_hard() {
	echo "ERROR: $1" >&2
	exit 1
}

[ `whoami` = "root" ] || die_hard "must be root"

show_usage() {
	echo "Usage:"
	echo "  update-openrocket --install"
	echo "  update-openrocket --uninstall"
	echo "  update-openrocket --status"
	echo "Additional options:"
	echo "  --verbose"
	echo "  --quiet"
	exit 1
}

getopt_temp=`getopt -o iusfvq --long install,uninstall,status,fast,verbose,quiet -n 'update-openrocket' -- "$@"` || show_usage
eval set -- "$getopt_temp" || show_usage

ACTION=none
fast=no
verbose=no
quiet=no

while [ true ]
do
	case "$1" in
		-i|--install)
			ACTION="--install"
			shift
			;;
		-u|--uninstall)
			ACTION="--uninstall"
			shift
			;;
		-s|--status)
			ACTION="--status"
			shift
			;;
		-f|--fast)
			fast=yes
			shift
			;;
		-v|--verbose)
			verbose=yes
			shift
			;;
		-q|--quiet)
			quiet=yes
			shift
			;;
		--)
			shift
			break
			;;
		*)
			echo "Internal error!"
			exit 1
			;;
	esac
done

[ "$ACTION" != "none" -a $# -eq 0 ] || show_usage
[ "$quiet" != "yes" ] || verbose=no

[ "$verbose" != "yes" ] || echo "options : $getopt_temp"

UNPACKDIR=`mktemp -d /tmp/openrocket.XXXXXXXXXX` || die_hard "mktemp failed"
echo "$UNPACKDIR" | grep -q "^/tmp/openrocket\." || die_hard "paranoia"
cd "$UNPACKDIR" || die_hard "cd failed"

[ "$verbose" != "yes" ] || echo "temporary directory: $UNPACKDIR"

do_cleanup() {
	[ "$verbose" != "yes" ] || echo "cleaning up temporary directory $UNPACKDIR ..."
	cd /
	echo "$UNPACKDIR" | grep -q "^/tmp/openrocket\." || die_hard "paranoia"
	rm -rf "$UNPACKDIR"
}

die_hard_with_a_cleanup() {
	return_0
	do_cleanup
	die_hard "$1"
}

trap "die_hard_with_a_cleanup interrupted" INT

cachedir=/var/cache/openrocket

wgetquiet=' -q '
wgetfast='-t 3 -T 15 '
wgetalways=' -nd -P . '
wgetprogress=' -v --progress=dot:default '

get_installed_version() {

	installed=`unzip -p /usr/lib/openrocket/*.jar build.properties | grep build.version= | awk -F\= '{ print $2 }'`
}

get_download_url() {
	url=`wget -qO - http://openrocket.sourceforge.net | grep https | grep download | awk -F\" '{ print $4 }'`
}

get_upstream_version() {
	get_download_url
	upstream=`echo $url | awk -F/ '{ print $8 }'`
}

case "$ACTION" in

	--install)
		[ "$verbose" != "yes" ] || echo "selected action = $ACTION"

		get_installed_version
		[ "$verbose" != "yes" ] || echo "installed version = $installed"

		get_upstream_version
		[ "$verbose" != "yes" ] || echo "upstream version = $upstream"

		if [ "$installed" != "" -a "$upstream" != "" -a "$installed" = "$upstream" ]
		then

			[ "$verbose" != "yes" ] || echo "upstream version $upstream is already installed"

		else

			wgetoptions="$wgetquiet $wgetalways"
			[ "$verbose" != "yes" ] || wgetoptions="$wgetalways $wgetprogress"
			[ "$fast" != "yes" ] || wgetoptions="$wgetoptions $wgetfast"
			[ "$verbose" != "yes" ] || echo "wgetoptions=$wgetoptions"

			[ "$verbose" != "yes" ] || echo "downloading $url..."

			HOME=/root \
			wget $wgetoptions $url \
				|| die_hard_with_a_cleanup "wget failed to download $downloadurl"

			targetdir=/usr/lib/openrocket

			[ "$verbose" != "yes" ] || echo "removing old .jar files from $targetdir ..."
			rm -f $targetdir/*.jar

			[ "$verbose" != "yes" ] || echo "moving download to $targetdir ..."
			mv -f download $targetdir/OpenRocket-$upstream.jar

			[ "$verbose" != "yes" ] || ( get_installed_version && echo "create /usr/bin/openrocket wrapper" )

			echo "#!/bin/sh" > /usr/bin/openrocket
			echo "exec java -jar /usr/lib/openrocket/OpenRocket-$upstream.jar \"\$@\"" >> /usr/bin/openrocket
			chmod +x /usr/bin/openrocket

			[ "$verbose" != "yes" ] || ( get_installed_version && echo "OpenRocket version: $installed" )

		fi # end if installed != upstream

		[ "$verbose" != "yes" ] || echo "end of action $ACTION"

		;;

	--uninstall)
		[ "$verbose" != "yes" ] || echo "selected action = $ACTION"

		[ "$verbose" != "yes" ] || echo "removing files ..."
		rm -f /usr/lib/openrocket/*.jar

		[ "$verbose" != "yes" ] || echo "end of action $ACTION"

		;;

	--status)
		[ "$verbose" != "yes" ] || echo "selected action = $ACTION"

		get_installed_version
		echo "OpenRocket version installed on this system  : $installed"
		get_upstream_version
		echo "OpenRocket version available on upstream site: $upstream"

		[ "$verbose" != "yes" ] || echo "end of action $ACTION"

		;;

	*)

		do_cleanup
		show_usage

		;;

esac

do_cleanup

[ "$verbose" != "yes" ] || echo "end of update-openrocket"

