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

# die if things go wrong
set -e

# source the debconf library
. /usr/share/debconf/confmodule

sharefile=q2-314-demo-x86.exe
sharemd5="4d1cd4618e80a38db59304132ea0856c"
pointfile=q2-3.20-x86-full-ctf.exe
pointmd5="490557d4a90ff346a175d865a2bade87"

sharepakmd5="27d77240466ec4f3253256832b54db8a"
cdrompakmd5="1ec55a724dc3109fd50dde71ab581d70"
pointpakmd5="42663ea709b7cd3eb9b634b36cfecb1a"

BASE=/usr/lib/games/quake2/baseq2
DOC=/usr/share/doc/quake2-data
CTF=/usr/lib/games/quake2/ctf

inst() {
	_FILE=$1
	_DEST=$2

	# check if the source is on the cdrom
	db_get quake2-data/cdromdir
	_CDROMDIR="$RET"
	_PANTS=`echo $_FILE | sed "s#$_CDROMDIR##"`
	_SRCDIR=`echo $_FILE | sed "s#$_PANTS##"`

	if [ -e "$_FILE" ]; then 
		_DESTFILE="$_DEST/`basename $_FILE`"
		if [ "$NOCOPY" = "false" -a "$_CDROMDIR" = "$_SRCDIR" ]; then
			# check that we havent already installed it
			l=`readlink $_DESTFILE || true`
			if [ "$l" != "$_FILE" ]; then
				ln -sf $_FILE $_DESTFILE
			fi
		else
			# check that we haven't already installed it
			_SRCSUM=`getsum $_FILE`
			_DESTSUM=`getsum $_DESTFILE`
			if [ "$_SRCSUM" != "$_DESTSUM" ]; then
				install -p -m 644 -o root -g games $_FILE $_DESTFILE
			fi
		fi
	fi
}

download() {
	#wget --directory-prefix $STORAGE -c $mirror$name
	_STORAGE=$1
	_FILE=$2
	db_get quake2-data/mirror
	_MIRROR="$RET"
	echo "Downloading $_FILE from $_MIRROR..."
	wget --dot-style=mega --directory-prefix $_STORAGE -c $_MIRROR$_FILE
	echo "done."
}

do_install() {
	_NAME=$1
	_SRCBASE=$2
	_SRCDOC=$3
	_SRCCTF=$4
	
	echo "Installing data from $_NAME"

	# base files
	if [ -n "$_SRCBASE" -a -d "$_SRCBASE" ]; then
		for i in $_SRCBASE/*.pak $_SRCBASE/maps.lst ; do
			inst $i $BASE
		done
	fi

	# capture the flag
	if [ -n "$_SRCCTF" -a -d "$_SRCCTF" ]; then
		for i in $_SRCCTF/*.pak ; do
			inst $i $CTF
		done
		install -p -m 644 $_SRCCTF/readme.txt $DOC/README.ctf
	fi

	# full motion videos
	if [ -n "$_SRCBASE/video" ]; then
		for i in $_SRCBASE/video/*.cin ; do
			inst $i $BASE/video
		done
	fi
	
	# documentation
	if [ -n "$_SRCDOC" -a -d "$_SRCDOC" ]; then
		for i in $_SRCDOC/*.txt ; do
			inst $i $DOC
		done
		mv $DOC/readme.txt $DOC/README

		if [ -d "$_SRCDOC/quake2_manual" -a ! -e "$DOC/quake2_manual" ]; then
			# only install the manual once
			inst $_SRCDOC/Manual.html $DOC
			install -d -m 755 $DOC/quake2_manual
			install -d -m 755 $DOC/quake2_manual/images
			for i in $_SRCDOC/quake2_manual/*.html ; do
				inst $i $DOC/quake2_manual
			done
			for i in $_SRCDOC/quake2_manual/images/*.gif ; do
				inst $i $DOC/quake2_manual/images
			done
		fi
	fi

	# player models
	for i in `find $_SRCBASE/players -type d -mindepth 1 | xargs -n 1 basename` ; do
		install -d -m 755 -o root -g games $BASE/players/$i
		for j in `find $_SRCBASE/players/$i -type f | sed 's/^.*players\///'` ; do
			inst $_SRCBASE/players/$j $BASE/players/$i
		done
	done
}

tempdir() {
	_TEMPDIR=`tempfile --directory $1 --prefix quake2-data`
	# kill off fresh tempfile
	rm $_TEMPDIR
	mkdir $_TEMPDIR
	echo $_TEMPDIR
}

validmd5() {
	_FILE=$1
	_SUM=$2

	check=`getsum $_FILE`
	if [ "$check" = "$_SUM" ]; then
		echo true
	else
		echo false
	fi
}

getsum() {
	_FILE=$1

	if [ -e "$_FILE" ]; then
		md5sum $_FILE | cut -f1 -d' '
	fi
}

pushdir() {
	OLDDIR=`pwd`
	cd $1
}

popdir() {
	cd $OLDDIR
}

# versions <= -5 had a bug (#128934) in the player model install
# because it overwrote file data, it's easier just to nuke this lot
# and install again
clean_players() {
	find $BASE/players -name '*.md2' -o -name '*.pcx' -print0 | xargs -0 rm
}

# wrapper around dpkg --compare-versions to get the FUCKING RETURN CODE


# 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>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see /usr/share/doc/packaging-manual/
#
# quoting from the policy:
#     Any necessary prompting should almost always be confined to the
#     post-installation script, and should be protected with a conditional
#     so that unnecessary prompting doesn't happen if a package's
#     installation fails and the `postinst' is called with `abort-upgrade',
#     `abort-remove' or `abort-deconfigure'.

case "$1" in
    configure)
		# if the old version is 5 or lower, then clean up the player models
		if dpkg --compare-versions "$2" lt-nl 6 ; then
			clean_players
		fi

		# first decide if we're going to install the full or shareware version

		# if the first question wasn't answered, then die.
		db_fget quake2-data/instmethod seen
		if [ "$RET" = "false" ]; then
			exit
		fi

		db_get quake2-data/instmethod
		INSTMETHOD="$RET"

		db_get quake2-data/copyfiles
		NOCOPY="$RET"

		DOWNLOAD=0
		
		if [ "$INSTMETHOD" = "download shareware data" ]; then

		    # shareware install

			# check if it's already installed

			VALID=`validmd5 $BASE/pak0.pak $sharepakmd5`
			if dpkg --compare-versions "$2" ge-nl 6 && [ "$VALID" = "true" ]; then
				echo "Shareware data already installed."
			else
			
				db_get quake2-data/storage
				STORAGE="$RET"

				if [ -z "$STORAGE" ]; then
					STORAGE="/root"
				fi

				#get shareware data
				VALID=`validmd5 $STORAGE/$sharefile $sharemd5`
				if [ ! -e "$STORAGE/$sharefile" -o "$VALID" = "false" ]; then
					download $STORAGE $sharefile
					DOWNLOAD=1
				fi
				
				#get a suitable tmpdir
				SRCDIR=`tempdir $STORAGE`
				#unpack the shareware data
				pushdir $SRCDIR
				unzip $STORAGE/$sharefile > /dev/null
				#copy data to dest dir
				do_install $sharefile $SRCDIR/Install/Data/baseq2 $SRCDIR/Install/Data/DOCS ""
				#clean up tmpdir
				popdir
				rm -r $SRCDIR

			fi # already installed
			
		elif [ "$INSTMETHOD" = "install from CD-ROM" ]; then

			# cdrom install
			
			db_get quake2-data/cdromdir
			CDROMDIR="$RET"

#FIXME: check that cdrom is mounted

			# check if it's already installed
			VALID=`validmd5 $BASE/pak0.pak $cdrompakmd5`
			if [ "$VALID" = "true" ]; then
				echo "CD-ROM data already installed."
			else

				#install data -- check for case of directories
				if [ -d "$CDROMDIR/dnstall/data/baseq2" ]; then
					do_install "CD-ROM" $CDROMDIR/install/data/baseq2 $CDROMDIR/install/data/docs ""
				else
					do_install "CD-ROM" $CDROMDIR/Install/Data/baseq2 $CDROMDIR/Install/Data/DOCS ""
				fi
			fi

			#do pointrelease
			db_get quake2-data/pointrelease
			POINTRELEASE="$RET"

			if [ "$POINTRELEASE" = "true" ]; then

				# check if it's alredy installed
				VALID=`validmd5 $BASE/pak1.pak $pointpakmd5`
				if [ "$VALID" = true ]; then
					echo "Point release already installed"
				else

					db_get quake2-data/storage
					STORAGE="$RET"

					if [ -z "$STORAGE" ]; then
						STORAGE="/root"
					fi

					#get pointrelease data
					VALID=`validmd5 $STORAGE/$pointfile $pointmd5`
					if [ ! -e "$STORAGE/$pointfile" -o "$VALID" = "false" ]; then
						download $STORAGE $pointfile
						DOWNLOAD=1
					fi

					#get a suitable tmpdir
					SRCDIR=`tempdir $STORAGE`
					#unpack the shareware data
					pushdir $SRCDIR
					unzip $STORAGE/$pointfile > /dev/null
					#copy data to dest dir
					do_install $pointfile $SRCDIR/baseq2 $SRCDIR/DOCS $SRCDIR/ctf
					#clean up
					popdir
					rm -r $SRCDIR

				fi # already installed
			fi
		fi

		#keep a record of installation
		#echo "OLDDOWNLOAD=$DOWNLOAD" > /etc/quake2-data.conf
		#echo "OLDINSTMETHOD=\"$INSTMETHOD\"" >> /etc/quake2-data.conf
    ;;

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

    ;;

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

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

#DEBHELPER#

exit 0


