#!/usr/bin/perl
# -*- Mode: cperl -*-
#--------------------------------------------------------------------
# Copyright (C) 2000, 2001, 2002 by MandrakeSoft.
# Chmouel Boudjnah <chmouel@mandrakesoft.com>.
#
# Redistribution of this file is permitted under the terms of the GNU
# Public License (GPL)
#--------------------------------------------------------------------
# $Id: lilo,v 1.32 2004/02/27 07:51:33 tvignaud Exp $
#--------------------------------------------------------------------
## description:
#	      Add/check entry for lilo bootloader.

use strict;
use lib qw(/usr/share/loader/);
use common;

my ($version, $nolaunch, $remove, $blank);

my $debug = 0;

{
    local $_;
    while ($ARGV[0] =~ /^-/) {
        $_ = shift;
        if (m/^-n/) {
            $nolaunch++;
        } elsif (m/^-r/) {
            $remove++;
        } else {
	die "Unknown option $_";
        }
    }
}

unless ($version = shift) {
    print STDERR "No kernel version has been given using the current\n";
    $version = `uname -r`;chomp $version;
}

my $glabel = common::sanitize_ver(${version});

my $lilo_conf = $ENV{LILO_CONF} || "/etc/lilo.conf";

# we keep entry as hash for future use.
my (%main, %entry);
my ($label, $image);
my ($initrd, $root, $vga);

(my $options = common::getoptions('1'));$options = join(" ", split " ",$options); #Remove the unused space
my $root_device = common::getroot();

`cp -f $lilo_conf ${lilo_conf}.old` if -f $lilo_conf && !$debug;

#first we parse the files and get all entry.
{
    my $F;
    open $F, $lilo_conf or die "Can't open $lilo_conf\n";
    local $_;
    while (<$F>) {
        $blank = "\n" if eof && !/^\s*$/;

        next if /^\s*#/;

        $main{default} = $1 if m/^default="?([^\t\n "]+)"?/;
        $main{vga} = $1 if m/^vga="?([^\t\n "]+)"?/;

        $initrd = $1 if /initrd="?([^\t\n "]+)"?/;
        $root = $1 if /root="?([^\t\n "]+)"?/;
        $vga = $1 if /vga="?([^\t\n "]+)"?/;

        if (/^(image|other)=/ || eof) {
            $entry{$label}{initrd} ||= $initrd;
            $entry{$label}{root} ||= $root;
            $entry{$label}{vga} ||= $vga;
            undef $initrd;undef $root;
        }

        $image=$1 if /^image="?([^\t\n "]+)"?/;
        if (/label="?([^\t\n "]+)"?/) {
            $label=$1;
            $entry{$label}{label}=$label;
            $entry{$label}{kernel}=$image;
            $entry{$label}{initrd}=$initrd;
            $entry{$label}{vga}=$vga;
            $entry{$label}{root}=$root;
        }
    }
    close $F;
}

(my $boot_image = $1) = common::cat_("/proc/cmdline") =~ /BOOT_IMAGE=(\S+)/;
if ($options =~ /vga=(\S+)/) {
    $vga = $1; $options =~ s/vga=(\S+)//;
} elsif ($entry{$boot_image}{vga}) {
    $vga = $entry{$boot_image}{vga};
}

common::check_default_entry(\%entry, \%main);
common::do_check($entry{$glabel}{label}, $version, $remove) unless $debug;

if ($remove) {

    my $tmp = $debug ? "true" : "mktemp";
    my $output = `$tmp /tmp/.lilo.XXXXXX`;chomp($output);
    local $/; local *F; local *O;

    if (-l $entry{$main{default}}{kernel}) {
	my $resolved_link = readlink($entry{$main{default}}{kernel});
	my $type = common::get_kernel_type($entry{$main{default}}{kernel});

	if ($resolved_link =~ m|vmlinuz-$version|) {

	    my $first_kernel = common::get_first_boot($type, $resolved_link);

	    system("ln -fs vmlinuz-$first_kernel /boot/vmlinuz$type");
	    system("ln -fs initrd-$first_kernel.img /boot/initrd$type.img") if -f "/boot/initrd$type.img";

	}

    }

    open F, $lilo_conf;
    open O, ">$output";
    select O unless $debug;

    local $_;
    while (<F>) {
	if (m!image=/boot/vmlinuz-$version.*label=$glabel.*?(?=(image|other|$))!s) {
	    if (m!image=/boot/vmlinuz-$version.*(image|other)=!s) {
             s!image=/boot/vmlinuz-$version.*?(?=(image|other))!!s;
	    } else {
             s|image=/boot/vmlinuz-$version.*||s;
	    }
	}
	print;
    }
    close F;
    close O;
    select STDOUT;
    system("mv -f $output $lilo_conf") unless $debug;
} else {
    local *F;
    unless ($debug) {
	open F, ">>$lilo_conf" or die "Can't write to $lilo_conf\n";
	select F;
    }
    print "\n" if $blank;

    my $lock;
    my $version_default = -l $entry{$main{default}}{kernel} ?
      basename(readlink($entry{$main{default}}{kernel})) :
	basename($entry{$main{default}}{kernel});
    $version_default =~ s|vmlinuz-||;
    my $label_default = common::sanitize_ver($version_default);
    while (my $act = each %entry) {
	$lock++ if $entry{$act}{label} =~ $label_default;
    }
    if (!$lock && -f "/boot/vmlinuz-$version_default" && $version !~ $version_default) {
	print <<EOF;
image=/boot/vmlinuz-$version_default
	label=$label_default
	$root_device
	read-only
	optional
EOF

	print "\tvga=$vga\n" if $vga;
	print qq(\tappend=" $options"\n) if $options;
	print "\tinitrd=/boot/initrd-$version_default.img\n" if -f "/boot/initrd-$version_default.img";
	print "\n";
    }
	print <<EOF;
image=/boot/vmlinuz-$version
	label=$glabel
	$root_device
	read-only
	optional
EOF

    print "\tvga=$vga\n" if $vga;
    print qq(\tappend=" $options"\n) if $options;
    print "\tinitrd=/boot/initrd-$version.img\n" if -f "/boot/initrd-$version.img";
    unless ($debug) {
	close F;
	select STDOUT;
    }
}

unless ($debug) {
    `/sbin/lilo >/dev/null 2>/dev/null` unless $nolaunch;
}
sub basename { local $_ = shift; s|/*\s*$||; s|.*/||; $_ }

die "There is an error when regenerating lilo, you may have to check your $lilo_conf \n" if $?;
