#!/bin/bash
# grabed from https://github.com/xerolinux/xerolinux_iso
#
# driver=free or driver=nonfree - grub and efi
echo "Start cachyos-remove-nvidia"
kernel_cmdline(){
    for param in $(cat /proc/cmdline); do
        case "${param}" in
            $1=*) echo "${param##*=}"; return 0 ;;
            $1) return 0 ;;
            *) continue ;;
        esac
    done
    [ -n "${2}" ] && echo "${2}"
    return 1
}

get_driver(){
    echo $(kernel_cmdline driver)
}

get_virtmach(){
    echo $(kernel_cmdline virtmach)
}

while [ -e "/var/lib/pacman/db.lck" ];
do
    echo 'Pacman is not ready yet. Will try again in 10 seconds.'
    seconds=$(($seconds + 10))
    sleep 10
    if [[ "$seconds" == "30" ]]; then
        echo 'Warning: removing pacman db.lck!'
        rm /var/lib/pacman/db.lck
    fi
done

# free = remove nvidia keep nouveau
selection=$(get_driver)
echo "#################################"
echo "Selection was "$selection
echo "#################################"
if [[ $selection == "free" ]]; then
    pacman -R --noconfirm nvidia-dkms nvidia-utils
fi

#freenonouveau = remove nvidia and nouveau
selection=$(get_driver)
echo "#################################"
echo "Selection was "$selection
echo "#################################"
if [[ $selection == "freenonouveau" ]]; then
    pacman -R --noconfirm nvidia-dkms nvidia-utils
    pacman -R --noconfirm xf86-video-nouveau
fi

#nonfree = keepin nvidia and nouveau
selection=$(get_driver)
echo "Selection was "$selection
if [[ $selection == "nonfree" ]]; then
    echo "#################################"
    echo "We keep nvidia and nouveau onboard"
    echo "#################################"
fi

# nonfreenonouveau : keep nvidia and remove nouveau
selection=$(get_driver)
echo "#################################"
echo "Selection was "$selection
echo "#################################"
if [[ $selection == "nonfreenonouveau" ]]; then
    pacman -R --noconfirm xf86-video-nouveau
fi

# remove virtual machines
selection=$(get_virtmach)
echo "#################################"
echo "Selection was "$selection
echo "#################################"
if [[ $selection == "none" ]]; then

    #remove virtualbox
    if pacman -Qi virtualbox-guest-utils &> /dev/null; then
        systemctl disable vboxservice.service
        pacman -Rns virtualbox-guest-utils --noconfirm
        echo "Removed virtualbox-guest-utils"
    fi

    if pacman -Qi virtualbox-guest-utils-nox &> /dev/null; then
        systemctl disable vboxservice.service
        pacman -Rns virtualbox-guest-utils-nox --noconfirm
        echo "Removed virtualbox-guest-utils-nox"
    fi

    #remove vmware
    if [ -f /etc/xdg/autostart/vmware-user.desktop ]; then
        rm /etc/xdg/autostart/vmware-user.desktop
        echo "Removed vmware-user.desktop"
    fi

    if pacman -Qi open-vm-tools &> /dev/null; then
        systemctl disable vmtoolsd.service
        echo "Disabled vmtoolsd.service"
        pacman -Rns open-vm-tools --noconfirm
        echo "Removed open-vm-tools"
    fi

    if [ -f /etc/systemd/system/multi-user.target.wants/vmtoolsd.service ]; then
        rm /etc/systemd/system/multi-user.target.wants/vmtoolsd.service
        echo "Removed vmtoolsd.service if still exists"
    fi

    #remove qemu
    if pacman -Qi qemu-guest-agent &> /dev/null; then
        systemctl disable qemu-guest-agent.service
        pacman -Rns qemu-guest-agent --noconfirm
        echo "Removed qemu-guest-agent"
    fi

fi

echo "#################################"
echo "End cachyos-remove-nvidia"
echo "#################################"
