#!/usr/bin/perl
##
## autokernconf is Copyright 1997 by Brandon Gillespie, All Rights Reserved.
##

$sys_base = "/sys";
$sys_conf = "i386/conf";

if (!-d "/usr/src" || !-d $sys_base) {
    &abort("Kernel source not installed, install 'sys' source package");
}

##############################################################
$interactive = 1;
$name = "";
$output = "";

while ($#ARGV != -1) {
    $_ = shift(@ARGV);
    if (/^-c(.*)$/) {
        $output = $2 || shift(@ARGV);
        (!$output && &abort_usage("No argument given with -c option."));
        @config = split(/\//, $output);
        $config = $config[$#config];
        if ($config =~ /[^A-Z]/) {
            $tmpid = $config;
            $tmpid =~ tr/[a-z]/[A-Z]/;
            $tmpid =~ s/[^A-Z]+//g;
            chop($err = <<END);
** The kernel config file name may only contain capitalized alphabetic
** characters.  Try '$tmpid' instead of '$config'.
END
            &error($err);
            exit;
        }
        if (!open(TMP, ">$output")) {
            &abort("Unable to write to $output: $!\n");
        }
        close(TMP);
    } elsif (/^-a/) {
        $interactive = 0;
    } else {
        &abort_usage("Unknown option '$_'\n");
    }
}

(!$output) &&
    &abort_usage("No config file specified");

## ok, later, we can come back and do something cool by generating a core
## config based off the dmesg output, followed by interactive questions
## but for now, lets just copy the GENERIC template 8)
$tmpl = "$sys_base/$sys_conf/GENERIC";
if (!-f $tmpl) {
    &abort("GENERIC config file template is missing!");
}

if ($interactive) {
    print "Interactive option to $0 not yet implemented.\n";
    sleep 1;
}

$sed = "sed -e 's/^ident[ \t][ \t]*GENERIC[ \t]*\$/ident\t\t$config/'";
system("$sed $tmpl > $output") && exit;

sub abort {
    print("abort: $_[0]\n");
    exit(1);
}

sub abort_usage {   
    &usage();     
    &error("** $_[0]");
    print "\n";   
    exit(1);      
}   

sub error {       
    print STDERR "$_[0]\n";
}   

sub usage {
    print STDERR <<END;

Syntax: autokernconf -c path [-a]

Options:

     -c path  Specify output configuration file and ID.
     -a       Automatically generate configuration defaults (non interactive)

See Also:

    doconfig(8), autokernconf(8), config(8)

END

}

