#!/bin/bash
lsof /dev/nvidia0 | grep mem
pgrep -a X


modprobe acpi_call
if [ "$( cat /etc/X11/xorg.conf 2>&1 | grep -o prime-nvidia-config)" != "" ] ; then
  modprobe -C /dev/null nvidia-drm
  modprobe -C /dev/null nvidia-modeset
  modprobe -C /dev/null nvidia
#  echo '\_SB.PCI0.PEG0.PEGP._ON' | sudo tee /proc/acpi/call
  echo 1> /sys/devices/pci0000:00/0000:00:01.0/rescan
else
   sleep 1
   modprobe -r -f nvidia-drm
   modprobe -r -f nvidia-modeset
   modprobe -r -f nvidia
   echo 1> /sys/devices/pci0000:00/0000:00:01.0/remove
#   echo '\_SB.PCI0.PEG0.PEGP._OFF' | sudo tee /proc/acpi/call
fi
