#!/bin/sh
# postinst script for eternallands-data
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
	configure)

	TARGETDIR="/usr/share/games/EternalLands"
	CACHEDIR="/var/cache/eternallands"
	DOWNLOADURL="http://twinmoons.org.uk/el/rel/196"
	DOWNLOADMAIN="eternallands-data_1.9.6.1.zip"
	MD5SUMMAIN="f55207a3bb74c444eac1d6c186117629"
	# post release updates
	#DOWNLOADUPDATE1=""
	#MD5SUMUPDATE1=""

	[ -f ~paul/el/installpkg/localdownload ] && DOWNLOADURL="https://harry/~paul/el"

	mkdir -p $CACHEDIR
	cd $CACHEDIR

	# remove old downloads
	rm -f el_195_*.zip  el_data_*.zip eternallands-data_1.9.5*.zip eternallands-data_1.9.6.0.zip eternallands-data-patch*.zip

	# if we already have an archive check the md5sum is ok and remove if not
	[ ! -f "$DOWNLOADMAIN" ] || echo "$MD5SUMMAIN  $DOWNLOADMAIN" | md5sum -c - || rm -fv $DOWNLOADMAIN
	# post release updates
	#[ ! -f "$DOWNLOADUPDATE1" ] || echo "$MD5SUMUPDATE1  $DOWNLOADUPDATE1" | md5sum -c - || rm -fv $DOWNLOADUPDATE1

	# Use existing file or download it
	if [ ! -f "$DOWNLOADMAIN" ]
	then
		wget -P "." -v --progress="dot:mega" --no-check-certificate $DOWNLOADURL/$DOWNLOADMAIN
		echo "$MD5SUMMAIN  $DOWNLOADMAIN" | md5sum -c -
	fi
	# post release updates
	#if [ ! -f "$DOWNLOADUPDATE1" ]
	#then
	#	wget -P "." -v --progress="dot:mega" --no-check-certificate $DOWNLOADURL/$DOWNLOADUPDATE1
	#	echo "$MD5SUMUPDATE1  $DOWNLOADUPDATE1" | md5sum -c -
	#fi

	# unnpack the archive
	UNPACKDIR="`mktemp -d /tmp/eternallands-data.XXXXXXXXXX`"
	cd $UNPACKDIR
	unzip -q $CACHEDIR/$DOWNLOADMAIN
	# post release updates
	#cd el_data
	#unzip -q -o $CACHEDIR/$DOWNLOADUPDATE1

	# fix el.ini and server.lst, remove unneeded files and set the permissions
	cd $UNPACKDIR/el_data
	rm -f *.dll *.exe el.*.bin
	sed -i 's/^#data_dir.*$/#data_dir = \/usr\/share\/games\/EternalLands/g' el.ini
	sed -i 's/^#browser.*$/#browser = x-www-browser/g' el.ini
	sed -i 's/^#use_new_selection.*$/#use_new_selection = 1/g' el.ini
	grep -v ^official servers.lst > sl
	mv sl servers.lst
	chown -R root:root .
	chmod -R a=rX .

	# make the file lists
	FILELIST="eternallands-data.filelist"
	DIRLIST="eternallands-data.dirlist"
	find . -type f -exec echo \"{}\" \; > $TARGETDIR/$FILELIST
	find . -type d ! -name "." -exec echo \"{}\" \; | sort -r > $TARGETDIR/$DIRLIST

	# remove any existing files in the lists then move the new files into the target directory
	cd $TARGETDIR/
	[ ! -s $FILELIST ] || cat $FILELIST | xargs rm -f || true
	[ ! -s $DIRLIST ] || cat $DIRLIST | xargs rmdir --ignore-fail-on-non-empty > /dev/null 2>&1 || true
	chmod -R +w .
	cp --remove-destination -rpf $UNPACKDIR/el_data/* .

	rm -rf $UNPACKDIR

	;;

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

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

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
