#!/bin/sh -efu
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2008  Michael A. Kangin <mak@rsmu.ru>
# Copyright (C) 2008-2023  Alexey Gladkov <gladkov.alexey@gmail.com>
# Copyright (C) 2018  Michael Shigorin <mike@altlinux.org>

# shellcheck source=tools/mki-sh-functions
. "${0%/*}"/mki-sh-functions

verbose "has started executing."

[ -d "$chroot" ] ||
	fatal "$dir: does not look like a hasher work directory."

run_chrooted "$chroot" <<EOF
#!/bin/sh -efu

mkdir -p /.image/alt0

if [ -L /boot/image ]; then
	kimage="\$(readlink -ev /boot/image)"
else
	kimage="\$(find /boot -type f -name 'image-*' -print -quit)"
fi

if [ -z "\$kimage" ]; then
	echo "no kernel, no initrd, no boot.conf => not bootable!" >&2
	exit 1
fi

cp $verbose -aLf "\$kimage" /.image/alt0/vmlinux.0
if [ -f /boot/full.cz ]; then
	cp $verbose -aLf /boot/full.cz /.image/alt0/full.cz
fi

if [ -s /boot/boot.conf ]; then
	cp $verbose -aLf /boot/boot.conf /.image/boot.conf
fi

[ -f /.image/boot.conf ] || {
       echo >&2 'Bootloader config boot.conf does not exist!'
       exit 1
}
EOF
