#
# Spec file for NVIDIA kernel module.
#
# $Header: //sw/OEM/nvr40_linux_4490/drivers/resman/NVIDIA_kernel.spec#1 $
#
%define RELEASE 4496
%define CUSTOM_RELEASE 4496
%define rel	%{?CUSTOM_RELEASE} %{!?CUSTOM_RELEASE:%RELEASE}
%define ver     1.0

Summary: NVIDIA kernel module for NVIDIA Architecture support.
Name: 		NVIDIA_kernel
Version: 	%ver
Release: 	%rel
Copyright: 	NVIDIA Corp. 2000
Group: 		User Interface/X Hardware Support
Source: 	ftp://ftp1.detonator.nvidia.com/pub/drivers/english/XFree86_40/NVIDIA_kernel-%{ver}-%{RELEASE}.tar.gz
URL: 		http://www.nvidia.com
Vendor: 	NVIDIA Corp.
BuildRoot: 	%{_tmppath}/%{name}-%{PACKAGE_VERSION}
Packager: 	Buildmeister
Requires:	kernel >= 2.2.0
Requires:	rpm >= 3.0.4

%define debug_package %{nil}

%description
NVIDIA Architecture support for systems with updated or custom kernels.

%prep
%setup -q -n ${RPM_PACKAGE_NAME}-${RPM_PACKAGE_VERSION}-${RPM_PACKAGE_RELEASE}


%build
    # don't exit immediately when something bad happens.
    set +e

    # building an rpm from an srpm does not check the requires field.
    # make sure that rpm is good enough for us.
    RPMVERSION=`rpm -q --queryformat "%{VERSION}" rpm`
    RPMMAJOR=`echo $RPMVERSION | awk -F \. '{print $1;}'`
    RPMMINOR=`echo $RPMVERSION | awk -F \. '{print $2;}'`
    RPMPATCH=`echo $RPMVERSION | awk -F \. '{print $3;}'`
    if [ $RPMMAJOR -le 3 -a $RPMMINOR -le 0 -a $RPMPATCH -lt 4 ]; then
        echo -e "\nNeed rpm version 3.0.4 or greater\n";
        exit 1;
    fi

    case `cat /proc/version` in
    *'gcc version 3.2'*)
	# set CC to compiler matching the one the kernel was built with
	export CC=gcc32;;
    esac

    make nvidia.o

# This will install the module itself onto the user's system, but not 
# load it into the running system. This will allow our auto-builds to
# execute properly
%install

    # create a file list for nvidia.o, to make sure the path is correct
    # it really should be under $RPM_BUILD_ROOT, but the %files section
    # does not honor it.  Bummer.
    rm -f /tmp/files.lst

    # allow a build script to override the targetted kernel
    # otherwise default to current running kernel
    if [ -z "$TARGET_KERNEL" ]; then
        export TARGET_KERNEL=`uname -r`
    fi

    if [ -d $RPM_ROOT/lib/modules/$TARGET_KERNEL/kernel ]; then
        INSTALLPATH=/lib/modules/$TARGET_KERNEL/kernel/drivers/video
    else
        INSTALLPATH=/lib/modules/$TARGET_KERNEL/video
    fi
    mkdir -p $RPM_BUILD_ROOT/$INSTALLPATH
    install -m 0444 nvidia.o $RPM_BUILD_ROOT/$INSTALLPATH
    echo "%attr(444 root root) " $INSTALLPATH/nvidia.o > /tmp/files.lst
    unset RPM_BUILD_ROOT  # trick RPM 4.0 into not stripping us

%clean 
[ -n "$RPM_BUILD_ROOT" -a "$RPM_BUILD_ROOT" != / ] && rm -rf $RPM_BUILD_ROOT

# In %post we basically do "run-time" installation
# Note that our files are installed onto the system, and the packages
# are cleaned up and gone. There are no Makefiles or fancy environment
# variables to play with. I basically repeated the needed parts of the
# install.sh script here
%post

    # unload it quietly if already loaded..
    /sbin/rmmod NVdriver >& /dev/null
    /sbin/rmmod nvidia   >& /dev/null

    rmmod_failed=0
    /sbin/lsmod | grep nvidia   >& /dev/null && rmmod_failed=1
    /sbin/lsmod | grep NVdriver >& /dev/null && rmmod_failed=1

    if [ $rmmod_failed -eq 1 ]; then
        echo
        echo "Unable to remove existing NVIDIA kernel module."
        echo "Please be sure you have exited X before attempting"
        echo "to install the NVIDIA_kernel rpm."
        echo
        exit 1
    fi

    # delete the old NVdriver
    # XXX is there a better way to do this?
    if [ -z "$TARGET_KERNEL" ]; then
        export TARGET_KERNEL=`uname -r`
    fi
    if [ -d $RPM_ROOT/lib/modules/$TARGET_KERNEL/kernel ]; then
        INSTALLPATH=/lib/modules/$TARGET_KERNEL/kernel/drivers/video
    else
        INSTALLPATH=/lib/modules/$TARGET_KERNEL/video
    fi
    rm -f $INSTALLPATH/NVdriver

    function error {
        echo "$(basename $0): fatal error, $1 failed"
        exit 1
    }

    modconf=""
    tmp=`mktemp -q /tmp/nvidia.XXXXXX` || error "mktemp"

    paths[0]=/etc/conf.modules
    paths[1]=/etc/modules.conf
    paths[2]=/etc/modutils/aliases

    for path in ${paths[@]}; do
        test -f $path && modconf=$path
    done

    if [ ! -z $modconf ]; then
	# Initialize the permissions on $tmp so that we don't change the
	# permissions of $modconf when we mv it into place
	cp -p $modconf $tmp
        sed '/^alias.*\(NVdriver\|nvidia\)/d'  < $modconf   > $tmp
        if [ -c /dev/.devfsd ]; then
            echo "alias /dev/nvidia*   nvidia" >> $tmp
        else
            echo "alias char-major-195 nvidia" >> $tmp
        fi
        mv -f $tmp $modconf
        test -x /sbin/update-modules && /sbin/update-modules
    else
        if [ -f /etc/rc.d/rc.modules ]; then
	    # Initialize the permissions on $tmp so that we don't change the
	    # permissions of /etc/rc.d/rc.modules when we mv it into place
	    cp -p /etc/rc.d/rc.modules $tmp
            sed '/.*\(NVdriver\|nvidia\).*/d' < /etc/rc.d/rc.modules > $tmp
            echo "/sbin/modprobe nvidia" >> $tmp
            mv -f $tmp /etc/rc.d/rc.modules
        fi
    fi

    if [ ! -c /dev/.devfsd ]; then
        for i in 0 1 2 3; do
            node="/dev/nvidia$i"
            rm -f $node
            mknod $node c 195 $i || error "mknod \"$node\""
            chmod 0666 $node     || error "chmod \"$node\""
        done
        node="/dev/nvidiactl"
        rm -f $node
        mknod $node c 195 255  || error "mknod \"$node\""
        chmod 0666 $node       || error "chmod \"$node\""
    fi

    # Add it to the module dependency list.
    /sbin/depmod -a && /sbin/modprobe nvidia

    # Check to see if the module is actually loaded
    if ! /sbin/lsmod | grep nvidia >& /dev/null ; then
        echo
        echo "Failed to install nvidia.o"
        echo
        exit 1
    fi

    # It worked
    echo
    echo nvidia.o installed successfully
    echo

    exit 0

%preun
if [ $1 = 0 ] ; then
    #
    # unload it quietly if already loaded..
    #
    # Only unload if unused, otherwise this scriptlet fails and
    # RPM won't continue with uninstallation.  So we also check
    # if the usage count is zero. 
    #
    if [ x`/sbin/lsmod | grep nvidia | tr -s " " | cut -f 3 -d " "` == "x0" ]; then
        /sbin/rmmod nvidia >& /dev/null
    fi
fi

%files -f /tmp/files.lst

%changelog
* Sat Oct 25 2003 Sjoerd Mullender <sjoerd@acm.org>
- Simplified and updated to work with Fedora Core 1.

* Sun Jan 05 2003 Joseph Kain <jkain@nvidia.com>
- Correctly preserve permissions on /etc/rc.d/rc.modules.
- Pass "-f" option to mv.
- Fix typos.

* Fri Jan 03 2003 Joseph Kain <jkain@nvidia.com>
- Preserve permissions on modules conf file.

* Thu Nov 07 2002 Joseph Kain <jkain@nvidia.com>
- Suppress lsmod output

* Fri Oct 11 2002 Andy Ritger <aritger@nvidia.com>
- Check that the module is loaded before reporting success.

* Fri Sep 06 2002 Gareth Hughes <gareth@nvidia.com>
- Change NVdriver to nvidia.o everywhere.

* Mon Nov 26 2001 Joseph Kain <jkain@nvidia.com>
- Use %{_tmppath} instead of hardcoding /var/tmp

* Tue Aug 24 2001 Joseph Kain <jkain@nvidia.com>
- in preun check usage count of NVdriver before trying to unload.

* Thu Aug 31 2000 Terence Ripperda <tripperda@nvidia.com>
- allow build scripts to specify a kernel version via TARGET_KERNEL variable
  otherwise fallback to `uname -r`
- when searching for agpgart module, search in RPM_BUILD_ROOT, so we don't
  pick up an errant symbol on a build machine.

* Tue Aug 15 2000 Mark Cooke <mpc@star.sr.bham.ac.uk>
- Change permissions on NVdriver to 444 to avoid symbol-stripping issues
  with versions of RPM after 3.0.4
- Add a test to make sure the NVdriver registers correctly.

* Wed Aug 02 2000 Terence Ripperda <tripperda@nvidia.com>
- regress use of /proc/devices to get major number

* Tue May 02 2000 Terence Ripperda <tripperda@nvidia.com>
- regress use of "make install" in post, no Makefile present

* Fri Apr 27 2000 Nick Triantos <nick@nvidia.com>
- Use "NVdriver" and "install" targets on make
- Remove BuildReq item from header, was causing build error

* Thu Apr 27 2000 Gregory McLean <gregm@comstar.net>
- Cleaned out some redundant parts of the spec file.
- Set the buildroot.
- Made sure the preun gets called properly.
- Added a %clean section to keep building machine tidy.
- Set up logic for epoc builds if needed.

