#!/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
##################################################################################################################

# Disclaimer message
tput setaf 3;
echo "NO WARRANTY"
echo
echo "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,"
echo "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF"
echo "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND"
echo "NONINFRINGEMENT."
echo "IN NO EVENT SHALL THE AUTHORS, COPYRIGHT HOLDERS, OR CONTRIBUTORS BE"
echo "LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN"
echo "ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN"
echo "CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE"
echo "SOFTWARE."
echo "NO WARRANTY OR ASSURANCE, EITHER EXPRESS OR IMPLIED, IS MADE WITH"
echo "RESPECT TO THE PRODUCT PROVIDED UNDER OR BY VIRTUE OF THIS"
echo "AGREEMENT, INCLUDING, BUT NOT LIMITED TO, ANY IMPLIED WARRANTY OF"
echo "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR"
echo "NON-INFRINGEMENT."
echo "NO WARRANTY IS GIVEN THAT THE SOFTWARE OR SERVICE WILL BE"
echo "ERROR-FREE, PERFORM IN AN UNINTERRUPTED MANNER, OR THAT ANY DEFECTS"
echo "OR ERRORS IN THE SOFTWARE OR SERVICE WILL BE CORRECTED."
echo "THE USER ASSUMES ALL RISKS AND RESPONSIBILITY FOR THE SELECTION OF"
echo "THE SOFTWARE TO ACHIEVE THEIR INTENDED RESULTS, AND FOR THE"
echo "INSTALLATION, USE, AND RESULTS OBTAINED FROM IT."
echo

echo "DISCLAIMER:"
echo "This script assists in changing the boot manager from GRUB to"
echo "either systemd-boot or rEFInd."
echo "Modifying your system's boot manager and configurations can be"
echo "hazardous and may result in a non-bootable system if not"
echo "executed correctly."
echo
echo "It is strongly recommended to have a full system backup and"
echo "a live USB available for system repair in case of any issues."
echo
echo "Do you wish to continue?"

# User confirmation
read -r -p "Please confirm with [Y/N]: " response
case "$response" in
    [yY][eE][sS]|[yY]) 
        echo "Proceeding with the boot manager change..."
        ;;
    *)
        echo "Operation cancelled by the user. Exiting script."
        exit 1
        ;;
esac
tput sgr0

echo
echo "Are you really sure?"
echo "This script may break your system"
echo "Resulting in an unbootable system!"

# User confirmation
read -r -p "Please confirm with [Y/N]: " response
case "$response" in
    [yY][eE][sS]|[yY]) 
        echo "Proceeding with the boot manager change..."
        ;;
    *)
        echo "Operation cancelled by the user. Exiting script."
        exit 1
        ;;
esac

Online=0

function check_connectivity() {

    local test_ip
    local test_count

    test_ip="8.8.8.8"
    test_count=1

    if ping -c ${test_count} ${test_ip} > /dev/null; then
       echo "You have internet connectivity"
       Online=1
    else
       echo "You do not have internet connectivity"
       Online=0
    fi
 }

check_connectivity

# if not online
if [ $Online -eq 0 ] ; then
    echo
    echo "###############################################################################"
    echo "###                          YOU ARE NOT ONLINE                            ####"
    echo "###         WE ARE PINGING 8.8.8.8 TO TEST YOUR INTERNET CONNECTION        ####"
    echo "###                        WE NEED AN INTERNET CONNECTION                     ####"
    echo "###############################################################################"
    exit 1
fi

function check_file() {

    file="/usr/local/share/arcolinux/pacman.conf"
    if [[ -f $file ]];then
        echo $file " exists"
        file_boolean=1
    else
        echo $file " doesn't exist"
        file_boolean=0
    fi
}

check_file


echo "###############################################################################"
echo "Removing other bootloaders"
echo "###############################################################################"

is_package_installed() {
    if pacman -Q "$1" &> /dev/null; then
        echo "Package $1 is installed."
        return 0  # Success
    else
        echo "Package $1 is not installed."
        return 1  # Failure
    fi
}

#SYSTEMD-BOOT GONE
if is_package_installed kernel-install-mkinitcpio; then
    echo "Removing kernel-install-mkinitcpio..."
    sudo pacman -R kernel-install-mkinitcpio --noconfirm
else
    echo "kernel-install-mkinitcpio is not installed, no need to remove."
fi

if is_package_installed pacman-hook-kernel-install; then
    echo "Removing pacman-hook-kernel-install..."
    sudo pacman -R pacman-hook-kernel-install --noconfirm
else
    echo "Pacman-hook-kernel-install is not installed, no need to remove."
fi

if is_package_installed arcolinux-bootloader-systemd-boot-git; then
    echo "Removing arcolinux-bootloader-systemd-boot..."
    sudo pacman -R arcolinux-bootloader-systemd-boot-git --noconfirm
else
    echo "Arcolinux-bootloader-systemd-boot is not installed, no need to remove."
fi

#REFIND GONE
if is_package_installed arcolinux-bootloader-refind-git; then
    echo "Removing arcolinux-bootloader-refind-git..."
    sudo pacman -R arcolinux-bootloader-refind-git --noconfirm
else
    echo "Arcolinux-bootloader-refind-git is not installed, no need to remove."
fi

if is_package_installed arcolinux-refind-theme-arco-specs-git; then
    echo "Removing arcolinux-refind-theme-arco-specs-git..."
    sudo pacman -R arcolinux-refind-theme-arco-specs-git --noconfirm
else
    echo "Arcolinux-refind-theme-arco-git is not installed, no need to remove."
fi

#keep last
if is_package_installed refind; then
    echo "Removing refind..."
    sudo pacman -R refind --noconfirm
else
    echo "Refind is not installed, no need to remove."
fi

if [ -d /boot/efi/loader ]; then
    sudo rm -r /boot/efi/loader
fi

if [ -d /boot/efi/EFI/refind ]; then
    sudo rm -r /boot/efi/EFI/refind
fi

if [ -f /boot/refind_linux.conf ]; then
    sudo rm /boot/refind_linux.conf
fi

echo "###############################################################################"
echo "Reinstalling the default grub for ArcoLinux"
echo "###############################################################################"

# pacman config + not online
if [ $Online -eq 0 ] && [ $file_boolean -eq 1 ] ; then
    echo "getting latest /etc/default/grub from local system"
	sudo cp /usr/local/share/arcolinux/grub/grub /etc/default/grub
fi

# pacman config + from local
if [ $file_boolean -eq 1 ] ; then
    sudo cp /usr/local/share/arcolinux/grub/grub /etc/default/grub
    echo "getting latest /etc/default/grub from local system"
fi

#putting old pacman config back
if [ $Online -eq 0 ]  && [ $file_boolean -eq 0 ] ; then
	echo "Run this script once you are back online"
fi

#failsafe does not exist
if [ ! -f /etc/default/grub ]; then
    sudo cp /usr/local/share/arcolinux/grub/grub /etc/default/grub
fi  

#failsafe empty
if [ ! -s /etc/default/grub ]; then
    sudo cp /usr/local/share/arcolinux/grub/grub /etc/default/grub
fi  


sudo pacman -S grub --noconfirm

sudo pacman -S arcolinux-grub-theme-vimix-git --noconfirm
sudo pacman -S arcolinux-bootloader-grub-git --noconfirm

if [ -f /usr/share/wayland-sessions/hyprland.desktop ] || \
    [ -f /usr/share/wayland-sessions/sway.desktop ] || \
    [ -f /usr/share/wayland-sessions/wayfire.desktop ] || \
    [ -f /usr/share/wayland-sessions/plasma.desktop ] || \
    [ -f /usr/share/wayland-sessions/gnome.desktop ]; then
    sudo /usr/local/bin/arcolinux-nvidia-drm-wayland
fi

sudo grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=ArcoLinux

sudo grub-mkconfig -o /boot/grub/grub.cfg

echo "grub" | sudo dd of="/bootloader"
