#!/bin/sh

# Long list of all packages, should be taken from the manifest
package_list="
RedHat Linux 6.1 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2880.rh61.i386.rpm MD5SUM: 1d97f57bd62fdda9be9ae953712bf59d

RedHat Linux 6.2 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2880.rh62.i386.rpm MD5SUM: 5023346c929119407f39464344d83eaa

SuSE Linux 6.4 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2880.suse64.i386.rpm MD5SUM: 9bda7d73fc2670064dbc2c20516ddd63

SuSE Linux 6.4 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2880.suse70.i386.rpm MD5SUM: b2fd81815dd6f88fd5dcd1e305a4a3e2

Mandrake Linux 7.1 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2880.mdk71.i386.rpm MD5SUM: 77e5845480f3567cadd1e93872bbff42

Mandrake Linux 7.2 UP Single Processor System i386 Architecture
NVIDIA_kernel-1.0-2880.mdk72.i386.rpm MD5SUM: 9655ef8e7d9beedfcc583bf4b35e46c7

RedHat Linux 6.1 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2880.rh61.smp.i386.rpm MD5SUM: 03d0bfb49898c13300387130a6e2f54d

RedHat Linux 6.2 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2880.rh62.smp.i386.rpm MD5SUM: 0a48804a54bce06d1382de49353f4873

SuSE Linux 6.4 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2880.suse64.smp.i386.rpm MD5SUM: 52ded691496d996080216f8e423b420c

SuSE Linux 7.0 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2880.suse70.smp.i386.rpm MD5SUM: b1ea2be155a79f159128e6685311be43

Mandrake Linux 7.1 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2880.mdk71smp.i386.rpm MD5SUM: 66337bfe1fc5c845e5df78cec6d579a6

Mandrake Linux 7.2 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2880.mdk72smp.i386.rpm MD5SUM: a9e5e4df2e061880c5ac20c908bfd3a2

RedHat Linux 7.0 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2880.rh70up.i386.rpm MD5SUM: f677d7743c41511c4e052697980f3237

RedHat Linux 7.0 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2880.rh70smp.i386.rpm MD5SUM: 0f82cb3fb3b1fc45e776772638c7ccdc

RedHat Linux 7.1 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2880.rh71up.i386.rpm MD5SUM: 246553979a8463def1737ca907da3a8c

RedHat Linux 7.1 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2880.rh71smp.i386.rpm MD5SUM: f58dfa758f47ef7b67ad9adccbc3dfff

RedHat Linux 7.2 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2880.rh72up.i386.rpm MD5SUM: 6e76275237998a2e770ec03adf953b65

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

Mandrake Linux 8.0 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2880.mdk80up.i386.rpm MD5SUM: 774c482ff883189322e27508959b8fdf

Mandrake Linux 8.0 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2880.mdk80smp.i386.rpm MD5SUM: ae480e5d645bcfa74f9a3ba4af6f59cc

Mandrake Linux 8.1 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2880.mdk81up.i386.rpm MD5SUM: 9ce9406ae8df6a77aa9e72ee58176d3b

Mandrake Linux 8.1 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2880.mdk81smp.i386.rpm MD5SUM: 273fee9aac38beb54ea9db365cf8fe78

RedHat Linux 7.0 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2880.rh70up.i686.rpm MD5SUM: aeb424f35ce38cd566770bcfc812e2bc

RedHat Linux 7.0 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-2880.rh70smp.i686.rpm MD5SUM: 3dd7d847f92729732a5a4e1afb57e6c7

RedHat Linux 7.0 Enterprise on i686 Architecture
NVIDIA_kernel-1.0-2880.rh70enterprise.i686.rpm MD5SUM: c31d61e76b5b13fd32b0447dfc11a9b8

RedHat Linux 7.1 UP Single Processor i686 Architecture
NVIDIA_kernel-1.0-2880.rh71up.i686.rpm MD5SUM: 8bdfbf9202eaca8da918e5750d5962df

RedHat Linux 7.1 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-2880.rh71smp.i686.rpm MD5SUM: d6c92a7da238f5112082cdf957fde767

RedHat Linux 7.1 Enterprise i686 Architecture
NVIDIA_kernel-1.0-2880.rh71enterprise.i686.rpm MD5SUM: 75936e63077d0c1d58ff4af3946ffa22

RedHat Linux 7.2 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2880.rh72up.i686.rpm MD5SUM: 3adfd84de8aa2dd1803a032dfab4b94c

RedHat Linux 7.2 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-2880.rh72smp.i686.rpm MD5SUM: 36752f56fdfc35c537e987f5e8e46581

RedHat Linux 7.2 Enterprise i686 Architecture
NVIDIA_kernel-1.0-2880.rh72enterprise.i686.rpm MD5SUM: d805cd704e008aa4ae6d8f2c653494a3

RedHat Linux 7.2 updated to 2.4.9-31 UP Single Processor i686 Architecture
NVIDIA_kernel-1.0-2880.rh72up_2.4.9_31.i686.rpm MD5SUM: 554cc16da7eed53255f0f09fd6f0770b

RedHat Linux 7.2 updated to 2.4.9-31 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-2880.rh72smp_2.4.9_31.i686.rpm MD5SUM: 34330f25bd731a8e0b798951d399776c

RedHat Linux 7.2 updated to 2.4.9-31 Enterprise i686 Architecture
NVIDIA_kernel-1.0-2880.rh72enterprise_2.4.9_31.i686.rpm MD5SUM: 3815360e51039c27ca7ccc6b073a1b80

Mandrake Linux 8.0 UP Single Processor i686 Architecture
NVIDIA_kernel-1.0-2880.mdk80up.i686.rpm MD5SUM: 801552dbc67af53b07949eb65482670e

Mandrake Linux 8.0 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-2880.mdk80smp.i686.rpm MD5SUM: 75cd2564cd85727ce600fe1cd34ac89f

Mandrake Linux 8.0 Enterprise i686 Architecture
NVIDIA_kernel-1.0-2880.mdk80enterprise.i686.rpm MD5SUM: 88858424087aa6873867e1c711859c13

Mandrake Linux 8.1 UP Single Processor i686 Architecture
NVIDIA_kernel-1.0-2880.mdk81up.i686.rpm MD5SUM: 9f19ca2f8d89b35d4143cea2b0c43a54

Mandrake Linux 8.1 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-2880.mdk81smp.i686.rpm MD5SUM: 3b29fa1e0e93be4361cde62c9f9a3fb9

Mandrake Linux 8.1 Enterprise i686 Architecture
NVIDIA_kernel-1.0-2880.mdk81enterprise.i686.rpm MD5SUM: 4406a5c008f0032664d8b047f8d7d656

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

RedHat Linux 7.2 SMP Multiprocessor Athlon Architecture
NVIDIA_kernel-1.0-2880.rh72smp.athlon.rpm MD5SUM: 236a43ce09ebbd527a51c74959f6380d

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

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

Mandrake Linux 8.2 UP Single Processor i586 Architecture
NVIDIA_kernel-1.0-2880.mdk82up.i586.rpm MD5SUM: 177c3bf24087bee5f6493947f5705989

Mandrake Linux 8.2 SMP Multiprocessor i586 Architecture
NVIDIA_kernel-1.0-2880.mdk82smp.i586.rpm MD5SUM: 014ae584e261fc8ad02d30ee4b590da7

Mandrake Linux 8.1 Enterprise i586 Architecture
NVIDIA_kernel-1.0-2880.mdk82enterprise.i586.rpm MD5SUM: c9d38dc678afbb7a33faac8dc3f25661

Mandrake Linux 8.2 Secure i586 Architecture
NVIDIA_kernel-1.0-2880.mdk82secure.i586.rpm MD5SUM: e252b8b7ab70b0135e9a327443bd9d00

Common X and OpenGL for all Linux systems.
NVIDIA_GLX-1.0-2880.i386.rpm MD5SUM: 53c14a0969555c0de24167070d50e4d3

For systems not listed above
NVIDIA_kernel-1.0-2880.src.rpm MD5SUM: 1dce0a038f930ea94f56168c21185dcc
NVIDIA_GLX-1.0-2880.src.rpm MD5SUM: d3b2fba0cc428a3d350385b3b460d204

For systems that don't use RPM
NVIDIA_kernel-1.0-2880.tar.gz MD5SUM: 20fdf77ca76a13987da691eb4d7fe7c9
NVIDIA_GLX-1.0-2880.tar.gz MD5SUM: 60cb303e5bb2ac424b865f6fa163b989
"

# Driver Version
NVVERSION=1.0-2880

# Determine which distribution and version is running

# 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

# 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 tar ball
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

# XXX Make this support command line options to specify a kernel that isn't
# the running kernel.

# Determine architecture (strip mdk out if its present)
KERNEL_TYPE=`uname -r | tr -d "0-9.-" | sed -e "s/mdk//"`
KERNEL_VERSION=`uname -r | tr -d "a-zA-Z"`
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
    # On mandrake we have to add mdk to the version (it was in the uname but 
    # we stripped it out with  tr above, we want to strip it out because
    # it has other stuff as well)
    if [ "$distro" == "mdk" ]; then 
        KERNEL_PACKAGE=${KERNEL_PACKAGE}mdk
    fi
fi

# Which kernel, matching the above, is actually installed?  Get the arch
# from it.  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, since some distro's name their
    # packages in different way's
    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

# Fixup KERNEL_VERSION change '-' to '_' since that's the way the RPM is 
# named. (For whatever reason RPM didn't allow a - in that field in the name.)
KERNEL_VERSION=`echo $KERNEL_VERSION | tr "-" "_"`

# Ok, lots of rules for patching up the ARCH
# 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 don't do i586 builds, except on Mandrake 8.2, they 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



# Two different naming styles, one for update kernels, one for standard 
# kernels.  We append "up" if there is no type
if [ -z "$KERNEL_TYPE" ]; then
    KERNEL_TYPE=up
fi
UPDATE_PACKAGE=NVIDIA_kernel-${NVVERSION}.${distro}${version}${KERNEL_TYPE}_${KERNEL_VERSION}.${KERNEL_ARCH}.rpm
STANDARD_PACKAGE=NVIDIA_kernel-${NVVERSION}.${distro}${version}${KERNEL_TYPE}.${KERNEL_ARCH}.rpm
GLX_PACKAGE=NVIDIA_GLX-${NVVERSION}.i386.rpm

echo "Please download and install the following files:"
# First assume we have an update kernel and see if its in the list
if echo -e $package_list | tr " " "\n" | grep "$UPDATE_PACKAGE" > /dev/null ; then
    echo $UPDATE_PACKAGE
# If its not an update kernel then try it as a standard kernel
elif echo -e $package_list | tr " " "\n" | grep "$STANDARD_PACKAGE" > /dev/null ; then
    echo $STANDARD_PACKAGE
else
    # if its not a standard kernel or an update kernel then recommend the
    # src rpm
    echo $package_list | tr " " "\n" | grep "NVIDIA_kernel.*src\.rpm" | \
            cut -f 1 -d " "
fi
echo $GLX_PACKAGE
