#!/usr/bin/bash
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

PATH=/sbin:/usr/sbin:$PATH

create_nextboot_timer() {
        # For future reference, it's possible to use `at` if supported everywhere
        nohup bash -c "
                logger -s 'NextBoot value is valid for the next $NEXTBOOT_VALID_TIME seconds';
                sleep $NEXTBOOT_VALID_TIME;
                logger -s 'Purging NextBoot value...'
                efibootmgr -N && logger -s 'NextBoot value is no longer valid'
        " 2>&1 | /usr/bin/tee /dev/console &
}

if efibootmgr &>/dev/null ; then
    # if not defined
    if [ -z "$NEXTBOOT_VALID_TIME" ] ; then
            NEXTBOOT_VALID_TIME=180
    # elif not a valid number
    elif [[ $NEXTBOOT_VALID_TIME =~ [^0-9] ]]; then
            logger -s "NEXTBOOT_VALID_TIME not numeric. Changing to default of 180"
            NEXTBOOT_VALID_TIME=180
    fi
    os_boot_entry=$(efibootmgr | awk '/BootCurrent/ { print $2 }')
    # fall back to /root/EFI_BOOT_ENTRY.TXT if it exists and BootCurrent is not available
    if [[ -z "$os_boot_entry" && -f /root/EFI_BOOT_ENTRY.TXT ]] ; then
        os_boot_entry=$(</root/EFI_BOOT_ENTRY.TXT)
    fi
    if [[ -n "$os_boot_entry" ]] ; then
        logger -s "efibootmgr -n $os_boot_entry"
        efibootmgr -n $os_boot_entry
        if [[ $NEXTBOOT_VALID_TIME > 0 ]]; then
            create_nextboot_timer
            # Adjust watchdog if running inside of test case
            if [[ -n $RSTRNT_RECIPE_URL && $RSTRNT_MAXTIME ]] ; then
                rstrnt-adjust-watchdog $(($RSTRNT_MAXTIME + $NEXTBOOT_VALID_TIME))
            fi
        else
            logger -s "NEXTBOOT_VALID_TIME is zero. BootNext setting will persist."
            sleep 5
        fi
    else
        logger -s "Could not determine value for BootNext!"
    fi
fi
