#!/bin/bash
#set -e
##################################################################################################################
# Author    : Erik Dubois
# Website   : https://www.erikdubois.be
# Website   : https://www.alci.online
# Website   : https://www.arcolinux.info
# Website   : https://www.arcolinux.com
# Website   : https://www.arcolinuxd.com
# Website   : https://www.arcolinuxb.com
# Website   : https://www.arcolinuxiso.com
# Website   : https://www.arcolinuxforum.com
##################################################################################################################
#
#   DO NOT JUST RUN THIS. EXAMINE AND JUDGE. RUN AT YOUR OWN RISK.
#
##################################################################################################################
#tput setaf 0 = black 
#tput setaf 1 = red 
#tput setaf 2 = green
#tput setaf 3 = yellow 
#tput setaf 4 = dark blue 
#tput setaf 5 = purple
#tput setaf 6 = cyan 
#tput setaf 7 = gray 
#tput setaf 8 = light blue
##################################################################################################################

# Function to restart the script with sudo
restart_with_sudo() {
    tput setaf 3
    echo
    echo "This script needs to be run with superuser rights."
    echo "Please restart the script with 'sudo' rights by typing:"
    echo "sudo $0"
    echo
    tput sgr0
    exit 1

}

# Check if running as root
if [ "$(id -u)" -ne 0 ]; then
    restart_with_sudo
fi

tput setaf 2;
# Checks for rEFInd
check_refind() {
    if efibootmgr -v | grep -iq refind; then
        echo "rEFInd is being used."
        return 0 # Success
    fi
    return 1 # Failure
}

# Checks for systemd-boot
# systemd-boot is known to create a loader directory within the EFI system partition
check_systemd_boot() {
    # Assuming the EFI system partition is mounted at /boot or /boot/efi
    if [ -d "/boot/loader" ] || [ -d "/boot/efi/loader" ]; then
        echo "systemd-boot is being used."
        return 0 # Success
    fi
    return 1 # Failure
}

# Checks for GRUB
check_grub() {
    if [ -d "/boot/grub" ] || [ -d "/boot/efi/EFI/grub" ]; then
        echo "GRUB is being used."
        return 0 # Success
    fi
    return 1 # Failure
}

# Attempt to detect the boot manager
if check_grub; then
    :
elif check_systemd_boot; then
    :
elif check_refind; then
    :
else
    echo "Unknown boot manager."
fi
tput sgr0

tput setaf 3
if [ -f /bootloader ]; then
    echo "Check:";cat /bootloader
fi
tput sgr0

echo
echo "###############################################################################"
echo "###                DONE - YOU CAN CLOSE THIS WINDOW                        ####"
echo "###############################################################################"
