#!/usr/bin/perl
# -*- Mode: cperl -*-
#--------------------------------------------------------------------
# Copyright (C) 2000, 2001 by MandrakeSoft.
# Chmouel Boudjnah <chmouel@mandrakesoft.com>.
#
# Redistribution of this file is permitted under the terms of the GNU 
# Public License (GPL)
#--------------------------------------------------------------------
# $Id: make-initrd,v 1.6 2001/10/15 11:42:22 chmouel Exp $
#--------------------------------------------------------------------
## description: 
#	      Generate an initrd.

use strict;

#define 0 here if you don't want link generated
my $link = 1;

# define 1 here if you want debugging.
my $debug = 0;

while ($ARGV[0] =~ /^--/ || $ARGV[0] =~ /^-/) {
    $_ = shift;
    if (/^--nolink/ or /^-n/) {
	undef $link;
    }
}

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

my $link_initrd;
if ($version =~ /^2\.2\./ ) {
    $link_initrd = "/boot/initrd-2.2.img";
} elsif ($version =~ /^2\.2\..*secure$/ ) {
    $link_initrd = "/boot/initrd-2.2-secure.img";
} elsif ($version =~ /^2\.2\..*smp$/ ) {
    $link_initrd = "/boot/initrd-2.2-smp.img";
} elsif ($version =~ /smp/ ) {
    $link_initrd = "/boot/initrd-smp.img";
} elsif ($version =~ /secure/ ) {
    $link_initrd = "/boot/initrd-secure.img";
} elsif ($version =~ /enterprise/ ) {
    $link_initrd = "/boot/initrd-enterprise.img";
} else {
    $link_initrd = "/boot/initrd.img";
}

debug("generating initrd");
if ( -d "/lib/modules/$version/" ) {
    system("/sbin/mkinitrd -f --ifneeded /boot/initrd-$version.img $version 2>/dev/null >/dev/null");
} else {
    print("There is no /lib/modules/$version, skipping creation of initrd\n");
}

if ( -f "/boot/initrd-$version.img") {
    debug("doing the symlink");
    my_symlinkf("initrd-$version.img", "$link_initrd") if $link;
} else {
    debug("no need to do any symlink, no initrd generated");
}

sub debug { print @_, "\n" if $debug;}
sub my_symlinkf { unlink $_[1]; symlink $_[0], $_[1] }
