#!/bin/sh
# postinst script for eternallands-music
#
# 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/music"
	CACHEDIR="/var/cache/eternallands"
	DOWNLOADURL="http://twinmoons.org.uk/el/rel/195"
	DOWNLOADMAIN="eternallands-music_1.9.5.9.zip"
	MD5SUM="95220e0df0be0eb4f2b9646cc5d6b7e5"
	[ -f ~paul/el/installpkg/localdownload ] && DOWNLOADURL="http://harry/~paul/el"

	mkdir -p $CACHEDIR
	cd $CACHEDIR

	# remove old downloads
	rm -f music_full.zip

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

	# download the archive if we don't already have a copy, checking m5again
	if [ ! -f $DOWNLOADMAIN ]
	then
		wget -P "." -v --progress="dot:mega" $DOWNLOADURL/$DOWNLOADMAIN
		echo "$MD5SUM  $DOWNLOADMAIN" | md5sum -c -
	fi

	UNPACKDIR="`mktemp -d /tmp/eternallands-music.XXXXXXXXXX`"

	# unpack the archive
	cd $UNPACKDIR
	mkdir music
	cd music
	unzip -q $CACHEDIR/$DOWNLOADMAIN

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

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

	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
