#!/bin/bash
#
# chkconfig: 2345 0 99
# description: script to handle third party kernel modules

# Changelog:
# 	* 2007/10/25 - Initial release. Jon Masters <jcm@redhat.com>

# We need a nice way to find out the kernel we might have installed.
# but since there isn't a nice way, ask RPM anyway.

RPM="/bin/rpm"
GRUBBY="/sbin/grubby"

DEFAULT_KERNEL=`$GRUBBY --default-kernel 2>/dev/null`

if [ "" == "$DEFAULT_KERNEL" ]; then
	echo $"$0: Cannot read kernel config (root privileges required)"
	exit 1
fi

if [ ! -x "$RPM" ]; then
	echo $"$0: Cannot locate RPM. Running on a Red Hat system?"
	exit 1
fi

DEFAULT_KERNEL_PKG=`$RPM -q --whatprovides $DEFAULT_KERNEL`
DEFAULT_KERNEL_VER=`$RPM -ql $DEFAULT_KERNEL_PKG | sed -nre 's:^/lib/modules/(.*)/kernel:\1:p' | head -n1`
DEFAULT_KERNEL_LIB="/lib/modules/$DEFAULT_KERNEL_VER"

CURRENT_KERNEL_VER=`uname -r`

# Check if we installed a newer kernel and will boot into it.
# This will rebuilt the initrd, if needed, before the reboot.

function configure_default_kernel()
{
	echo -n $"Configuring default kernel for installed third party drivers: "
	/sbin/weak-modules --add-kernel "$DEFAULT_KERNEL_VER"
	echo $"done"
}

case "$1" in
	start|restart|force|reload)
		configure_default_kernel
		touch /var/lock/subsys/weak-modules
		exit 0
		;;
	stop)
		configure_default_kernel
		rm -f /var/lock/subsys/weak-modules
		exit 0
		;;
	status)
		if [ -e /var/lock/subsys/weak-modules ]; then
			echo "weak-modules configured"
			exit 0
		else
			echo "weak-modules not configured"
			exit 1
		fi
		;;
	*)
		echo $"Usage: $0 {start|stop|restart|force}"
esac

