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

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

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

Online=0
file_boolean=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 connectivity"
       Online=0
    fi
 }

function check_file() {

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

check_connectivity
check_file


# pacman config + online
if [ $Online -eq 1 ] ; then
	echo "getting latest /etc/default/grub from github"
	sudo wget https://raw.githubusercontent.com/arconetpro/arconet-iso/refs/heads/main/archiso/airootfs/etc/default/grub -O $workdir/etc/default/grub
fi

# pacman config + not online
if [ $Online -eq 0 ] && [ $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

echo "Check if nvidia-drm.modeset is necessary"

/usr/local/bin/arcolinux-nvidia-drm-wayland

echo
echo "###############################################################################"
echo "###    Updating your /boot/grub/grub.cfg to apply the grub default file    ####"
echo "###    Make sure you installed arcolinux-grub-theme-vimix-git              ####"
echo "###    to see the default ArcoLinux grub theme again                       ####"
echo "###############################################################################"
echo

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

echo
echo "###############################################################################"
echo "###    Make sure you installed arcolinux-grub-theme-vimix-git              ####"
echo "###    to see the default ArcoLinux grub theme again                       ####"
echo "###                DONE - YOU CAN CLOSE THIS WINDOW                        ####"
echo "###############################################################################"
