#!/bin/sh
# postinst script for drnoksnes
#
# see: dh_installdeb(1)

set -e

DUNE2_DATA=http://devs.opendune.org/~truebrain/releases/opendune-data.tar.bz2

case "$1" in
    configure)
	if [ ! -L /opt/maemo/usr/share/games/opendune/data ]; then
		mkdir -p /home/user/MyDocs/Games/DUNE2
		mv -f /opt/maemo/usr/share/games/opendune/data/* /home/user/MyDocs/Games/DUNE2/ || true
		rmdir /opt/maemo/usr/share/games/opendune/data
		ln -s /home/user/MyDocs/Games/DUNE2 /opt/maemo/usr/share/games/opendune/data
	fi

	if [ ! -r /home/user/MyDocs/Games/DUNE2/scenario.pak ]; then
		DATAFILE="$(basename $DUNE2_DATA)"
		cd /home/user/MyDocs/Games/DUNE2
		rm -f $DATAFILE

		wget $DUNE2_DATA 2>&1 \
		| sed 's/.*\ \([0-9]\+%\)\ \+\([0-9.]\+\ [KMB\/s]\+\)$/\1\n# Downloading \2/' \
		| zenity --progress --auto-close --auto-kill --title="Downloading Dune II Data files..." &

		#Start a loop testing if zenity is running, and if not kill wget
		RUNNING=0
		while [ $RUNNING -eq 0 ]
		do
		if [ -z "$(pidof zenity)" ]
		then
		  pkill wget | :
		  RUNNING=1
		fi
		sleep 1
		done

		bzcat $DATAFILE | tar x
		if [ ! -r opendune-data/data/scenario.pak ]
		then
			zenity --info --text="Downloading Dune II Data files failed."
			exit 1
		fi
		
		mv opendune-data/data/* ./
		rm -rf opendune-data $DATAFILE
	fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \$1" >&2
        exit 1
    ;;
esac

#DEBHELPER#

exit 0

