#!/bin/bash

desktop=$(ps -e | grep kwin)
timestamp=$( date +%F-%T )

# arg 1:  the new package version
# arg 2:  the old package version

post_install() {
    EXTRAMODULES='extramodules-6.18'
    depmod $(cat /lib/modules/$EXTRAMODULES/version)
    echo 'In order to use nvidia module, reboot the system.'
}

post_upgrade() {
    EXTRAMODULES='extramodules-6.18'
    depmod $(cat /lib/modules/$EXTRAMODULES/version)
    
    if [[ $(vercmp ${2} 2:590.48.01-2) -eq -1 ]]; then
    
        kdialog --warningyesno "NVIDIA 590.xx driver drops support for Pascal (GTX 10xx) based GPUs<br>
                If you own such an NVIDIA card you either need to switch to <strong>in-kernel or NVIDIA 580.xx.</strong><br><br>
                sudo pacman -Rdd nvidia-utils nvidia<br>
                Then:<br>
                sudo pacman -S nvidia-580xx-utils nvidia-580xx<br>
                Or:<br>
                leave as is and use the in-kernel driver<br><br>
                Support for Wayland was added with 495.xx, add <strong>nvidia-drm.modeset=1</strong> to the kernel parameter to run a Wayland session.<br><br>
                Do you want to save this output?"

                save=${?}

                    if [[ ${save} == 1 ]]; then

                        kdialog --msgbox "Output not saved"

                    else

                        echo "NVIDIA 590.xx driver drops support for Pascal (GTX 10xx)-based GPUs<br>
                            If you own such an NVIDIA card you either need to switch to in-kernel or NVIDIA 580.xx.<br>
                            sudo pacman -Rdd nvidia-utils nvidia</b>
                            Then:<br>
                            sudo pacman -S nvidia-580xx-utils nvidia-580xx<br>
                            Or:<br>
                            leave as is and use the in-kernel driver<br>
                            Support for Wayland was added with 495.xx, add <strong>nvidia-drm.modeset=1</strong> to the kernel parameter to run a Wayland session." > /var/log/nvidia_install${timestamp}.txt

                        kdialog --msgbox "Output is saved in /var/log/nvidia_install${timestamp}.txt"
                        
                    fi
    fi
}

post_remove() {
    EXTRAMODULES='extramodules-6.18'
    depmod $(cat /lib/modules/$EXTRAMODULES/version)
    
}
