#!/bin/bash
#====================================================================
# This is a Source Mage script for installing kernels to a Source
# Mage system
#
# @Copyright Source Mage GNU/Linux Developers
#
#====================================================================

VERSION=$1
IMAGE=$2
SYSMAP=$3
BOOT_DIR=${INSTALL_ROOT}$4
BUILD_DIRECTORY=$(dirname `pwd`)

kernel_arch=$(echo $IMAGE | cut -d/ -f2)

mkdir -p ${BOOT_DIR} &&
if grep 'CONFIG_MODULES=y' ./.config ; then
	case "$VERSION" in
		[2-9].*|[1-9][0-9].*)
			echo "installing for a ${VERSION%%.*}.x kernel" &&
			ARCH="$kernel_arch" make INSTALL_MOD_PATH="${INSTALL_ROOT}" modules_install
			;;
		*)
			echo "I again don't know what version: $VERSION is" &&
			return 1
			;;
	esac
fi &&
echo "Copying and linking kernel" &&
case  ${kernel_arch}  in
	ppc)
		cp vmlinux ${BOOT_DIR}/vmlinux-$VERSION &&
		ln -sf vmlinux-${VERSION} ${BOOT_DIR}/vmlinux
		;;
	sparc*)
		gzip -c9 vmlinux > ${BOOT_DIR}/vmlinuz-$VERSION &&
		ln -sf vmlinuz-${VERSION} ${BOOT_DIR}/vmlinuz
		;;
	*)
		cp $IMAGE ${BOOT_DIR}/vmlinuz-$VERSION &&
		ln -sf vmlinuz-${VERSION} ${BOOT_DIR}/vmlinuz
		;;
esac &&
echo "Copying and linking System.map" &&
cp $SYSMAP ${BOOT_DIR}/System.map-${VERSION} &&
ln -sf System.map-${VERSION} ${BOOT_DIR}/System.map &&
echo "Copying and linking .config" &&
cp .config ${BOOT_DIR}/config-${VERSION} &&
ln -sf config-${VERSION} ${BOOT_DIR}/config &&
# for people with boot partitions so grub can still find the kernel
# if they reference /boot/vmlinuz in their menu.lst
ln -sfn . ${BOOT_DIR}/boot
ln -sfn $(pwd) ${BUILD_DIRECTORY}/linux

#---------------------------------------------------------------------
##=back
##
##=head1 LICENSE
##
## This software 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.
##
## This software 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 this software; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##
#---------------------------------------------------------------------
