#!/bin/sh
#
# chkconfig:    2345 8 92
# description:  This script will check if a nvidia 96xx 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-96xx'
exec="/usr/sbin/$name-config-display"
prog="`basename $exec`"
lockfile="/var/lock/subsys/$name"

RETVAL=0
modname="nvidia.ko"
modpath="/lib/modules/$(uname -r)/extra/$name"
# Default to no module
module="noneWithSomeCrazyNameSoItsNeverFound"
# If one exists, then use it.
if test -e "${modpath}/${modname}";then
    module="${modpath}/${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.

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'
        RETVAL=1
    fi
    if [ "$RETVAL" -eq "0" ];then
        touch $lockfile
    else
        # Let them read the errors or warnings
        sleep 3
    fi
    return
}

stop() {
    action $"Disabling the $name driver:" $exec disable
    RETVAL=$?
    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 will not 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
