#!/bin/sh
#
# chkconfig:    2345 8 92
# description:  This script will check if a nvidia kernel module is present for\
#               the running kernel and modify the xorg.conf to  the appropriate\
#               configuration.

# Source function library.
. /etc/rc.d/init.d/functions

name='nvidia'
exec="/usr/sbin/$name-config-display"
prog="`basename $exec`"
lockfile="/var/lock/subsys/$name"

RETVAL=0
modname="nvidia.ko"
modpath="/lib/modules/$(uname -r)"
# Default to no module
module="noneWithSomeCrazyNameSoItsNeverFound"
# If one exists, then use it.
if test -e "${modpath}/extra/${modname}";then
    module="${modpath}/extra/${modname}"
elif test -e "${modpath}/extra/nvidia/${modname}";then
    module="${modpath}/extra/nvidia/${modname}"
elif test -e "${modpath}/kernel/drivers/video/nvidia/${modname}";then
    module="${modpath}/kernel/drivers/video/nvidia/${modname}"
fi


# Try to modify the below the least possible. If you need to change something, try
# using the variables above first, as most of the program control is based on
# the variables above. If something really needs to be changed, try to make
# the change in all the drivers for the sake of consistency.

# We shouldn't use lock files anymore because the lock file
# doesn't mean a driver is disabled or enabled. For example, we start()
# at boot and enable the driver. start() makes a lock file. We stop() on
# shutdown, which removes the lock file. But what if the user ran *config-display
# manually? Or if we don't `*config-display disable` in stop()?

start() {
    if action $"Checking for module $modname:" test -f $module;then
        action $"Enabling the $name driver:" $exec enable
        RETVAL=$?
    else
        echo -en $"$modname for kernel $(uname -r) was not found." && echo_warning;echo -en '\n'
        echo -en $"The $name driver will not be enabled until one is found." && echo_warning;echo -en '\n'
        $exec disable
        RETVAL=1
    fi
    if [ "$RETVAL" -eq "0" ];then
        #touch $lockfile
        # this is a nothing assignment so that bash doesn't complain.
        RETVAL=0
    else
        # Let them read the errors or warnings
        sleep 3
    fi
    return
}

stop() {
    #action $"Disabling the $name driver:" $exec disable
    #RETVAL=$?
    RETVAL=0
    #if [ "$RETVAL" -eq 0 ];then
    #    rm -f $lockfile
    #fi
    return
}

restart() {
    stop
    start
}

reload() {
    restart
}

status() {
    #if [ -e $lockfile ];then
        if [ -f $module ] && lsmod | grep ^${modname%%.ko} &>/dev/null ;then
            echo $"$modname for kernel $(uname -r) was found."
            echo $"The driver is enabled."
        else
            echo $"$modname for kernel $(uname -r) was not found."
            echo $"The $name driver cannot be enabled until one is found."
        fi
    #else
    #    echo $"The $name driver has not been enabled."
    #fi
    RETVAL=0
}

fdrstatus() {
    status $prog
}

case "$1" in
    start|stop|restart|reload)
        $1
    ;;
    status)
        fdrstatus
    ;;
    condrestart)
        restart
    ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
        exit 1
    ;;
esac

exit $RETVAL