#!/usr/bin/perl
#
# By: Joey Hess <joeyh@master.deb.org>
# Copyright: GPL

$|=1;

sub Error {
	print STDERR shift;
	print STDERR<<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;
}

$SIG{'INT'} = "Error";

print "Quake library configuration\n\n";

# The old version of this package used a symlink to point to the quake libs. 
# If that's still hanging around, use it.
if (-l "/var/lib/games/quake/id1") {
	$quakedir_old=readlink('/var/lib/games/quake/id1');
}
elsif (-l "/var/lib/games/quake/id1.old") {
	$quakedir_old=readlink('/var/lib/games/quake/id1.old');
}
$quakedir_old=~s:/id1$::g;

# Now get rid of the symlinks we used to have.
foreach $file ('/var/lib/games/quake/id1','/var/lib/games/quake/id1.old') {
        if (-l $file) {
                print "Removing old symlink $file\n";
                if (unlink($file) eq 0) {
                        print "Problems removing the symlink: $!\n";
                }
		print "\n";
        }
}

# Now we prefer to use the /etc/quake.conf file. Read it and get the quake
# lib dir out of it. Remember the contents of the file.
if (-r "/etc/quake.conf") {
	open (CONF,"</etc/quake.conf") || Error("Can't read config file /etc/quake.conf: $!\n");
	while (<CONF>) {
		$conflines[$#conflines+1]=$_; 
		if (m/^quakedir\s*?=\s*?(.*)$/ ne undef) {
			$quakedir=$1;
		}
	}
	close CONF;
}
else {
	# Couldn't read the conf file, so we will create one instead.
	@conflines=(
		"# Quake library configuration file.\n",
		"#\n",
		"# You may edit this file directly, or run /usr/sbin/quake-lib-config for an\n",
		"# interactive configuration.\n",
		"#\n",
		"# The following line determines where the quake libararies are installed.\n",
		"# The direcotry listed here should have an id1/ subdirectory, with the\n",
		"# various quake library files under that.\n",
		"\n",
		"quakedir=/var/lib/games/quake\n");
	$quakedir='/var/lib/games/quake';
}

# See if the conf file just has the default value in it.
if ($quakedir eq '/var/lib/games/quake' && $quakedir_old) {
	# Let's use the value from the symlink as our default.
	$quakedir=$quakedir_old;
}
else {
	# Use the value from the conf file as our default.
	$quakedir_old=$quakedir;
}

print <<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 have 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

while (!$ok) {
	print "Dos Quake is in ";
	if ($quakedir_old) { print "[$quakedir_old]" }
	print ": ";
	$quakedir=<>;
	chomp $quakedir;
	print "\n";

	if (!$quakedir) { $quakedir=$quakedir_old }
	$quakedir_old=$quakedir;

	if (!-d $quakedir) {
		print <<eof;
The directory you entered does not exist. Try again or press
ctrl-c to cancel.

eof
	}
	elsif ((-d $quakedir) && (!-d "$quakedir/id1")) {
		print <<eof;
The directory you entered does not have a subdirectory named id1.
Quake is probably not installed there; if it is, I don't know how to use it.
Try again or press ctrl-c to cancel.

eof
	}
	else {
		$ok=1;
	}
}

print "$quakedir looks good. Saving config file /etc/quake.conf ..\n";
open (CONF,">/etc/quake.conf") || Error("Unable to save to config file: $!\n");
foreach (@conflines) {
	if (m/^quakedir\s*?=\s*?(.*)$/ ne undef) {
		$_="quakedir=$quakedir\n";
	}
	print CONF $_;
}
close CONF;

print <<eof;

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

eof
