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

if lsblk | grep -q "/boot/efi" ; then
    echo "/boot/efi is mounted."

	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

	echo "###############################################################################"
	echo "Installing Refind for ArcoLinux"
	echo "###############################################################################"
	echo
	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
	}

	#GRUB GONE
	if is_package_installed archiso; then
	    echo "Removing archiso..."
	    sudo pacman -R archiso --noconfirm
	else
	    echo "Archiso is not installed, no need to remove."
	fi

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

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

	if is_package_installed grub-customizer; then
	    echo "Removing grub-customizer..."
	    sudo pacman -R --noconfirm grub-customizer 
	else
	    echo "Grub-customizer is not installed, no need to remove."
	fi

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

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

	if [ -f /etc/default/grub ]; then
		sudo rm /etc/default/grub*
	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

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

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

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

	sudo pacman -S --noconfirm refind
	sudo refind-install
	sudo pacman -S --noconfirm arcolinux-refind-theme-arco-specs-git
	sudo pacman -S --noconfirm arcolinux-bootloader-refind-git

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

	# Path to refind.conf
	CONF_FILE="/boot/efi/EFI/refind/refind.conf"

	# Kernel names to add
	KERNEL_NAMES="linux,linux-lts,linux-zen,linux-hardened,linux-rt,linux-rt-lts,linux-xanmod,linux-cachyos"

	# Check if the line exists and append the kernel names
	if grep -q "^#extra_kernel_version_strings" "$CONF_FILE"; then
	    # The line exists, append the kernel names
	    sed -i "/^#extra_kernel_version_strings/s/$/ $KERNEL_NAMES/" "$CONF_FILE"
	    sed -i "s/^#*extra_kernel_version_strings.*/extra_kernel_version_strings $KERNEL_NAMES/" "$CONF_FILE"
	    echo "Kernel names added to #extra_kernel_version_strings in $CONF_FILE."
	else
	    # The line does not exist, add it with the kernel names
	    echo "#extra_kernel_version_strings $KERNEL_NAMES" >> "$CONF_FILE"
	    echo "Line added to $CONF_FILE."
	fi

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

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

else

	echo
	echo "###############################################################################"
	echo "###                    YOU CAN NOT USE REFIND                              ####"
	echo "###                        KEEP USING GRUB                                 ####"
	echo "###        https://wiki.archlinux.org/title/Arch_boot_process              ####"
	echo "###############################################################################"
fi
