module_isloaded() {
	if [ `lsmod | grep ^nvidia | wc -l` -gt 0 ]; then
		return 0
	fi
	return 1
}

module_load() {
	echo -n "Loading nvidia kernel module... "
	## if nvidia dependent kernel is the current kernel
	if [ -e /lib/modules/`uname -r`/kernel/drivers/video/nvidia.ko.xz ]; then
		if modprobe nvidia --syslog; then
			echo "done."
			return 0
		else
			echo "FAILURE."
			echo "WARNING: Check why nvidia refused to load using dmesg."
		fi
	else
		echo "FAILURE."
		echo "WARNING: Kernel was updated, you have to restart so this change take effect."
	fi
	return 1
}

module_unload() {
	echo -n "Unloading nvidia kernel module... "
	if module_isloaded; then
		if modprobe --remove nvidia --syslog; then
			echo "done."
			return 1
		else
			echo "FAILURE."
			echo "WARNING: You have to restart X for this change to take effect !"
		fi
	else
		echo "skipped."
	fi
	return 1
}

post_install() {
	echo "*** In order to use this package you have to accept Nvidia's license which"
	echo "*** can be found at /usr/share/doc/$pkgname-$pkgver/LICENSE."
	echo "*** If you don't accept it, please remove the package!"

	exist=`cat /etc/sysconfig/modules | grep nvidia | wc -l`
	if [ "$exist" -eq 0 ]; then
		echo nvidia >> /etc/sysconfig/modules
	fi
	/sbin/depmod -a @_F_kernelmod_uname@
	module_load


	if ! grep -q 'rd.driver.blacklist=nouveau'  /etc/default/grub ; then
		echo "Updating kernel cmdline to blacklist nouveau in initrd.."
		sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT="/GRUB_CMDLINE_LINUX_DEFAULT="rd.driver.blacklist=nouveau /' /etc/default/grub
	fi

	if [ -f /boot/grub/grub.cfg ]; then
		grub-mkconfig -o /boot/grub/grub.cfg > /dev/null 2>&1
	fi
}

post_upgrade() {
	echo -n "Runing depmod for nvidia kernel module for kernel @_F_kernelmod_uname@ ..."
	/sbin/depmod -a @_F_kernelmod_uname@
	echo " done."
        ## if that exisit we bumped / upgraded the package only no new kernel
	## on a new kernel we do nothing because one has to reboot anyway
	if [ -e /lib/modules/`uname -r`/kernel/drivers/video/nvidia.ko.xz ]; then
		## try to unload it, on success try to load it again
		if module_unload; then
			sleep 1
			module_load
		fi
	fi

	if ! grep -q 'rd.driver.blacklist=nouveau'  /etc/default/grub ; then
		 echo "Updating kernel cmdline to blacklist nouveau in initrd.."
		 sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT="/GRUB_CMDLINE_LINUX_DEFAULT="rd.driver.blacklist=nouveau /' /etc/default/grub
	fi

	if [ -f /boot/grub/grub.cfg ]; then
		grub-mkconfig -o /boot/grub/grub.cfg > /dev/null 2>&1
	fi
}

pre_remove() {
	if module_unload; then
		echo "WARNING: You have to restart X for this change to take effet !"
	fi

	exist=`cat /etc/sysconfig/modules | grep nvidia | wc -l`
	if [ "$exist" -eq 1 ]; then
		grep -v '^nvidia' /etc/sysconfig/modules > /etc/sysconfig/modules.tmp
		mv /etc/sysconfig/modules.tmp /etc/sysconfig/modules
	fi
}

post_remove() {
	/sbin/depmod -a @_F_kernelmod_uname@

	if grep -q 'rd.driver.blacklist=nouveau'  /etc/default/grub ; then
		echo "Updating kernel cmdline..."
		sed -i 's/rd.driver.blacklist=nouveau //' /etc/default/grub
	fi

	if [ -f /boot/grub/grub.cfg ]; then
		grub-mkconfig -o /boot/grub/grub.cfg > /dev/null 2>&1
	fi
}

op=$1
shift
$op $*
