#!/usr/bin/perl
# $Id: lilo-ins.pl,v 1.12 2000/01/31 14:58:42 duwe Exp $

$nam = $insimage = $ARGV[0];
$nam =~ s/\.//g;

open (ELC, "</etc/lilo.conf") || die;

# Determine if this is a true Pentium or better and we can use that kernel,
# and if we need to disable SMP (for AMD, Cyrix, ...). Set failsafe defaults.
$apic = " noapic nosmp";
$pc97 = "";
if (open(CI, "</proc/cpuinfo")) {
  while(<CI>) {
    m/^\s*flags\s*:.* fpu\b.* tsc\b/ && ($pc97 = "-pc97");
    m/^\s*flags\s*:.* apic\b/ && ($apic = "");
  }
  close CI;
}

# Now let's see whether we need an initrd for the current root.
$thisroot = `/usr/sbin/rdev`;
$thisroot =~ s/\s.*$//sm;
$initrd = "\tinitrd = /boot/initrd-$insimage.gz\n";
($thisroot =~ m,^/dev/hd, ) && ( $initrd = "" );

$secret = "no";
$otherimg = "";
# Seek forward to just before the first "image=" entry (if there is one)
while (<ELC>) {
  if (m/^\s*password\s*=/i) { $secret = "yes"; }
  if (m/^\s*image\s*=/) { $otherimg = $_; last; }
  print;
}

$vga = "";
if (open(ESC, "</etc/system.cnf")) {
  while(<ESC>) {
    m/^\s*CONF_GRAPHIC_CARD1_VESAMODE\s*=\s*(\w+)/ && ($vga = "$1");
  }
  close ESC;
}
$vga = "274" if ($vga eq "");
print "image = /boot/vmlinuz$pc97-$insimage-modular\n";
print "\tlabel  = linux\n";
print "\troot   = $thisroot\n" unless ($thisroot eq "");
print "\tvga    = $vga\n";
print "\tread-only\n";
print "\tappend = \"debug=2$apic\"\n$initrd\n";

# and now dump the remaining input file
print $otherimg;
while (<ELC>) {
  if (m/^\s*password\s*=/i) { $secret = "yes"; }
  s/(label|alias)\s*=\s*oldlinux\b/\1 = cruft$$/; # the best we can do
  s/(label|alias)\s*=\s*linux\b/\1 = oldlinux/;
  print;
}
close ELC;

chmod 0600, "/proc/self/fd/1" unless ($secret eq "no");
