#!/bin/bash
set -eo pipefail

GREENBOOT_CONFIGURATION_FILE=/etc/greenboot/greenboot.conf
if test -f "$GREENBOOT_CONFIGURATION_FILE"; then
    source $GREENBOOT_CONFIGURATION_FILE
fi

if [ -n "$1" ]; then
    max_boot_attempts=$1
elif [ -n "$GREENBOOT_MAX_BOOT_ATTEMPTS" ]; then
    max_boot_attempts=$GREENBOOT_MAX_BOOT_ATTEMPTS
else
    max_boot_attempts=3 # default to 3 attempts
fi
grub2-editenv - set boot_counter="$max_boot_attempts"
grub2-editenv - set boot_success=0

echo "<3>GRUB2 environment variables have been set for system upgrade. Max boot attempts is $max_boot_attempts"
