#!/bin/sh -e

# ugly perl hack that returns what a symlink actually points to
readlink () {
        perl -e '
		$l = shift; 
		exit 1 unless -l $l; 
		$r = readlink $l;
		exit 1 unless $r;
		print "$r\n"
	' $1;
}

# doogie's idea - why didn't I think of this?
strip_multi_slash () {
	sed 's,//*,/,g;s,/$,,'
}

shquakedir=/usr/share/games/quake
id1dir=$shquakedir/id1

if [ "x$1" = "xupgrade" ]; then
	if [ "x$2" != "x" ] && dpkg --compare-versions $2 lt 1.9-8; then
		if [ "x$(readlink $id1dir|strip_multi_slash)" = \
				"x$(echo "$id1dir"|strip_multi_slash)" ]; then
			rm $id1dir
		fi
	fi
	if [ -e "/etc/quake.conf" -a ! -e "$id1dir" ]; then
		. /etc/quake.conf
		quakedir=$(echo "$quakedir"|strip_multi_slash)
		if [ "x$quakedir" != x ]; then
			if [ "x$quakedir" != "x$shquakedir" ]; then
				mkdir -p $shquakedir
				ln -s $quakedir/id1 $id1dir
			fi
		fi
	fi
fi

#DEBHELPER#

