#!/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/195"
	DOWNLOADMAIN="el_195_1_data_files.zip"
	MD5SUMMAIN="e42ebe628e704c8f7e0b21e3340f475d"
	# 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_170_linux.zip* el_190_rc2_extra.zip* el_190_rc2.zip* el_update_180.zip* \
		el_update_191_rc*.zip* EternalLands-update_191_2.zip* el_192_rc2.zip \
		el_linux_193.zip el_linux_190_update.zip el_linux_update_191.zip \
		el_linux_update_192.zip el_linux_update_193*.zip EL-1.9.4-all-datafiles.zip \
		el_data_update_1.9.4-2.zip eternallands-data_1.9.4-1.zip el_195_data_files.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
