#!/bin/sh

# Long list of all packages, should be taken from the manifest
package_list="
RedHat Linux 7.0 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-3123.rh70up.i386.rpm MD5SUM: 9961b1d713db4bc7f7fbb22afba98721

RedHat Linux 7.0 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-3123.rh70smp.i386.rpm MD5SUM: f90bb32915ea27cd15cfd262e8a0e254

RedHat Linux 7.1 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-3123.rh71up.i386.rpm MD5SUM: 6aa3c415c3358b9659ad1fe01cf9c74a

RedHat Linux 7.1 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-3123.rh71smp.i386.rpm MD5SUM: cc9c1a969975c751d2d6746fbc006449

RedHat Linux 7.2 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-3123.rh72up.i386.rpm MD5SUM: 9075f54a48e748240734a692ece97620

RedHat Linux 7.2 updated to 2.4.9-31 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-3123.rh72up_2.4.9_31.i386.rpm MD5SUM: 25485d5ec5cb00889376383e343bb3c8

RedHat Linux 7.3 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-3123.rh73up.i386.rpm MD5SUM: 12470abefe0468a7cc43005283c6c507

RedHat Linux 7.3 updated to 2.4.18-10 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-3123.rh73up_2.4.18_10.i386.rpm MD5SUM: 1513f7fcb37d5af849a5a0b723dcfac7

Mandrake Linux 8.0 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-3123.mdk80up.i386.rpm MD5SUM: a3044815036de2e9098fcc581ae8cc1b

Mandrake Linux 8.0 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-3123.mdk80smp.i386.rpm MD5SUM: 143b5d1b21d00898857e5775c5a44c03

Mandrake Linux 8.1 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-3123.mdk81up.i386.rpm MD5SUM: a9e16f7482deadcdb1e190a5f449539d

Mandrake Linux 8.1 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-3123.mdk81smp.i386.rpm MD5SUM: 0745ffd387873a2a777af5d64d46d1dc

RedHat Linux 7.0 UP Single Processor i686 Architecture
NVIDIA_kernel-1.0-3123.rh70up.i686.rpm MD5SUM: bc4a541b71db551a3adc3f15e1fa4539

RedHat Linux 7.0 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-3123.rh70smp.i686.rpm MD5SUM: aa75bf1c38e86c7367cb9e663b3a46d2

RedHat Linux 7.0 Enterprise on i686 Architecture
NVIDIA_kernel-1.0-3123.rh70enterprise.i686.rpm MD5SUM: 341392d3b1f506a83ec49bd1badd5e43

RedHat Linux 7.1 UP Single Processor i686 Architecture
NVIDIA_kernel-1.0-3123.rh71up.i686.rpm MD5SUM: f719f3c9013dc9cec974e46c9554c45e

RedHat Linux 7.1 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-3123.rh71smp.i686.rpm MD5SUM: c83a5266fbf0ccd259248edcd87f0f51

RedHat Linux 7.1 Enterprise i686 Architecture
NVIDIA_kernel-1.0-3123.rh71enterprise.i686.rpm MD5SUM: c0825727e6207898d5330318a6a57047

RedHat Linux 7.2 UP Single Processor i686 Architecture
NVIDIA_kernel-1.0-3123.rh72up.i686.rpm MD5SUM: 85b534b5cf13f0f340a687a2b34289c0

RedHat Linux 7.2 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-3123.rh72smp.i686.rpm MD5SUM: 9ac86e433b3f7ca282e55291a7911e0d

RedHat Linux 7.2 Enterprise i686 Architecture
NVIDIA_kernel-1.0-3123.rh72enterprise.i686.rpm MD5SUM: 204793d4aeccc6d469dd673011a3526b

RedHat Linux 7.2 updated to 2.4.9-31 UP Single Processor i686 Architecture
NVIDIA_kernel-1.0-3123.rh72up_2.4.9_31.i686.rpm MD5SUM: 77fbe45b2101ba1eff97e94c02320e16

RedHat Linux 7.2 updated to 2.4.9-31 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-3123.rh72smp_2.4.9_31.i686.rpm MD5SUM: 2faa346630d47947a57ed25d50542789

RedHat Linux 7.2 updated to 2.4.9-31 Enterprise i686 Architecture
NVIDIA_kernel-1.0-3123.rh72enterprise_2.4.9_31.i686.rpm MD5SUM: 84292dab4a2ce273dd02ed2e6a0ea986

RedHat Linux 7.3 UP Single Processor i686 Architecture
NVIDIA_kernel-1.0-3123.rh73up.i686.rpm MD5SUM: 455878c1572ad53f1b5f80f6a073a8bf

RedHat Linux 7.3 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-3123.rh73smp.i686.rpm MD5SUM: cc87392ad5e3ca18410ffe671b467b1b

RedHat Linux 7.3 Bigmem i686 Architecture
NVIDIA_kernel-1.0-3123.rh73bigmem.i686.rpm MD5SUM: 9dac7ae9036de8098b653c5adf6e99b0

RedHat Linux 7.3 updated to 2.4.18-10 UP Single Processor i686 Architecture
NVIDIA_kernel-1.0-3123.rh73up_2.4.18_10.i686.rpm MD5SUM: 751968a67f34289edde8b3adf9f7bbd3

RedHat Linux 7.3 updated to 2.4.18-10 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-3123.rh73smp_2.4.18_10.i686.rpm MD5SUM: 5f4613b242cd057796d6ab53635ddbda

RedHat Linux 7.3 updated to 2.4.18-10 Enterprise i686 Architecture
NVIDIA_kernel-1.0-3123.rh73bigmem_2.4.18_10.i686.rpm MD5SUM: 40a856b632b7258e9b6d34805eaad3be

Mandrake Linux 8.0 UP Single Processor i686 Architecture
NVIDIA_kernel-1.0-3123.mdk80up.i686.rpm MD5SUM: c4f83ecb040a6c33353936ac7a45cdb6

Mandrake Linux 8.0 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-3123.mdk80smp.i686.rpm MD5SUM: c730b73d5ae686bc36e4813c1ff733e6

Mandrake Linux 8.0 Enterprise i686 Architecture
NVIDIA_kernel-1.0-3123.mdk80enterprise.i686.rpm MD5SUM: 00f1cdef6e7b559d43fdc558be1cc7f5

Mandrake Linux 8.1 UP Single Processor i686 Architecture
NVIDIA_kernel-1.0-3123.mdk81up.i686.rpm MD5SUM: e389a18d909113c9f3d65cde2aeb6396

Mandrake Linux 8.1 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-3123.mdk81smp.i686.rpm MD5SUM: 0ce8ceaf15fa8d48cc42a168584ca9f3

Mandrake Linux 8.1 Enterprise i686 Architecture
NVIDIA_kernel-1.0-3123.mdk81enterprise.i686.rpm MD5SUM: 7e5ed1c13a83e96eb99f74384fb3ef4c

RedHat Linux 7.2 UP Single Processor Athlon Architecture
NVIDIA_kernel-1.0-3123.rh72up.athlon.rpm MD5SUM: c12834e985532f3dd15b7c2ca22fbd3f

RedHat Linux 7.2 SMP Multiprocessor Athlon Architecture
NVIDIA_kernel-1.0-3123.rh72smp.athlon.rpm MD5SUM: c730b814fc86fefd8aaf93d80640560e

RedHat Linux 7.2 updated to 2.4.9-31 UP Single Processor Athlon Architecture
NVIDIA_kernel-1.0-3123.rh72up_2.4.9_31.athlon.rpm MD5SUM: 8705d4767dce6f77daa8ff247342bf55

RedHat Linux 7.2 updated to 2.4.9-31 SMP Multiprocessor Athlon Architecture
NVIDIA_kernel-1.0-3123.rh72smp_2.4.9_31.athlon.rpm MD5SUM: 33cc8bf3d6859778b2a11dddbf7a89d0

RedHat Linux 7.3 UP Single Processor Athlon Architecture
NVIDIA_kernel-1.0-3123.rh73up.athlon.rpm MD5SUM: 3fbe05186af944ab49c5fe96432936f2

RedHat Linux 7.3 SMP Multiprocessor Athlon Architecture
NVIDIA_kernel-1.0-3123.rh73smp.athlon.rpm MD5SUM: 3fa8226612b5fcd1662dc1d0193be7a8

RedHat Linux 7.3 updated to 2.4.18-10 UP Single Processor Athlon Architecture
NVIDIA_kernel-1.0-3123.rh73up_2.4.18_10.athlon.rpm MD5SUM: 3d9f44003242bea731bccf33c447e478

RedHat Linux 7.3 updated to 2.4.18-10 SMP Multiprocessor Athlon Architecture
NVIDIA_kernel-1.0-3123.rh73smp_2.4.18_10.athlon.rpm MD5SUM: 1654ba0be5ee17ac69b714f9c8b5419a

RedHat Linux 7.3 SMP Multiprocessor i586 Architecture
NVIDIA_kernel-1.0-3123.rh73smp.i586.rpm MD5SUM: 18cc860446d01c8fb9c7572641a0f3cb

RedHat Linux 7.3 updated to 2.4.18-10 UP Single Processor i586 Architecture
NVIDIA_kernel-1.0-3123.rh73up_2.4.18_10.i586.rpm MD5SUM: 3e857beeca0490e3ee05388a39558347

RedHat Linux 7.3 updated to 2.4.18-10 SMP Multiprocessor i586 Architecture
NVIDIA_kernel-1.0-3123.rh73smp_2.4.18_10.i586.rpm MD5SUM: c36366b38ab06f2ddb03062ff7d50246

Mandrake Linux 8.2 UP Single Processor i586 Architecture
NVIDIA_kernel-1.0-3123.mdk82up.i586.rpm MD5SUM: 31d6516e531f23f48a917c729b7ef71a

Mandrake Linux 8.2 SMP Multiprocessor i586 Architecture
NVIDIA_kernel-1.0-3123.mdk82smp.i586.rpm MD5SUM: f4d20c06fc587b40c6cf11903c331aab

Mandrake Linux 8.1 Enterprise i586 Architecture
NVIDIA_kernel-1.0-3123.mdk82enterprise.i586.rpm MD5SUM: 06eb912b7a596ab1a3351b751170fb46

Mandrake Linux 8.2 Secure i586 Architecture
NVIDIA_kernel-1.0-3123.mdk82secure.i586.rpm MD5SUM: a8bc78cddf736c6f0e6b0f4b4dda19c1

Common X and OpenGL for all Linux systems.
NVIDIA_GLX-1.0-3123.i386.rpm MD5SUM: a693331e694bb447d13204babbf46cb2

For systems not listed above
NVIDIA_kernel-1.0-3123.src.rpm MD5SUM: 1c7d3296bee5960159c6cb3bca1170b1
NVIDIA_GLX-1.0-3123.src.rpm MD5SUM: e62f4d35a172b52014dee3f81f466e81

For systems that don't use RPM
NVIDIA_kernel-1.0-3123.tar.gz MD5SUM: 9496c1b260985eaea59d3760b1e42eb4
NVIDIA_GLX-1.0-3123.tar.gz MD5SUM: 2e1ea7847838f49360cf358b2c8fb15b
"

kernel_list="
2.2.16-22:i386:NVIDIA_kernel-1.0-3123.rh70up.i386.rpm;
2.2.16-22smp:i386:NVIDIA_kernel-1.0-3123.rh70smp.i386.rpm;
2.4.2-2:i386:NVIDIA_kernel-1.0-3123.rh71up.i386.rpm;
2.4.2-2smp:i386:NVIDIA_kernel-1.0-3123.rh71smp.i386.rpm;
2.4.7-10:i386:NVIDIA_kernel-1.0-3123.rh72up.i386.rpm;
2.4.9-31:i386:NVIDIA_kernel-1.0-3123.rh72up_2.4.9_31.i386.rpm;
2.4.18-3:i386:NVIDIA_kernel-1.0-3123.rh73up.i386.rpm;
2.4.18-10:i386:NVIDIA_kernel-1.0-3123.rh73up_2.4.18_10.i386.rpm;
2.4.3-20mdk:i386:NVIDIA_kernel-1.0-3123.mdk80up.i386.rpm;
2.4.3-20mdksmp:i386:NVIDIA_kernel-1.0-3123.mdk80smp.i386.rpm;
2.4.8-26mdk:i386:NVIDIA_kernel-1.0-3123.mdk81up.i386.rpm;
2.4.8-26mdksmp:i386:NVIDIA_kernel-1.0-3123.mdk81smp.i386.rpm;
2.2.16-22:i686:NVIDIA_kernel-1.0-3123.rh70up.i686.rpm;
2.2.16-22smp:i686:NVIDIA_kernel-1.0-3123.rh70smp.i686.rpm;
2.2.16-22enterprise:i686:NVIDIA_kernel-1.0-3123.rh70enterprise.i686.rpm;
2.4.2-2:i686:NVIDIA_kernel-1.0-3123.rh71up.i686.rpm;
2.4.2-2smp:i686:NVIDIA_kernel-1.0-3123.rh71smp.i686.rpm;
2.4.2-2enterprise:i686:NVIDIA_kernel-1.0-3123.rh71enterprise.i686.rpm;
2.4.7-10:i686:NVIDIA_kernel-1.0-3123.rh72up.i686.rpm;
2.4.7-10smp:i686:NVIDIA_kernel-1.0-3123.rh72smp.i686.rpm;
2.4.7-10enterprise:i686:NVIDIA_kernel-1.0-3123.rh72enterprise.i686.rpm;
2.4.9-31:i686:NVIDIA_kernel-1.0-3123.rh72up_2.4.9_31.i686.rpm;
2.4.9-31smp:i686:NVIDIA_kernel-1.0-3123.rh72smp_2.4.9_31.i686.rpm;
2.4.9-31enterprise:i686:NVIDIA_kernel-1.0-3123.rh72enterprise_2.4.9_31.i686.rpm;
2.4.18-3:i686:NVIDIA_kernel-1.0-3123.rh73up.i686.rpm;
2.4.18-3smp:i686:NVIDIA_kernel-1.0-3123.rh73smp.i686.rpm;
2.4.18-3bigmem:i686:NVIDIA_kernel-1.0-3123.rh73bigmem.i686.rpm;
2.4.18-10:i686:NVIDIA_kernel-1.0-3123.rh73up_2.4.18_10.i686.rpm;
2.4.18-10smp:i686:NVIDIA_kernel-1.0-3123.rh73smp_2.4.18_10.i686.rpm;
2.4.18-10enterprise:i686:NVIDIA_kernel-1.0-3123.rh73bigmem_2.4.18_10.i686.rpm;
2.4.3-20mdk:i686:NVIDIA_kernel-1.0-3123.mdk80up.i686.rpm;
2.4.3-20mdksmp:i686:NVIDIA_kernel-1.0-3123.mdk80smp.i686.rpm;
2.4.3-20mdkenterprise:i686:NVIDIA_kernel-1.0-3123.mdk80enterprise.i686.rpm;
2.4.8-26mdk:i686:NVIDIA_kernel-1.0-3123.mdk81up.i686.rpm;
2.4.8-26mdksmp:i686:NVIDIA_kernel-1.0-3123.mdk81smp.i686.rpm;
2.4.8-26mdkenterprise:i686:NVIDIA_kernel-1.0-3123.mdk81enterprise.i686.rpm;
2.4.7-10:athlon:NVIDIA_kernel-1.0-3123.rh72up.athlon.rpm;
2.4.7-10smp:athlon:NVIDIA_kernel-1.0-3123.rh72smp.athlon.rpm;
2.4.9-31:athlon:NVIDIA_kernel-1.0-3123.rh72up_2.4.9_31.athlon.rpm;
2.4.9-31smp:athlon:NVIDIA_kernel-1.0-3123.rh72smp_2.4.9_31.athlon.rpm;
2.4.18-3:athlon:NVIDIA_kernel-1.0-3123.rh73up.athlon.rpm;
2.4.18-3smp:athlon:NVIDIA_kernel-1.0-3123.rh73smp.athlon.rpm;
2.4.18-10:athlon:NVIDIA_kernel-1.0-3123.rh73up_2.4.18_10.athlon.rpm;
2.4.18-10smp:athlon:NVIDIA_kernel-1.0-3123.rh73smp_2.4.18_10.athlon.rpm;
2.4.18-3smp:i586:NVIDIA_kernel-1.0-3123.rh73smp.i586.rpm;
2.4.18-10:i586:NVIDIA_kernel-1.0-3123.rh73up_2.4.18_10.i586.rpm;
2.4.18-10smp:i586:NVIDIA_kernel-1.0-3123.rh73smp_2.4.18_10.i586.rpm;
:i586:NVIDIA_kernel-1.0-3123.mdk82up.i586.rpm;
:i586:NVIDIA_kernel-1.0-3123.mdk82smp.i586.rpm;
:i586:NVIDIA_kernel-1.0-3123.mdk82enterprise.i586.rpm;
:i586:NVIDIA_kernel-1.0-3123.mdk82secure.i586.rpm;
"

# Driver Version
NVVERSION=1.0-3123

# Determine which distribution and version is running
if [ -f "/etc/issue" ]; then
    # Is it Red Hat? 
    # Red Hat's issue looks like "Red Hat Linux release X.Y (codename)
    if ( grep "Red Hat" /etc/issue > /dev/null ); then
	distro="rh"
	version=`grep "Red Hat" /etc/issue  | tr -d "a-zA-Z(). "`
    fi
	
    # Is it Mandrake?
    # Mandrake's issue has a big logo in it and then
    # "Mandrake Linux release X.Y (codename) for some_arch
    if ( grep "Mandrake" /etc/issue > /dev/null ); then
	distro="mdk"
	version=`grep "Mandrake" /etc/issue | sed -e "s/.*release \([0-9]\+\)\.\([0-9]\+\).*/\1\2/"`
    fi
fi

# Is it SuSE?
# 
if [ -f /etc/SuSE-release ]; then
    version=`grep VERSION /etc/SuSE-release|tr -d "a-zA-Z =."`
    [ "$version" = "70" ] && version="70+xfree86-4.0.2"
    KERNEL_PACKAGE=NVIDIA_kernel-${NVVERSION}.suse${version}.i386.rpm
    GLX_PACKAGE=NVIDIA_GLX-${NVVERSION}.suse${version}.i386.rpm
    echo "Please download and install the following files:"
    echo $GLX_PACKAGE
    echo $KERNEL_PACKAGE
    exit 0
fi

# XXX Detect Debian and recommend either the tarball or link to the
# Debian contrib package.

# XXX Other distros, recommend the tarball packages
if [ -z "$distro" ]; then
    echo "NVIDIA does not provide a prebuilt binary driver for your"
    echo "distribution at this time.  Please download:"
    echo $package_list | tr " " "\n" | grep "tar.gz" | cut -f 1 -d " "
    exit
fi

# Determine kernel version 
KERNEL_TYPE=`uname -r | tr -d "0-9.-" | sed -e "s/mdk//"`
KERNEL_VERSION=`uname -r | tr -d "a-zA-Z"`

# Build an appropriate string to represent the installed RPM kernel package
KERNEL_PACKAGE=kernel
if [ -n "$KERNEL_TYPE" ]; then
    KERNEL_PACKAGE=$KERNEL_PACKAGE-$KERNEL_TYPE
fi
if [ -n "$KERNEL_VERSION" ]; then
    KERNEL_PACKAGE=$KERNEL_PACKAGE-$KERNEL_VERSION
    if ( `uname -r | grep "mdk" > /dev/null` ) ; then
	# add "mdk" back to the end of the kernel package name
	KERNEL_PACKAGE=${KERNEL_PACKAGE}mdk
    fi
fi

# Determine kernel architecture, based on $KERNEL_VERSION
# Default to something that can't match
KERNEL_ARCH="unknown"

# rpm will emit error text to stdout if it doesn't find what I asked for, 
# so  we have to check the return value.
rpm -q --qf '%{ARCH}' $KERNEL_PACKAGE > /dev/null && KERNEL_ARCH=`rpm -q --qf '%{ARCH}' $KERNEL_PACKAGE`
if [ "$KERNEL_ARCH" = "unknown" ]; then
    # Try some other variations, as some distributions name their packages
    # in different ways
    TMP_KERNEL_VERSION=`echo $KERNEL_VERSION | tr "-" "."`
    KERNEL_PACKAGE="kernel-$TMP_KERNEL_VERSION"
    if [ "$distro" = "mdk" ]; then
	KERNEL_PACKAGE=${KERNEL_PACKAGE}mdk
    fi
    rpm -q --qf '%{ARCH}' $KERNEL_PACKAGE > /dev/null && KERNEL_ARCH=`rpm -q --qf '%{ARCH}' $KERNEL_PACKAGE`
fi

# We need to patch up KERNEL_ARCH according to the following rules:
# 1) We only have i386 builds for things that aren't one of:
#    - RedHat 7.2 or later
#    - Mandrake 8.1 or later
#    - enterprise kernels
# 2) We only do i586 builds on Mandrake 8.2; all others get promoted to i686
if [ \( "$distro" = "rh"  -a "$version" -ge 72 \) -o \
     \( "$distro" = "mdk" -a "$version" -eq 81 \) -o \
     \( "$KERNEL_TYPE" = "enterprise" \) ]; then
    # We have i686/athlon builds, promote i586 builds to i686
    KERNEL_ARCH=`echo $KERNEL_ARCH | sed -e "s/i586/i686/"`
elif [ \( "$distro" = "mdk" -a "$version" -ge 82 \) ]; then
    # Do nothing, just stay our of the default case
    echo -n
elif [ "$KERNEL_ARCH" != "unknown" ]; then
    # We just have i386 builds
    KERNEL_ARCH="i386"
fi

# Set the GLX package name
GLX_PACKAGE=NVIDIA_GLX-${NVVERSION}.i386.rpm

# Print out the appropriate filenames
echo "Please download and install the following files:"

# Mandrake 8.2 needs a special case because of the kernel rpm name
if ( echo $KERNEL_VERSION | grep 2.4.18-6 > /dev/null ) ; then
    if [ -z "$KERNEL_TYPE" ]; then
	echo $kernel_list | tr ";" "\n" | grep "mdk82up" | cut -f 3 -d ":"
    else
	echo $kernel_list | tr ";" "\n" | grep "mdk82${KERNEL_TYPE}" | \
	     cut -f 3 -d ":"
    fi
    echo $GLX_PACKAGE
    exit
fi

CURRENT_KERNEL=`echo ${KERNEL_VERSION}${KERNEL_TYPE}`
LIST=`echo $kernel_list | tr ";" "\n" | grep "${CURRENT_KERNEL}:$KERNEL_ARCH"`

if [ -n "$LIST" ] ; then
    echo $LIST | cut -f 3 -d ":"
else
    echo $package_list | tr " " "\n" | grep "NVIDIA_kernel.*src\.rpm" | \
            cut -f 1 -d " "
fi
echo $GLX_PACKAGE
