#!/bin/sh -efu
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2007-2023  Alexey Gladkov <gladkov.alexey@gmail.com>
# Copyright (C) 2023  Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>

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

verbose "has started executing."

BOOT_TYPE="${BOOT_TYPE:?boot type required}"
EFI_BOOTLOADER="${EFI_BOOTLOADER:-}"

outname="${MKI_OUTNAME:-outname}"
outname="${outname##*/}"

# shellcheck disable=SC2086
set -- ${BOOT_TYPE:-}

[ "$#" -gt 0 ] ||
	fatal "Boot type required"

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

boot_isolinux=
boot_pxelinux=
boot_syslinux=
boot_ieee1275boot=
boot_grubpcboot=
boot_e2kboot=
boot_grub_efi=
for b; do
	case "$b" in
		isolinux)
			message 'WARNING: the isolinux boot type is deprecated!'
			message 'WARNING: use either isolinux-boot (if you want isolinux),'
			message 'WARNING: or "isolinux-boot grub-efi" (if you want isolinux + EFI).'
			boot_isolinux=1
			if [ "$EFI_BOOTLOADER" = grub-efi ]; then
				boot_grub_efi=1
			fi
			;;
		grubpcboot)
			message 'WARNING: the grubpcboot boot type is deprecated!'
			message 'WARNING: use either grub-pc-boot (if you want grub-pc),'
			message 'WARNING: or "grub-pc-boot grub-efi" (if you want grub-pc + EFI).'
			boot_grubpcboot=1
			if [ "$EFI_BOOTLOADER" = grub-efi ]; then
				boot_grub_efi=1
			fi
			;;
		efiboot|grubaa64boot)
			message "WARNING: the '$b' boot type is deprecated!"
			message 'WARNING: use the grub-efi boot type instead.'
			boot_grub_efi=1
			;;
		isolinux-boot) boot_isolinux=1 ;;
		grub-pc-boot) boot_grubpcboot=1 ;;
		pxelinux) boot_pxelinux=1 ;;
		syslinux) boot_syslinux=1 ;;
		ieee1275boot) boot_ieee1275boot=1 ;;
		e2kboot) boot_e2kboot=1 ;;
		grub-efi) boot_grub_efi=1 ;;
		*) fatal "$b: unknown boot type" ;;
	esac
done

# The EFI_BOOTLOADER had previously supported other values here.  Therefore,
# it's better to fail early if the profile specifies an EFI bootloader that is
# not supported.
case "$EFI_BOOTLOADER" in
	''|grub-efi) ;;
	*) fatal "$EFI_BOOTLOADER EFI bootloader is not supported!" ;;
esac

# Recreate and reorder the boot types list for mki-copy* helpers
# and further processing.  Some boot type keywords are translated into two
# boot types (like isolinux -> isolinux-boot grub-efi).
set --

if [ -n "$boot_isolinux" ]; then
	set -- "$@" isolinux
fi

if [ -n "$boot_grubpcboot" ]; then
	set -- "$@" grubpcboot
fi

if [ -n "$boot_grub_efi" ]; then
	set -- "$@" grub-efi
fi

if [ -n "$boot_pxelinux" ]; then
	set -- "$@" pxelinux
fi

if [ -n "$boot_syslinux" ]; then
	set -- "$@" syslinux
fi

if [ -n "$boot_ieee1275boot" ]; then
	set -- "$@" ieee1275boot
fi

if [ -n "$boot_e2kboot" ]; then
	set -- "$@" e2kboot
fi

# The default is used for pxelinux and syslinux boot types.
pack_helper=mki-pack-data
case "$#" in
	1)
		if [ -n "$boot_isolinux" ]; then
			pack_helper=mki-pack-isolinux
		elif [ -n "$boot_grubpcboot" ]; then
			pack_helper=mki-pack-grubpc
		elif [ -n "$boot_grub_efi" ]; then
			pack_helper=mki-pack-grub-efi
		elif [ -n "$boot_ieee1275boot" ] || [ -n "$boot_e2kboot" ]; then
			pack_helper=mki-pack-isodata
		fi
		;;
	2)
		if [ -n "$boot_isolinux" ] && [ -n "$boot_grub_efi" ]; then
			pack_helper=mki-pack-hybrid-isolinux-grub-efi
		elif [ -n "$boot_grubpcboot" ] && [ -n "$boot_grub_efi" ]; then
			pack_helper=mki-pack-hybrid-grubpc-grub-efi
		else
			fatal "The '${BOOT_TYPE:-}' boot type combination is not supported"
		fi
		;;
	*)
		fatal "The '${BOOT_TYPE:-}' boot type combination is not supported"
		;;
esac

[ -z "${PROPAGATOR_MAR_MODULES:-}" ] ||
	mki-build-propagator

for b; do
	mki-copy-"$b"
done

exec "$pack_helper"
