#!/bin/sh -e

targetdir=/usr/share/games/quake/id1

error() {
	cat >&2 << EOF


Configuration canceled. Quake may not work.
You can reconfigure this package by running /usr/sbin/quake-lib-config
or editing /etc/quake.conf.

EOF
	exit 1
}

trap "error" 1 2 3 15

echo "Quake library configuration"

if test -d "$targetdir"; then
	cat << EOF
	
/usr/share/games/quake/id1 is a directory.  This probably means that you
have already installed Quake to that location.  Since that's exactly where
we want it, you're done before you've even started!

EOF
	exit 0
fi

if test -L "$targetdir"; then
	quakedir_old=`readlink /var/lib/games/quake/id1`
else
	if test -L "/var/lib/games/quake/id1.old"; then
		quakedir_old=`readlink /var/lib/games/quake/id1.old`
	fi
fi

for i in /var/lib/games/quake/id1 /var/lib/games/quake/id1.old; do
	if test -L $i; then
		echo "Removing old symlink $i"
		rm -f $i
	fi
done
if test -d /var/lib/games/quake/id1; then
	rmdir --ignore-fail-on-non-empty /var/lib/games/quake/id1
	rmdir --ignore-fail-on-non-empty /var/lib/games/quake
fi

if test -L "/usr/share/games/quake/id1"; then
	quakedir_old=`readlink /usr/share/games/quake/id1`
fi

cat << EOF

This quake-lib-stub package will configure linux Quake to use an 
already installed copy of dos Quake for its library files.

This will probably only work with dos Quake version 1.06 or a
registered Quake. If you have one of them installed on your computer,
then enter the directory where it can be found now. This directory
should be a subdirectory named "id1" with a "*.pak" file in it.

If you don't have dos Quake installed, you can press ctrl-c to cancel.

EOF

ok=""
while test "x$ok" = x; do
	echo -n "Quake is in"
	if test "x$quakedir_old" != x; then
		echo -n " [$quakedir_old]"
	fi
	echo -n ": "
	read quakedir
	
	if test "x$quakedir" = x -a "x$quakedir_old" != x; then
		quakedir="$quakedir_old"
	fi

	if test "x$quakedir" != x -a -d $quakedir; then
		if test -e "$quakedir/pak0.pak"; then
			ok=1
		else
			cat << EOF
The directory you entered does not contain pak0.pak.  This likely means you
do not have Quake installed in this directory.

EOF
			echo -n "Would you like to use it anyway? [y/N]: "
			read yesno
			case "$yesno" in
				y*|Y*)	ok=1 ;;
				*)	quakedir="" ;;
			esac
		fi
	else
		cat << EOF
The directory you entered does not exist. Try again or press
ctrl-c to cancel.

EOF
	fi
done

if test "$quakedir" = "$targetdir"; then
	echo "Cowardly refusing to symlink $targetdir to itself"
	echo
	echo "If your Quake library files are not installed in that dir, Quake"
	echo "won't run properly."
	echo
	echo "You can rerun $0 if you need to."
fi

echo "All right, $quakedir it is then."

rm -f $targetdir
install -d `dirname $targetdir`
ln -s $quakedir $targetdir
echo "Your Quake files have been symlinked to $targetdir"

cat << EOF

Quake should work now. If you change the location of your Quake files,
you can update the symlink yourself or rerun this program,
/usr/sbin/quake-lib-config, to do it for you interactively.

EOF

