#!/bin/sh
# $Id: installkernel,v 1.7 2004/08/17 02:08:27 at Exp $
#--------------------------------------------------------------------
# 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)
#--------------------------------------------------------------------
# Copyright (C) 2003, 2004 by ALT Linux Team,
# Alexey Tourbin <at@altlinux.org>.
#--------------------------------------------------------------------
# description: Install a kernel to /boot

# This file is called from $KSRC/*/boot/install.sh on `make install' in the
# kernel source tree. For historical reasons, the installer, livecd-install,
# and mkimage-profiles also call it.

# Calls through an obsolete memtest entry point are no-ops.
[ -z "${0##*memtest*}" ] && exit 0

REMOVE=
NODEFAULT=
NOFLAVOUR=
KEEPINITRD=
UBOOT=

. /usr/lib/bootloader-utils/functions

load_config

usage()
{
	cat <<EOF >&2
Usage: $0 [options] KERNEL_VERSION [IMAGE_FILE MAP_FILE INSTALL_PATH]

  -r, -R, --remove	remove bootloader entry and prepare to remove kernel;
  -m, --memtest		legacy options, ignored;
      --label <label>	use <label> if it is not already used;
      --nodefault	do not change default kernel (/boot/vmlinuz) link;
      --noflavour	do not change flavour kernel (/boot/vmlinuz-flavour) link;
      --keep-initrd	do not overwrite existing initrd;
  -u, --uboot		assume u-boot bootloader, optionally create packed images for it;
  -h, --help		this help.
EOF
	[ -n "$1" ] && exit "$1" || exit 2
}

LEGACY=mnalgyNdsSCiLc
ARGV=`getopt -n "$0" -o rRuh$LEGACY -l remove,memtest,help,nodefault,noflavour,keep-initrd,label:,uboot,nolaunch -- "$@"` || usage
eval set -- "$ARGV"

while :; do
	case "$1" in
		-r|-R|--remove) REMOVE=--remove; shift
			;;
		--label) shift 2
			;;
		--nodefault) NODEFAULT=--nodefault; shift
			;;
		--noflavour) NOFLAVOUR=--noflavour; shift
			;;
		--keep-initrd) KEEPINITRD=--keep-initrd; shift
			;;
		-u|--uboot) UBOOT=--uboot; shift
			;;
		-h|--help) shift; usage 0
			;;
		--) shift; break
			;;
		-[$LEGACY]|--memtest|--nolaunch) echo "$0: legacy option $1 ignored (probably harmless)" >&2; shift
			;;
		*) echo "$0: unrecognized option: $1" >&2; exit 1
			;;
	esac
done

VERSION="$1"
[ -n "$VERSION" ] || usage

! during_install || exit 0

# Handle call from install.sh
IMAGE=
if [ $# -ge 4 ]; then
	KCFG=.config
	KIMG=$2
	KMAP=$3
	INSTALL_PATH=${4:-/}
	cp -S.old --preserve=timestamps "$KCFG" "$INSTALL_PATH/config-$VERSION" &&
	cp -S.old --preserve=timestamps "$KIMG" "$INSTALL_PATH/vmlinuz-$VERSION" ||
		exit 1
	if [ -f "$KMAP" ]; then
		cp -S.old --preserve=timestamps "$KMAP" "$INSTALL_PATH/System.map-$VERSION" ||
			exit 1
	fi
	NOFLAVOUR=--noflavour
	# not a packaged image of the same version in $MODULES_PREFIX
	IMAGE="$BOOTDIR/vmlinuz-$VERSION"
	if ! [ "$INSTALL_PATH" -ef "$BOOTDIR" ]; then
		# The kernel image is being installed into staging directory, i.e.
		# make install INSTALL_PATH=`pwd`/inst ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
		# Don't generate initramfs, and don't touch boot loader.
		exit 0
	fi
fi

if [ -n "$REMOVE" ]; then
	# the files in $BOOTDIR are copies, the package owns the ones next to
	# the image
	COPIES=
	[ ! -f "$MODULES_PREFIX/$VERSION/vmlinuz" ] || COPIES=--copies
	exec /usr/lib/bootloader-utils/kernel-boot-files remove "$VERSION" \
		$COPIES --reselect $UBOOT
else
	exec /usr/lib/bootloader-utils/kernel-boot-files add "$VERSION" \
		${IMAGE:+--image "$IMAGE"} $NODEFAULT $NOFLAVOUR $KEEPINITRD $UBOOT
fi
