#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later

set -e

. /usr/share/debconf/confmodule

root_fs=$(mount | grep "on /target " | tail -n1 | cut -d' ' -f1)
if [ -z "$root_fs" ]; then
    echo "Could not determine root filesystem"
    exit 1
fi

root_uuid=$(blkid -s UUID -o value "$root_fs")
if [ -z "$root_uuid" ]; then
    echo "Could not determine root filesystem UUID"
    exit 1
fi

mkdir -p /target/etc/kernel/
echo "root=UUID=$root_uuid" > /target/etc/kernel/cmdline

db_get systemd-boot-installer/tries
tries=$((RET + 0))
if [ "$tries" -gt 0 ] && [ "$tries" -le 5 ]; then
    echo "$tries" > /target/etc/kernel/tries
fi

apt-install systemd-boot
