#!/bin/sh
#--------------------------------------------------------------------
# Copyright (C) 2000, 2002 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.36 2002/01/24 15:40:04 sbenedict Exp $
#--------------------------------------------------------------------
## description: 
#	      Install a kernel to /boot and add an entry for grub,
#             lilo, or yaboot

sharfile=/usr/share/loader/

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

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

function read_link () {
    #at this time we have perl since we do kernel copy
    [[ -x /usr/bin/perl ]] && perl -e '{print readlink shift, "\n"}' $1
}

function copy_image () {
    local config=""
    local link

    if [[ -f .config ]];then
	config=.config
    elif [[ -f ../../../.config ]];then
	config=../../../.config
    fi
    
    if [ -L $boot/vmlinuz ];then
	link=$(read_link $boot/vmlinuz)
	link=${link##*/} #make it relative
	rm -f /boot/vmlinuz
	[[ "${boot}/${link}" != "${boot}/vmlinuz-${version}" && -f ${boot}/${link} ]] && ln -sf ${link} /boot/vmlinuz.old
    elif [ -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
}

#TODO: commonize
function do_link () {
    local extra=

    case $version in
	2.2*linus) extra=-2.2.linus;;
	2.2*secure) extra=-2.2.secure;;
	2.2*smp) extra=-2.2.smp;;
	2.2*) extra=-2.2;;
	2.4*mosix) extra=-mosix;;
	2.4*BOOT) extra=-BOOT;;
	2.4*linus) extra=-linus;;
	2.4*smp) extra=-smp;;
	2.4*secure) extra=-secure;; #not existing at this time
	2.4*enterprise) extra=-enterprise;;
    esac
    
    ln -fs vmlinuz-$version $boot/vmlinuz${extra}
}

function do_remove_link () {
    local ext=
    for ext in 2.2.linus 2.2.secure 2.2.smp 2.2 mosix BOOT linus smp secure enterprise;do
	if [[ -L /boot/vmlinuz-${ext} && ! -e /boot/vmlinuz-${ext} ]];then
	    rm -f /boot/vmlinuz-${ext}
	fi
    done
    if [[ -L /boot/vmlinuz && ! -e /boot/vmlinuz ]];then
	rm -f /boot/vmlinuz
    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.
  -y:  Add a yaboot 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";;
      y) LOADER="YABOOT";;
      R) REMOVE="-r";NOCOPY="yes" ;;
      n) NOLAUNCH="-n";;
      L) NOLINK="-n";;
      h) usage;;
      *) usage;;
  esac
done
shift $((OPTIND - 1))

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

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

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

[[ $AUTOREMOVE = "no" ]] && REMOVE=""
[[ $NOLINK = "yes" ]] && NOLINK=-n

if [[ $version == *BOOT ]];then
    cat <<EOF 1>&2
*********     WARNING	  **********
You install a BOOT kernel, and this is not intended for use on a normal system.
EOF
fi

[[ -z $NOCOPY ]] && copy_image
[[ -z $NOLINK ]] && [[ -z $REMOVE ]] && do_link
[[ -z $NOLINK ]] && [[ -n $REMOVE ]] && do_remove_link
[[ -z $NOINITRD ]] && [[ -z $REMOVE ]] && $sharfile/make-initrd $NOLINK $version
if [[ $HOSTTYPE == *586 ]];then
    [[  ! -e /boot/grub/menu.lst && ! -e /etc/lilo.conf ]] && exit 0 #this is for install time.
elif [[ $HOSTTYPE == *powerpc ]];then
     [[  ! -e /etc/yaboot.conf ]] && exit 0
fi

[[ -n $AUTODETECT ]] && [[ -z $LOADER ]] && [[ -f /usr/sbin/detectloader ]] && LOADER=$(/usr/sbin/detectloader -q)

[[ $LOADER != "LILO" && $LOADER != "GRUB" && $LOADER != "YABOOT" ]] && {
    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
	perl $sharfile/grub $REMOVE $version
    fi

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

    if [[ $LOADER = "YABOOT" ]] && [[ -f $sharfile/yaboot ]];then
	perl $sharfile/yaboot $NOLAUNCH $REMOVE $version
    fi
fi
