#!/usr/bin/env bash

# this file is installed by sys-kernel/booster

ver=${1}
img=${2}
initrd=$(dirname "${img}")/initrd

# familiar helpers, we intentionally don't use Gentoo functions.sh
die() {
	echo -e " ${NOCOLOR-\e[1;31m*\e[0m }${*}" >&2
	exit 1
}

einfo() {
	echo -e " ${NOCOLOR-\e[1;32m*\e[0m }${*}" >&2
}

ewarn() {
	echo -e " ${NOCOLOR-\e[1;33m*\e[0m }${*}" >&2
}

main() {
	# re-define for subst to work
	[[ -n ${NOCOLOR+yes} ]] && NOCOLOR=

	# Exit if booster is not the INSTALLKERNEL_INITRD_GENERATOR
	[[ ${INSTALLKERNEL_INITRD_GENERATOR} == "booster" ]] || exit 0

	# do nothing if somehow booster is not installed
	[[ -x $(command -v booster) ]] || { ewarn "booster is not installed" && exit 0 ; }

	[[ ${EUID} -eq 0 ]] || die "Please run this script as root"

	booster -v build --force --kernel-version="${ver}" "${initrd}" || die "Failed to generate initramfs"
}

main