From 28288670a519b8f4a4fd76b0a6add271e703784b Mon Sep 17 00:00:00 2001 From: David Thurstenson Date: Wed, 1 Dec 2021 18:54:15 -0600 Subject: [PATCH] nextboot: minimize and reduce repitition --- bin/nextboot | 95 ++++++++++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 43 deletions(-) diff --git a/bin/nextboot b/bin/nextboot index 5f25425..c9870ab 100755 --- a/bin/nextboot +++ b/bin/nextboot @@ -73,66 +73,75 @@ while getopts ":en" opt; do esac done + # If -e wasn't set, and systemd-boot isn't installed, fail over to efimode anyways if [[ ! -v efimode && ! -v sdboot ]]; then echo "W: systemd-boot not installed. Choosing EFI boot entries instead..." efimode=1 fi + # If we're in EFI mode if [[ -v efimode ]]; then title="Pick EFI boot entry" - prompt="Entry" - - if [[ -v noreboot ]]; then - prompt="$prompt for next boot:" - elif [[ ! -v noreboot ]]; then - prompt="$prompt to boot IMMEDIATELY:" - else - echo "E: idk how you got here..." - exit 1 - fi - - # Prompt for the user's choice - choice="$(selectentry "$title" "$prompt")" - - # A special case to boot into firmware interface - if [[ "$choice" == "$biosname" ]]; then - sudo bootctl reboot-to-firmware true - elif [[ "$choice" != "$biosname" ]]; then - sudo efibootmgr -q --bootnext "$choice" - fi # If we're in systemd-boot mode elif [[ ! -v efimode ]]; then title="Pick systemd-boot entry config" - prompt="Entry" - if [[ -v noreboot ]]; then - prompt="$prompt for next boot:" - elif [[ ! -v noreboot ]]; then - prompt="$prompt to boot IMMEDIATELY:" - else - echo "E: idk how you got here either..." - exit 1 - fi - - # Prompt for the user's choice - choice="$(selectentry "$title" "$prompt")" - - # A special case to boot into firmware interface - if [[ "$choice" == "$biosname" ]]; then - sudo bootctl reboot-to-firmware true - elif [[ "$choice" != "$biosname" ]]; then - sudo bootctl set-oneshot "$choice" - # Add an option to speed past the boot menu, since we've already - # chosen the desired entry. - rebootopts+=("--boot-loader-menu=1") - fi else - echo "E: you DEFINITELY don't belong here..." + echo "E: idk how you got here..." exit 1 fi +# Specialize prompt for reboot mode +if [[ -v noreboot ]]; then + prompt="Entry for next boot:" +elif [[ ! -v noreboot ]]; then + prompt="Entry to boot IMMEDIATELY:" +else + echo "E: idk how you got here either..." + exit 1 +fi + + +# Prompt for the user's choice +choice="$(selectentry "$title" "$prompt")" + + +# A special case to boot into firmware interface +if [[ "$choice" == "$biosname" ]]; then + sudo bootctl reboot-to-firmware true + +# For all other entry selections +elif [[ "$choice" != "$biosname" ]]; then + + # If we're in EFI mode + if [[ -v efimode ]]; then + + # Set bootnext in nvram + sudo efibootmgr -q --bootnext "$choice" + + # If we're in systemd-boot mode + elif [[ ! -v efimode ]]; then + + # Set default boot entry only for next boot + sudo bootctl set-oneshot "$choice" + + # Add an option to speed past the boot menu, since we've already + # chosen the desired entry. + rebootopts+=("--boot-loader-menu=1") + + else + echo "E: you DEFINITELY don't belong here..." + exit 1 + fi +else + echo "E: must you insist on breaking my things?" + exit 1 +fi + + +# Reboot if necessary [[ ! -v noreboot ]] && sudo systemctl reboot "${rebootopts[@]}"