#!/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 REMOVE MICROCODE                  ####"
echo "###############################################################################"
tput sgr0
echo "This script will remove microde packages"
read -p "Are you sure you want to proceed? (Y/y) " response

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

	if grep -q "base udev autodetect microcode" /etc/mkinitcpio.conf; then
		echo "###############################################################################"
		echo "Removing microcode"
		echo "###############################################################################"
		sudo sed -i "s/microcode //" /etc/mkinitcpio.conf;
	fi

	echo
	echo "###############################################################################"
	echo "Removing the correct microcode - intel or amd"
	echo "###############################################################################"
	cpu=$(cat /proc/cpuinfo | grep vendor_id | uniq | awk '{print $3}')
	echo "We are removing the package for this cpu: "$cpu
	echo "###############################################################################"

	if [ "$cpu" == "GenuineIntel" ]; then
		echo "Intel CPU detected. Removing intel-ucode package..."
		sudo pacman -R --noconfirm intel-ucode
	elif [ "$cpu" == "AuthenticAMD" ]; then
		echo "AMD CPU detected. Removing amd-ucode package..."
		sudo pacman -R --noconfirm amd-ucode
	else
		echo "CPU type unrecognized. No microcode package removed."
	fi

	sudo mkinitcpio -P

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

fi

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