#!/bin/sh
#--------------------------------------------------------------------
# Copyright (C) 2000 by MandrakeSoft,
# Chmouel Boudjnah <chmouel@mandrakesoft.com>, 
# Redistribution of this file is permitted under the terms of the GNU 
# Public License (GPL)
#--------------------------------------------------------------------
# $Id: installkernel,v 1.24 2001/10/05 08:20:57 chmouel Exp $
#--------------------------------------------------------------------
## description: 
#	      Install a kernel to /boot and add an entry for grub or lilo

sharfile=/usr/share/loader/

NOLAUNCH=""
NOCOPY=""
AUTODETECT="yes"
REMOVE=
NOLINK=""
LOADER=""
NOCONFIG=""
NOENTRY=""

[[ -n "$DURING_INSTALL" ]] && exit 0

if [ -f /etc/sysconfig/installkernel ];then
    source /etc/sysconfig/installkernel
fi

function copy_image () {
    local config=""
    if [[ -f .config ]];then
	config=.config
    elif [[ -f ../../../.config ]];then
	config=../../../.config
    fi

    if [ -f $boot/vmlinuz ]; then
	mv -f $boot/vmlinuz $boot/vmlinuz.old
    fi
    
    if [[ -z $NOCONFIG ]];then
	if [[ -n $config ]];then
	    cat $config > $boot/config-$version
	fi
    fi
    cat $boot_image > $boot/vmlinuz-$version
    cp -f $map_file $boot/System.map-$version
}

function do_link () {
    local extra=""

    case $version in
	*smp) extra="-smp";
	    ;;
	*secure) extra="-secure";
	    ;;
	*enterprise) extra="-enterprise";
	    ;;*)
    esac
    
    ln -fs vmlinuz-$version $boot/vmlinuz${extra}
}

function do_remove_link () {
    if [[ -L /boot/vmlinuz && ! -e /boot/vmlinuz ]];then
	rm -f /boot/vmlinuz
    fi
    if [[ -L /boot/vmlinuz-secure && ! -e /boot/vmlinuz-secure ]];then
	rm -f /boot/vmlinuz-secure
    fi 
    if [[ -L /boot/vmlinuz-smp && ! -e /boot/vmlinuz-smp ]];then
	rm -f /boot/vmlinuz-smp
    fi 
    if [[ -L /boot/vmlinuz-enterprise && ! -e /boot/vmlinuz-enterprise ]];then
	rm -f /boot/vmlinuz-enterprise
    fi 
}

function usage () {
cat << EOF >&2
Usage: ${0##*/} -[NCLlngarhcq] KERNEL_VERSION BOOTIMAGE MAPFILE

  -C:  Don't copy config files
  -l:  Add a lilo entry
  -i:  Dont generate Initrd files	    
  -n:  Don't launch lilo.
  -N:  Don't add entry to bootloader
  -g:  Add a Grub entry.
  -L:  Don't do link to default /boot/vmlinuz and /boot/System.map
  -d:  Don't autodetect boot loader.
  -a:  Autodetect boot loader.
  -r:  Features for RPM post install.
  -c:  Don't copy files.
  -q:  Be quiet.
  -h:  This help.
    
EOF
exit 2
}

while getopts CNLlRdngycachr opt
  do
  case "$opt" in
      N) NOENTRY="yes";;
      C) NOCONFIG="yes";;
      r) RPM="yes";NOCOPY="yes";;
      c) NOCOPY="yes";;
      i) NOINITRD="yes";;
      a) AUTODETECT="yes";;
      d) AUTODETECT=;;
      l) LOADER="LILO";;
      g) LOADER="GRUB";;
      R) REMOVE="-r";NOCOPY="yes" ;;
      n) NOLAUNCH="yes";;
      L) NOLINK="yes";;
      h) usage;;
      *) usage;;
  esac
done
while [[ $1 = -* ]];do shift; done

version=$1
boot_image=$2
map_file=$3

case "$NOLAUNCH" in
	yes) NOLAUNCH="-n" ;;
	*) NOLAUNCH="" ;;
esac

case "$NOLINK" in
	yes) NOLINK="-n" ;;
	*) NOLINK="" ;;
esac

if [[ -z $map_file || -z $map_file ]] && [[ -z $NOCOPY ]];then
    usage;
fi

[[ -n $4 ]] && boot=$4 || boot=/boot

[[ $AUTOREMOVE = "no" ]] && REMOVE=""

[[ -z $NOCOPY ]] && copy_image
[[ -z $NOLINK ]] && [[ -z $REMOVE ]] && do_link
[[ -z $NOLINK ]] && [[ -n $REMOVE ]] && do_remove_link
[[ -z $NOINITRD ]] && [[ -z $REMOVE ]] && [[ -x /usr/bin/perl ]] && perl $sharfile/make-initrd $NOLINK $version
[[ -n $AUTODETECT ]] && [[ -z $LOADER ]] && [[ -f /usr/sbin/detectloader ]] && LOADER=$(/usr/sbin/detectloader -q)

[[ $LOADER != "LILO" && $LOADER != "GRUB" ]] && {
    cat <<EOF 1>&2
Cannot find a boot loader, you may have to see why detectloader has
problems or specify via the command line.
EOF
exit 2;
}

if [[ -x /usr/bin/perl && -z $NOENTRY ]];then

    if [[ $LOADER = "GRUB" ]] && [[ -f $sharfile/grub ]];then
	$sharfile/grub $REMOVE $version
    fi

    if [[ $LOADER = "LILO" ]] && [[ -f $sharfile/lilo ]];then
	$sharfile/lilo $NOLAUNCH $REMOVE $version
    fi
fi
