#!/bin/sh
################################################################################
# $Id: install_kernel 105 2006-04-11 00:49:35Z makia $
################################################################################
#  Copyright (C) 2004 The Regents of the University of California.
#  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
#  Written by Makia Minich <makia@llnl.gov>
#  UCRL-CODE-155916
#
#  This file is part of GeDI, a diskless image management tool.
#  For details, see <http://www.llnl.gov/linux/gedi/>.
#
#  GeDI is free software; you can redistribute it and/or modify it under
#  the terms of the GNU General Public License as published by the Free
#  Software Foundation; either version 2 of the License, or (at your option)
#  any later version.
#
#  GeDI is distributed in the hope that it will be useful, but WITHOUT ANY
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
#  details.
#
#  You should have received a copy of the GNU General Public License along
#  with GeDI; if not, write to the Free Software Foundation, Inc.,
#  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
################################################################################
#
# Install a kernel for use.

# Source function library.
. /etc/gedi/variables

usage()
{
   echo "USAGE: $0 [OPTION]... <diskless image>"
   echo ""
   echo "Options:"
   echo "      -f          Remake the ramdisk even if it exists."
   echo "      -d          Change the default kernel."
   echo "      -h          Print out this help statement."
}

while getopts fdh opt ; do
   case "$opt" in
      f) force="-f" ;;
      d) change="-d" ;;
      h) usage ; exit 0 ;;
   esac
done

shift $(($OPTIND-1))

if [ ! -z "$1" -a -d "${IMAGE}/${1}" ] ; then
   IMAGE_TYPE=$1
else
   usage
   echo "Type specified is not available."
   exit 1
fi

for k in ${IMAGE}/${IMAGE_TYPE}/boot/vmlinuz-* ; do
   [ -h "$k" ] && continue

   modver=$(echo $k | sed "s/.*vmlinuz-//")
   avail_kerns=( ${avail_kerns[*]} $modver )
done

for kernel in ${avail_kerns[*]} ; do
   initrd=/boot/initrd-$kernel-gedi
   if [ ! -f ${IMAGE}/${IMAGE_TYPE}/${initrd} -o "${force}" = "-f" ] ; then
      echo "Attempting to create ramdisk for $kernel"
      chroot ${IMAGE}/${IMAGE_TYPE} /sbin/mkinitrd_gedi ${force} \
                                                        ${initrd} ${kernel}
   fi
done

if [ -z "$change" ] ; then
   echo "Running update_pxe_files"
   chroot ${IMAGE}/${IMAGE_TYPE} ${CLIENT_LIBDIR}/update_pxe_files
else
   echo "Selecting kernel..."
   ${SCRIPTDIR}/select_kernel
fi
