#!/bin/sh
# -*- Mode: shell-script -*-
# $Id: make-initrd,v 1.13 2002/01/24 16:50:01 chmouel Exp $
# Copyright (C) 2000, 2001, 2002 by Chmouel Boudjnah <chmouel@mandrakesoft.com>
# Redistribution of this file is permitted under the terms of the GNU 
# Public License (GPL)

INITRDOPTS=""
[ -f /etc/sysconfig/installkernel ] && . /etc/sysconfig/installkernel

link=yes
#debug="-v"

version=
link_initrd=
status=

while getopts n opt
  do
  case "$opt" in
      n) link=;;
      *) echo "Unknow option $opt\n";;
  esac
done
shift $((OPTIND - 1))

version=$1

[[ -z $version ]] && {
    echo "No kernel version has been given using the current" 1>&2
    version=$(uname -r)
}

case $version in
    2.2*linus) link_initrd=initrd-2.2.linus.img;;
    2.2*secure) link_initrd=initrd-2.2.secure.img;;
    2.2*smp) link_initrd=initrd-2.2.smp.img;;
    2.2*) link_initrd=initrd-2.2.img;;
    2.4*linus) link_initrd=initrd-linus.img;;
    2.4*mosix) link_initrd=initrd-mosix.img;;
    2.4*smp) link_initrd=initrd-smp.img;;
    2.4*secure) link_initrd=initrd-secure.img;; #not existing at this time
    2.4*enterprise) link_initrd=initrd-enterprise.img;;
    2.4*BOOT) link_initrd=initrd-BOOT.img;;
    *) link_initrd=initrd.img;;
esac

if [[ -d /lib/modules/$version/ ]];then
    mkinitrd -f $debug --ifneeded /boot/initrd-$version.img $version  2>&1 >/dev/null
    mkinitrd -f $debug --ifneeded $INITRDOPTS /boot/initrd-$version.img $version  2>&1 >/dev/null
    status=$?
else
    echo "There is no /lib/modules/$version, skipping creation of initrd";
fi

if [[ -f /boot/initrd-$version.img ]];then
    if [[ -n $link ]];then
	ln -sf initrd-$version.img /boot/$link_initrd
    fi
else
    if [[ -n $link ]] && [[ $status != 0 ]];then
	echo "There was an error when generating initrd try to do a :"
	echo "/sbin/mkinitrd /boot/initrd-$version.img $version"
	echo "and see the errors"
    fi
fi
