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

tput setaf 1;
echo "###############################################################################"
echo "###      SCRIPT TO REMOVE ALL THE LINUX KERNELS BUT THE LINUX KERNEL       ####"
echo "###############################################################################"
tput sgr0

echo "This script will remove all the kernels (used in ArcoLinux) but the linux-zen and linux-zen-headers"
read -p "Are you sure you want to proceed? (Y/y) " response

if [[ $response =~ ^[Yy]$ ]]; then

# Ensure at least one kernel remains
    if pacman -Qi linux-zen &> /dev/null && pacman -Qi linux-zen-headers &> /dev/null; then
        # Define all the kernels and their headers you want to remove
        KERNELS_TO_REMOVE=(
             linux-headers linux
             linux-lts-headers linux-lts
             linux-hardened-headers linux-hardened
             linux-rt-headers linux-rt
             linux-rt-lts-headers linux-rt-lts
             linux-cachyos-headers linux-cachyos
             linux-xanmod-headers linux-xanmod
        )
        remove_package() {
            local package=$1
            # Check if the package is installed
            if pacman -Q $package &> /dev/null; then
                echo "$package is installed. Removing..."
                sudo pacman -Rns --noconfirm $package
            else
                echo "$package is not installed."
            fi
        }

        # Loop over the array and remove each kernel package
        for kernel in "${KERNELS_TO_REMOVE[@]}"; do
            remove_package "$kernel"
        done

    else
        echo "Cannot proceed: At least one kernel must remain installed."
    fi

else
    echo "####################################"
    echo "Operation canceled by user."
    echo "####################################"

fi

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