#!/usr/bin/perl
#
# /usr/sbin/quikconfig	configure quik automatically
# this program is a modified version of siloconfig
# (C) 1998 Davide Barbieri <paci@debian.org>
#
# based on liloconfig, which is copyrighted by
# Author:	Bruce Perens <bruce@Pixar.com>
# Maintainer:	Bernd Eckenfels <ecki@debian.org>
#
# quikconfig (C) 1998 Matt McLean <keys@yikes.com>
# this software is GPLed

$|=1;

if ( ! (-f "/etc/fstab") ) {
	exit(0);
}

print "QUIK, the Linux Loader for PowerMac and CHRP, sets up your system\n";
print "to boot directly from your hard disk, without the need for a boot\n";
print "floppy or a net boot.\n";

@boilerplate = (
	"# Generated by quikconfig\n",
	"# (C) 1998 Matt McLean <keys\@yikes.com>\n",
	"# partition=3\n",
	"# map=/boot/map\n",
	"timeout=20\n",
	"image=/boot/vmlinux\n",
	"label=Linux\n",
	"read-only\n"
);

sub asky {
	print @_,"? [Yes] ";
	$answer=<STDIN>;
	return ( !($answer =~ /^[nN].*/) );
}

sub askn {
	print @_,"? [No] ";
	$answer=<STDIN>;
	return ( $answer =~ /^[yY].*/ );
}

if (-T "/etc/quik.conf") {
	# Trust and use the existing quik.conf.
	print "\n";
	print "You already have a QUIK configuration in the file /etc/quik.conf\n";
	if ( &asky("Install a boot block using your current QUIK configuration") ) {
		print "Running quik\n";
		system("/sbin/quik");
		exit(0);
	}
	else {
		print "\n";
		if ( &askn("Wipe out your old QUIK configuration and make a new one") ) {
			print "Saving configuration in /etc/quik.conf.OLD\n";
			rename("/etc/quik.conf", "/etc/quik.conf.OLD");
		}
		else {
			print "No changes made.\n";
			exit(0);
		}
	}
}

# Create a quik.conf if one doesn't exist.
open(FSTAB, "</etc/fstab");
while ( $line=<FSTAB> ) {
	if ( $line =~ m/^\#/ ) {
		next;
	}
	($device,$filesystem)=split(/[ \t]+/, $line);
	if ( $filesystem =~ m:^/$: ) {
		last;
	}
}
if ( ! $filesystem =~ m:^/$: ) {
	exit(0);
}

$disk = $device;
$disk =~ s/[0-9]+$//;
$partition = $device;
$partition =~ s/$disk//;

if ( &asky("Install a partition boot record to boot Linux from ", $device)) {
        print "Creating small quik.conf and running quik.\n";
	umask(022);
	open(CONF, ">/etc/quik.conf");
	chown(0, 0, "/etc/quik.conf");
	print CONF "root=".$device, "\n", @boilerplate;
	close(CONF);
	system("/sbin/quik");
}

exit(0);
