#!/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-2802.rh61.i386.rpm MD5SUM: bd403a35a5d16fe3a11721d5266aaec2

RedHat Linux 6.2 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2802.rh62.i386.rpm MD5SUM: 2c73917675254ec1dfd5bc6f5450ace9

SuSE Linux 6.4 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2802.suse64.i386.rpm MD5SUM: 6420639f3ca9c6f4b2634c94694d7dbe

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

Mandrake Linux 7.1 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2802.mdk71.i386.rpm MD5SUM: 0bb1574ff97e50c58c00ad64465d9a1d

Mandrake Linux 7.2 UP Single Processor System i386 Architecture
NVIDIA_kernel-1.0-2802.mdk72.i386.rpm MD5SUM: 2c074750cb440a178c690cc270dc2d06

RedHat Linux 6.1 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2802.rh61.smp.i386.rpm MD5SUM: c72602e76373ccfe7ca4a6648eb8a51e

RedHat Linux 6.2 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2802.rh62.smp.i386.rpm MD5SUM: d54fb4fcb5c55aaf3d1764fbdb20b8e4

SuSE Linux 6.4 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2802.suse64.smp.i386.rpm MD5SUM: cd729b634b1e6fc54441ca02cb2135ba

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

Mandrake Linux 7.1 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2802.mdk71smp.i386.rpm MD5SUM: 4f9bb872ec4681f5948cdc3030ed45ec

Mandrake Linux 7.2 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2802.mdk72smp.i386.rpm MD5SUM: 6444f55980fb005ff4208bf3a48ae414

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

RedHat Linux 7.0 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2802.rh70smp.i386.rpm MD5SUM: d68703dff518577b2d108954abd271bb

RedHat Linux 7.1 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2802.rh71up.i386.rpm MD5SUM: 16ea397a84264dab77b6fb1cca9ae833

RedHat Linux 7.1 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2802.rh71smp.i386.rpm MD5SUM: 49a9d6cd7a7ccc134e6aced5310625a3

RedHat Linux 7.2 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2802.rh72up.i386.rpm MD5SUM: 79cb1118c5d875bc4454584cf5915f30

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

Mandrake Linux 8.0 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2802.mdk80up.i386.rpm MD5SUM: 0ebfd2ce08a6f75a01604e0c13c4e8e9

Mandrake Linux 8.0 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2802.mdk80smp.i386.rpm MD5SUM: 26d75b3fa065b8a0221bbdf09a6ede08

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

Mandrake Linux 8.1 SMP Multiprocessor i386 Architecture
NVIDIA_kernel-1.0-2802.mdk81smp.i386.rpm MD5SUM: 174def48a249ad46fc5e9c3cfb3c969b

RedHat Linux 7.0 UP Single Processor i386 Architecture
NVIDIA_kernel-1.0-2802.rh70up.i686.rpm MD5SUM: 2fb14dee404ecdb31aac585f7b8fc7e5

RedHat Linux 7.0 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-2802.rh70smp.i686.rpm MD5SUM: ee0803520143ddfcee90f96d558aebb2

RedHat Linux 7.0 Enterprise on i686 Architecture
NVIDIA_kernel-1.0-2802.rh70enterprise.i686.rpm MD5SUM: eb5a4a5e1c4ac91ff5fc57b4db2bcb9f

RedHat Linux 7.1 UP Single Processor i686 Architecture
NVIDIA_kernel-1.0-2802.rh71up.i686.rpm MD5SUM: 931b2c1abaa96aabac4dd109d844a250

RedHat Linux 7.1 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-2802.rh71smp.i686.rpm MD5SUM: 4b5f48697a252549333ec3934786d977

RedHat Linux 7.1 Enterprise i686 Architecture
NVIDIA_kernel-1.0-2802.rh71enterprise.i686.rpm MD5SUM: d4142e628eddcfa1cb43e2cc9ca68cc2

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

RedHat Linux 7.2 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-2802.rh72smp.i686.rpm MD5SUM: f026765b1873e425df9c22eea3c8ba69

RedHat Linux 7.2 Enterprise i686 Architecture
NVIDIA_kernel-1.0-2802.rh72enterprise.i686.rpm MD5SUM: 59c2ac6367c1afea8670fc1018b342b7

RedHat Linux 7.2 updated to 2.4.9-31 UP Single Processor i686 Architecture
NVIDIA_kernel-1.0-2802.rh72up_2.4.9_31.i686.rpm MD5SUM: 1206de594838ac8f77a4a208a4b381cd

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

RedHat Linux 7.2 updated to 2.4.9-31 Enterprise i686 Architecture
NVIDIA_kernel-1.0-2802.rh72enterprise_2.4.9_31.i686.rpm MD5SUM: 6e2d708ea28112fc02fbbf96eb7830f7

Mandrake Linux 8.0 UP Single Processor i686 Architecture
NVIDIA_kernel-1.0-2802.mdk80up.i686.rpm MD5SUM: 19c93ec4b6fe9d67c2cc9edd7abfb60a

Mandrake Linux 8.0 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-2802.mdk80smp.i686.rpm MD5SUM: ed7db024a002d6dcdb2bbd0747010f2f

Mandrake Linux 8.0 Enterprise i686 Architecture
NVIDIA_kernel-1.0-2802.mdk80enterprise.i686.rpm MD5SUM: 478a8144167bda6444b18943212c5c2e

Mandrake Linux 8.1 UP Single Processor i686 Architecture
NVIDIA_kernel-1.0-2802.mdk81up.i686.rpm MD5SUM: 64bd801afe891fa15f6a61e25dcbe121

Mandrake Linux 8.1 SMP Multiprocessor i686 Architecture
NVIDIA_kernel-1.0-2802.mdk81smp.i686.rpm MD5SUM: 3569385869acb056af3f6466d5a2b1d3

Mandrake Linux 8.1 Enterprise i686 Architecture
NVIDIA_kernel-1.0-2802.mdk81enterprise.i686.rpm MD5SUM: e9933cb271d04a7501ef613d3f1093f8

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

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

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

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

Common X and OpenGL for all Linux systems.
NVIDIA_GLX-1.0-2802.i386.rpm MD5SUM: 2490be4a3e303cd72a50a91053d8fd83

For systems not listed above
NVIDIA_kernel-1.0-2802.src.rpm MD5SUM: 2a60dcdc892532f2e559be2f80b353a3
NVIDIA_GLX-1.0-2802.src.rpm MD5SUM: b6be81941d738b2dcdd223ede31a3370

For systems that don't use RPM
NVIDIA_kernel-1.0-2802.tar.gz MD5SUM: dfde8afcae05f38a39bd3c7856aca314
NVIDIA_GLX-1.0-2802.tar.gz MD5SUM: 8ac546db964b58b916a1c220271086b3
"

# Driver Version
NVVERSION=1.0-2802

# 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

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

# 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`

# 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 they get promoted to i686
if [ \( "$distro" == "rh"  -a "$version" -ge 72 \) -o \
     \( "$distro" == "mdk" -a "$version" -ge 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 [ "$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

