#!/bin/bash -x

export arch=amd64
if [ $# == 0 ]; then
  echo "assuming arch = amd64"
  echo "override with $0 ia32|ia64|amd64"
else
  export arch=$1
fi
if [ $2 ]; then
  export kver=$2
else
  export kver=`uname -r`
fi
export carch=`echo $arch | tr [:lower:] [:upper:]`

if [ ! $(which links2) ]; then
  echo "links2 not found, dying"
  exit 1
fi

export ver=$(links2 -source http://www.nvidia.com/object/linux.html | grep STRONG.*${carch} | cut -d\> -f6 | cut -d\< -f1 | head -1)
export prefix=/tmp/${arch}_${ver}
#export ver=1.0-7174

case $arch in
  amd64)  if [ ! -f NVIDIA-Linux-x86_64-${ver}-pkg2.run ]; then
            wget http://download.nvidia.com/XFree86/Linux-x86_64/${ver}/NVIDIA-Linux-x86_64-${ver}-pkg2.run
	  fi
          echo "Adding the new kernel interface"
	  if [ -d $prefix ]; then
	    rm -rf $prefix
	  fi
	  mkdir -p $prefix
          sudo rmmod nvidia
          sudo sh NVIDIA-Linux-x86_64-${ver}-pkg2.run --accept-license \
	    --ui=none \
	    --kernel-name=$kver #\
#            compat32 /emul/ia32-linux
#	    --x-prefix=$prefix/usr.X11R6 \
#	    --installer-prefix=$prefix/usr \
#	    --utility-prefix=$prefix/usr
	  sudo /sbin/modprobe nvidia
          ;;
esac

