#!/bin/sh
# postinst script for eternallands-sound
#
# 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/sound"
	CACHEDIR="/var/cache/eternallands"
	DOWNLOADURL="http://twinmoons.org.uk/el/rel/196"
	DOWNLOADMAIN="eternallands-sound_1.9.6.1.zip"
	MD5SUMMAIN="59e055a191fc58163ddab81ba5615d2a"

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

	mkdir -p $CACHEDIR
	cd $CACHEDIR

	# remove old downloads
	rm -f el_sound_*.zip EL_sound_* eternallands-sound_1.9.4*.zip eternallands-sound_1.9.5*.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

	# 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

	# unnpack the archive
	UNPACKDIR="`mktemp -d /tmp/eternallands-sound.XXXXXXXXXX`"
	cd $UNPACKDIR
	unzip -q $CACHEDIR/$DOWNLOADMAIN

	# set the permissions
	cd sound
	chown -R root:root .
	chmod -R a=rX .

	# make the file lists
	FILELIST="eternallands-sound.filelist"
	DIRLIST="eternallands-sound.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/sound/* .

	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
