#!/bin/sh
UNSUPPORTED=1
LEGACY=0
#CARDS="$(lspci -n|gawk '{sub("Class","")} $2 == "0300:" {print$3}')"
CARDS="$(lspci -n|perl -e 'while(<>){(push(@cards,$_)) if (s/^.+?\s*0300\:\s+(.+?)\s+.*$/$1/)} print join(" ",@cards);')"
for card in $CARDS; do
 VENDOR=${card%:*}
 DEVICE=${card#*:}
 case $VENDOR in
  10de|12d2)
   case $DEVICE in
    0008|0009|0010|0018|0019) UNSUPPORTED=1 ;;
    0020|0028|0029|002c|002d|00a0|0100|0101|0103|0150|0151|0152|0153) LEGACY=1;UNSUPPORTED=0 ;;
    0110|0111|0112|0113|0170|0171|0172|0173|0174|0175|0176|0177|0178|0179|017a|017c|017d|0181|0182|0183|0185|0188|018a|018b|018c|01a0|01f0|0200|0201|0202|0203|0250|0251|0253|0258|0259|025b|0280|0281|0282|0286|0288|0289|028c) LEGACY=2;UNSUPPORTED=0 ;;
    #0170) LEGACY=1;UNSUPPORTED=0 ;; # GeForce4 MX 460 workaround
    *) UNSUPPORTED=0 ;;
   esac
   ;;
 esac
done
if [ ! -f /etc/kanotix-version ]; then
 echo Error: No KANOTIX found. Do not ask for support! >&2
fi
if [ ! -f /etc/debian_version ]; then
 echo Error: No Debian compatible system found. Do not ask for support! >&2
 exit 100
fi
if [ "$UNSUPPORTED" = "1" ]; then
 echo Error: No supported nVidia VGA adapter found. >&2
 exit 3
fi
if [ "$(id -u)" != "0" ]; then
 echo Error: You must be root to run this script! >&2
 exit 4
fi
[ -d /KNOPPIX -o -d /KANOTIX ] && INSTALLED="no" || INSTALLED="yes"
if [ -f /etc/default/distro ]; then
 . /etc/default/distro
 [ "$FLL_DISTRO_MODE" = "live" ] && INSTALLED="no"
fi
if [ "$INSTALLED" = "yes" ]; then
 if [ -n "$DISPLAY" ]; then
  echo Error: Can\`t be run within X! Use a text console \(ALT-CTRL-F1\) >&2
  exit 5
 fi
else
 if grep -q -e unionfs -e aufs /proc/mounts; then
  echo Unionfs found. >&2
 else
  echo Activating Unionfs now. >&2
  UNIONFS=unionfs
  modinfo aufs >/dev/null 2>&1 && UNIONFS=aufs
  mkdir -p /tmp/union
  UNION=/KNOPPIX
  [ -d /KANOTIX ] && UNION=/KANOTIX
  [ -d "$FLL_MOUNTPOINT" ] && UNION="$FLL_MOUNTPOINT"
  mount -t $UNIONFS -o dirs=/tmp/union/:$UNION none $UNION/ >/dev/null 2>&1
 fi
fi
X_CONF=XF86Config-4
if which Xorg >/dev/null; then
 [ -e /etc/X11/xorg.conf ] && X_CONF=xorg.conf
fi

AGPRATE=0
# AGPRATE can be 15 (8x), 4, 2, 1 or 0 (auto)
FW=0
# FW can be 0 (off) or 1 (on)
EMUL=1
OLDEMUL=0
COMPOSITE=1
NRC=1
USEEDID=1
USEEDIDFREQS=1
RMML=0
XVMC=1
TV=0
DYN_TWINVIEW=0
VER_FORCE=""
PRINT_VER=0
NO_X_KILL=0
AUTO_SELECT=0
UDD=""

while getopts :acdefg:nprtlqxv:zh opt; do
 case $opt in
  a) AUTO_SELECT=1
     ;;
  c) COMPOSITE=0
     ;;
  d) DYN_TWINVIEW=1
     ;;
  e) USEEDID=0
     ;;
  f) FW=1
     ;;
  g) UDD="$OPTARG"
     ;;
  n) NRC=0
     ;;
  p) PRINT_VER=1
     ;;
  r) RMML=1
     ;;
  t) TV=1
     ;;
  l) EMUL=0
     ;;
  q) USEEDIDFREQS=0
     ;;
  x) XVMC=0
     ;;
  v) VER_FORCE="$OPTARG"
     ;;
  z) NO_X_KILL=1
     ;;
  :) case $OPTARG in
      g) UDD="DFP"
         ;;
      v) echo "Warning: -v option detected but no override specified!" >&2
         ;;
     esac
     ;;
  \?|h) echo "Usage $0 [options] " >&2
	echo "optional options are:" >&2
	echo " -a      Use Auto-Selection for screen res" >&2
        echo " -c      Disable Composite extension" >&2
	echo " -d      Enable DynamicTwinView" >&2
        echo " -e      Disable UseEDID" >&2
	echo " -f      Enable FastWrites" >&2
	echo " -g      UseDisplayDevice [DFP|CRT|TV] (default DFP)" >&2
	echo " -n      Disable DFP Native Resolution Check" >&2
	echo " -r      Remove ModeLines" >&2
        echo " -t      Enable TV-Out" >&2
	echo " -q      Disable UseEdidFreqs" >&2
	echo " -x      Disable XvMC" >&2
	echo " -l      Disable 32 bit emulation libs in 64 bit mode" >&2
	echo " -v VER  Force use of specific driver build. Example: -v 1.0-9629 or -v latest" >&2
        echo " -p      Print driver version to be installed and quit" >&2
        echo " -z      No X kill/restart. Special purpose only!" >&2
        echo " -h      Show this help" >&2
	exit 6
        ;;
 esac
done

# check for pipe usage
[ "$0" = "sh" -o "$0" = "bash" ] && NO_X_KILL=1

# old DX7 cards up to gf2gts/pro
VER_LEGACY_1=71.86.04

# DX7 cards since gf2mx and all DX8 cards
VER_LEGACY_2=96.43.05

VER=174.55
#VER=173.08
#VER=171.06.01
#VER=171.06
#VER=171.05
#VER=169.12
#VER=169.09
#VER=169.07
#VER=169.04
#VER=165.33.09
#VER=100.14.23
#VER=100.14.19
#VER=100.14.11
#VER=100.14.09
#VER=100.14.06
#VER=100.14.03
#VER=96.43.05
#VER=96.43.01
#VER=71.86.04
#VER=71.86.01
#VER=1.0-9755
#VER=1.0-9746
#VER=1.0-9742
#VER=1.0-9639
#VER=1.0-9631
#VER=1.0-9629
#VER=1.0-9626
#VER=1.0-9625
#VER=1.0-8776
#VER=1.0-8774
#VER=1.0-8762
#VER=1.0-8756
#VER=1.0-8178
#VER=1.0-8174
#VER=1.0-7676
#VER=1.0-7667
#VER=1.0-7664
#VER=1.0-7185
#VER=1.0-7184
#VER=1.0-7182
#VER=1.0-7174
#VER=1.0-7167
#VER=1.0-6629
#VER=1.0-6111
#VER=1.0-6106
#VER=1.0-5336
#VER=1.0-5328
#VER=1.0-4620

[ "$VER" = "1.0-8776" -a "$COMPOSITE" = "1" ] && VER=1.0-9631

case $VER in
 1.0-7664|1.0-7667|1.0-7676|1.0-8174|1.0-8178|1.0-8756|1.0-8762|1.0-8774|1.0-8776|1.0-9625|1.0-9626|1.0-9629|1.0-9631|96.43.01|96.43.05)
  [ "$LEGACY" = "1" ] && VER=$VER_LEGACY_1 ;;
 1.0-9742|1.0-9746|1.0-9755|100.14.03|100.14.06|100.14.09|100.14.11|100.14.19|100.14.23|165.33.09|169.04|169.07|169.09|169.12|171.05|171.06|171.06.01|173.08|174.55)
  if [ "$LEGACY" = "1" ]; then
   VER=$VER_LEGACY_1
  elif [ "$LEGACY" = "2" ]; then
   VER=$VER_LEGACY_2
  fi ;;
esac

SERVER=de.download.nvidia.com
ARCH=""
[ "$(uname -m)" = "x86_64" ] && ARCH="_64"
[ "$VER" = "1.0-7664" -a "$ARCH" = "_64" ] && VER=1.0-7184

if [ -n "$VER_FORCE" ]; then
 if [ "${#VER_FORCE}" = "4" ]; then
  VER=1.0-$VER_FORCE
 elif [ "$VER_FORCE" = "latest" ]; then
  VER_DE="$(wget -qO- http://de.download.nvidia.com/XFree86/Linux-x86$ARCH/latest.txt|cut -f1 -d' ')"
  VER_US="$(wget -qO- http://us.download.nvidia.com/XFree86/Linux-x86$ARCH/latest.txt|cut -f1 -d' ')"
  [ -n "$VER_DE" ] && VER=$VER_DE
  [ -n "$VER_US" ] && VER=$VER_US
  if [ -n "$VER_DE" -a -n "$VER_US" -a "$VER_DE" != "$VER_US" ]; then
   SERVER=us.download.nvidia.com
   VER=$VER_US
  fi
 else
  VER=$VER_FORCE
 fi
fi

if [ "$PRINT_VER" = "1" ]; then
 echo $VER
 exit
fi

DVER=1.0.${VER#1.0-}
case $VER in 1.0-7184|1.0-7185|1.0-9742|71.86.01|96.43.01) SERVER=us.download.nvidia.com ;; esac
URL=http://$SERVER/XFree86/Linux-x86$ARCH/$VER/
EURL=http://$SERVER/XFree86/Linux-x86/$VER/
PKG=0
[ "$VER" = "165.33.09" ] && PKG=1
if [ "$VER" = "174.55" ]; then
 PKG=1
 [ "$ARCH" = "_64" ] && PKG=2
 URL=http://developer.download.nvidia.com/compute/cuda/2.0-Beta/
 EURL=$URL
fi
[ "$ARCH" = "_64" -a "$EMUL" = "1" ] && PKG=2
PATCH=0
HACK=0
UDEVHACK=0
[ "$VER" = "1.0-7174" ] && UDEVHACK=1
KVER=$(uname -r)
KV=$(uname -r|cut -d. -f1-2)
cd /usr/src
[ -f NVIDIA-Linux-x86$ARCH-$VER-pkg$PKG.run ] || wget -Nc "$URL"NVIDIA-Linux-x86$ARCH-$VER-pkg$PKG.run
rm -rf NVIDIA-Linux-x86$ARCH-$VER-pkg$PKG
bash NVIDIA-Linux-x86$ARCH-$VER-pkg$PKG.run --extract-only >/dev/null 2>&1 || (
 rm -f NVIDIA-Linux-x86$ARCH-$VER-pkg$PKG.run
 echo NVIDIA-Linux-x86$ARCH-$VER-pkg$PKG.run could not be extracted >&2
 echo and has been deleted. Please restart this script. >&2
 exit 1
) || exit $?
if [ "$ARCH" = "_64" -a "$OLDEMUL" = "1" ]; then
 [ -f NVIDIA-Linux-x86-$VER-pkg$PKG.run ] || wget -Nc "$EURL"NVIDIA-Linux-x86-$VER-pkg$PKG.run
 rm -rf NVIDIA-Linux-x86-$VER-pkg$PKG
 bash NVIDIA-Linux-x86-$VER-pkg$PKG.run --extract-only >/dev/null 2>&1 || (
  rm -f NVIDIA-Linux-x86-$VER-pkg$PKG.run
  echo NVIDIA-Linux-x86-$VER-pkg$PKG.run could not be extracted >&2
  echo and has been deleted. Please restart this script. >&2
  exit 2
 ) || exit $?
fi
if [ "$KV" = "2.6" -a "$PATCH" = "1" ]; then
 wget -Nc http://www.minion.de/files/NVIDIA_kernel-$VER-2.6.diff
fi
if [ -f /etc/modutils/1alsa ]; then
 mv /lib/modules/extra/alsa /lib/modules/`uname -r`/
 rm -f /etc/modutils/1alsa
 depmod -a
fi
if ! [ "$NO_X_KILL" = "1" ]; then
 if [ "$INSTALLED" = "yes" ]; then
  DM=$(cat /etc/X11/default-display-manager 2>/dev/null)
  DM=$(basename $DM 2>/dev/null)
  if [ -z "$DM" ]; then
   if [ -x /etc/init.d/kdm ]; then
    /etc/init.d/kdm stop
   elif [ -x /etc/init.d/gdm ]; then
    /etc/init.d/gdm stop
   elif [ -x /etc/init.d/xdm ]; then
    /etc/init.d/xdm stop
   fi
  else
   [ -x /etc/init.d/$DM ] && /etc/init.d/$DM stop
  fi
 fi
fi
killall -9 Xgl >/dev/null 2>&1
rmmod nvidia >/dev/null 2>&1
rm -f /lib/modules/`uname -r`/kernel/drivers/video/nvidia.*o
rm -f /lib/modules/`uname -r`/volatile/nvidia.*o
rm -f /lib/modules/$(uname -r)/updates/dkms/nvidia.*o
rm -f /usr/local/bin/nvidia-settings /usr/local/bin/nvidia-xconfig
OLD_NVIDIA=$(COLUMNS=200 dpkg -l|cut -f3 -d' '|grep nvidia)
[ -n "$OLD_NVIDIA" ] && dpkg --purge $OLD_NVIDIA
# Ubuntu workaround
[ -f /etc/default/linux-restricted-modules-common ] && perl -pi -e 's/^(DISABLED_MODULES)=.*/\1="nv"/' /etc/default/linux-restricted-modules-common
[ -x /sbin/lrm-manager ] && /sbin/lrm-manager

unset UP

# Ubuntu universe workaround
[ -r /etc/lsb-release ] && . /etc/lsb-release
if [ "$DISTRIB_ID" = "Ubuntu" ]; then
 if ! grep -Eq "^[[:space:]]*deb[[:space:]]+.*universe$" /etc/apt/sources.list; then
  grep -Eq "deb[[:space:]]+.*universe$" /etc/apt/sources.list && sed -i 's/^#\s*\(deb\s.*universe\)$/\1/' /etc/apt/sources.list
 fi
fi

if ! [ -x /usr/bin/module-assistant ]; then
 apt-get update && UP=1
 apt-get install --allow-unauthenticated --yes module-assistant
fi

if ! [ -d /usr/share/build-essential ]; then
 [ -z "$UP" ] && apt-get update
 apt-get install --allow-unauthenticated --yes build-essential
fi

if ! [ -x /usr/bin/module-assistant ]; then
 echo "Error: module-assistant missing." >&2
 [ -r /etc/lsb-release ] && . /etc/lsb-release
 [ "$DISTRIB_ID" = "Ubuntu" ] && echo "Hint: Enable universe repository in /etc/apt/sources.list." >&2
 exit 1
fi

# Debian (and Ubuntu)
[ -f /etc/debian_version -a ! -f /etc/kanotix-version ] && m-a -i prepare

# Kanotix fallback
[ -f /etc/kanotix-version -a "$(readlink -e /lib/modules/$(uname -r)/build)" = "" ] && m-a -i prepare

# Header hotfix
[ -d /lib/modules/$(uname -r)/build/include/asm-x86 ] && ln -fs asm-x86 /lib/modules/$(uname -r)/build/include/asm-i386

cd NVIDIA-Linux-x86$ARCH-$VER-pkg$PKG
if [ "$KV" = "2.6" ]; then 
 if [ "$PATCH" = "1" ]; then
  cd usr/src/nv
  patch -p1 < ../../../../NVIDIA_kernel-$VER-2.6.diff
  ln -s Makefile.kbuild Makefile
  cd ../../..
 fi
else
 # fix NVIDIA's stupid error in Makefile 
 perl -pi -e 's/"\$\$\?"/"\$\?"/' Makefile
fi
[ "$(uname -r|cut -d- -f1)" = "2.6.10" ] && perl -pi -e s/pci_find_class/pci_get_class/ usr/src/nv/nv.c
if [ "$(uname -r|grep -E '2.6.11')" != "" -a "$VER" = "1.0-6629" ]; then
 #PATCH_BASE_URL="http://www.minion.de/files/1.0-6629/"
 PATCH_BASE_URL="http://kanotix.com/files/1.0-6629/"
 PATCHES="NVIDIA_kernel-1.0-6629-1155389.diff NVIDIA_kernel-1.0-6629-1161283.diff NVIDIA_kernel-1.0-6629-1165235.diff NVIDIA_kernel-1.0-6629-1171869.diff NVIDIA_kernel-1.0-6629-1175225.diff NVIDIA_kernel-1.0-6629-1182399.diff NVIDIA_kernel-1.0-6629-1189413.diff NVIDIA_kernel-1.0-6629-1201042.diff"
 for p in $PATCHES; do
  #wget -qO- $PATCH_BASE_URL$p|patch -p0
  [ -f /usr/src/$p ] || wget -NqO/usr/src/$p $PATCH_BASE_URL$p
  patch -p0 </usr/src/$p || rm -f /usr/src/$p
 done
fi
if [ "$VER" = "1.0-7167" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/1.0-7167/"
 PATCHES="NVIDIA_kernel-1.0-7167-1233959.diff NVIDIA_kernel-1.0-7167-1237815.diff"
 for p in $PATCHES; do
  #wget -qO- $PATCH_BASE_URL$p|patch -p0
  [ -f /usr/src/$p ] || wget -NqO/usr/src/$p $PATCH_BASE_URL$p
  patch -p0 </usr/src/$p || rm -f /usr/src/$p
 done
fi
if [ "$VER" = "1.0-7174" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/1.0-7174/"
 PATCHES="NVIDIA_kernel-1.0-7174-1258475.diff NVIDIA_kernel-1.0-7174-1296092.diff NVIDIA_kernel-1.0-7174-1321905.diff NVIDIA_kernel-1.0-7174-1361053.diff"
 for p in $PATCHES; do
  #wget -qO- $PATCH_BASE_URL$p|patch -p0
  [ -f /usr/src/$p ] || wget -NqO/usr/src/$p $PATCH_BASE_URL$p
  patch -p0 </usr/src/$p || rm -f /usr/src/$p
 done
fi
if [ "$VER" = "1.0-7174" -a "$ARCH" = "_64" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/1.0-7174/"
 PATCHES="NVIDIA_kernel-1.0-7174-1386866.diff"
 for p in $PATCHES; do
  #wget -qO- $PATCH_BASE_URL$p|patch -p0
  [ -f /usr/src/$p ] || wget -NqO/usr/src/$p $PATCH_BASE_URL$p
  patch -p0 </usr/src/$p || rm -f /usr/src/$p
 done
fi
if [ "$VER" = "1.0-7184" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/1.0-7184/"
 PATCHES="NVIDIA_kernel-2.6.19-fixups.diff"
 for p in $PATCHES; do
  #wget -qO- $PATCH_BASE_URL$p|patch -p0
  [ -f /usr/src/$p ] || wget -NqO/usr/src/$p $PATCH_BASE_URL$p
  patch -p0 </usr/src/$p || rm -f /usr/src/$p
 done
fi
if [ "$VER" = "1.0-7676" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/1.0-7676/"
 PATCHES="NVIDIA_kernel-1.0-7676-1321905.diff NVIDIA_kernel-1.0-7676-1361053.diff NVIDIA_kernel-1.0-7676-1392404.diff"
 for p in $PATCHES; do
  #wget -qO- $PATCH_BASE_URL$p|patch -p0
  [ -f /usr/src/$p ] || wget -NqO/usr/src/$p $PATCH_BASE_URL$p
  patch -p0 </usr/src/$p || rm -f /usr/src/$p
 done
fi
if [ "$VER" = "1.0-7676" -a "$ARCH" = "_64" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/1.0-7676/"
 PATCHES="NVIDIA_kernel-1.0-7676-1386866.diff"
 for p in $PATCHES; do
  #wget -qO- $PATCH_BASE_URL$p|patch -p0
  [ -f /usr/src/$p ] || wget -NqO/usr/src/$p $PATCH_BASE_URL$p
  patch -p0 </usr/src/$p || rm -f /usr/src/$p
 done
fi
if [ "$VER" = "1.0-8174" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/1.0-8174/"
 PATCHES="NVIDIA_kernel-1.0-8174-1423627.diff NVIDIA_kernel-1.0-8174-1427453.diff NVIDIA_kernel-1.0-8174-1430812.diff"
 for p in $PATCHES; do
  #wget -qO- $PATCH_BASE_URL$p|patch -p0
  [ -f /usr/src/$p ] || wget -NqO/usr/src/$p $PATCH_BASE_URL$p
  patch -p0 </usr/src/$p || rm -f /usr/src/$p
 done
fi	  
if [ "$VER" = "1.0-8178" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/1.0-8178/"
 PATCHES="NVIDIA_kernel-1.0-8178-1423627.diff NVIDIA_kernel-1.0-8178-1427453.diff NVIDIA_kernel-1.0-8178-1435131.diff NVIDIA_kernel-1.0-8178-1450608.diff NVIDIA_kernel-1.0-8178-1451498.diff NVIDIA_kernel-1.0-8178-1453708.diff"
 for p in $PATCHES; do
  #wget -qO- $PATCH_BASE_URL$p|patch -p0
  [ -f /usr/src/$p ] || wget -NqO/usr/src/$p $PATCH_BASE_URL$p
  patch -p0 </usr/src/$p || rm -f /usr/src/$p
 done
fi
if [ "$VER" = "1.0-8762" -o "$VER" = "1.0-7182" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/1.0-8762/"
 PATCHES="NVIDIA_kernel-1.0-8762-U072006.diff"
 for p in $PATCHES; do
  #wget -qO- $PATCH_BASE_URL$p|patch -p0
  [ -f /usr/src/$p ] || wget -NqO/usr/src/$p $PATCH_BASE_URL$p
  patch -p0 </usr/src/$p || rm -f /usr/src/$p
 done
fi
# NOSMBUS patch enabled for 1.0-9631 too for 2.6.20
if [ "$VER" = "1.0-9625" -o "$VER" = "1.0-9626" -o "$VER" = "1.0-9631" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/1.0-9625/"
 PATCHES="NVIDIA_kernel-1.0-9625-NOSMBUS.diff"
 for p in $PATCHES; do
  #wget -qO- $PATCH_BASE_URL$p|patch -p0
  [ -f /usr/src/$p ] || wget -NqO/usr/src/$p $PATCH_BASE_URL$p
  patch -p0 </usr/src/$p || rm -f /usr/src/$p
 done
fi
if [ "$VER" = "1.0-9625" -o "$VER" = "1.0-9626" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/1.0-9625/"
 PATCHES="NVIDIA_kernel-2.6.19.patch"
 for p in $PATCHES; do
  #wget -qO- $PATCH_BASE_URL$p|patch -p1
  [ -f /usr/src/$p ] || wget -NqO/usr/src/$p $PATCH_BASE_URL$p
  patch -p1 </usr/src/$p || rm -f /usr/src/$p
 done
fi
[ "$VER" = "1.0-9742" ] && sed -i s/==/=/ usr/src/nv/conftest.sh

if [ "$VER" = "100.14.11" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/100.14.11/"
 PATCHES="NVIDIA_kernel-100.14.11-336672.diff"
 for p in $PATCHES; do
  #wget -qO- $PATCH_BASE_URL$p|patch -p1
  [ -f /usr/src/$p ] || wget -NqO/usr/src/$p $PATCH_BASE_URL$p
  patch -p0 </usr/src/$p || rm -f /usr/src/$p
 done
fi

if [ "$(uname -r|grep -E '2.6.25')" != "" -a "$VER" = "71.86.04" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/71.86.04/"
 PATCHES="NVIDIA_kernel-71.86.04-2305230.diff"
 for p in $PATCHES; do
  #wget -qO- $PATCH_BASE_URL$p|patch -p0
  [ -f /usr/src/$p ] || wget -NqO/usr/src/$p $PATCH_BASE_URL$p
  patch -p0 </usr/src/$p || rm -f /usr/src/$p
 done
fi

if [ "$(uname -r|grep -E '2.6.25')" != "" -a "$VER" = "96.43.05" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/96.43.05/"
 PATCHES="NVIDIA_kernel-96.43.05-2290218.diff"
 for p in $PATCHES; do
  #wget -qO- $PATCH_BASE_URL$p|patch -p0
  [ -f /usr/src/$p ] || wget -NqO/usr/src/$p $PATCH_BASE_URL$p
  patch -p0 </usr/src/$p || rm -f /usr/src/$p
 done
fi

if [ "$(uname -r|grep -E '2.6.25')" != "" ] &&  [ "$VER" = "169.12" -o "$VER" = "171.06" -o "$VER" = "171.06.01" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/169.12/"
 PATCHES="NVIDIA_kernel-169.12-2286310.diff"
 for p in $PATCHES; do
  #wget -qO- $PATCH_BASE_URL$p|patch -p0
  [ -f /usr/src/$p ] || wget -NqO/usr/src/$p $PATCH_BASE_URL$p
  patch -p0 </usr/src/$p || rm -f /usr/src/$p
 done
fi

if [ "$(uname -r|grep -E '2.6.26')" != "" ] && [ "$VER" = "173.08" -o "$VER" = "174.55" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/173.08/"
 PATCHES="NVIDIA_kernel-173.08-2404825.diff"
 for p in $PATCHES; do
  #wget -qO- $PATCH_BASE_URL$p|patch -p0
  [ -f /usr/src/$p ] || wget -NqO/usr/src/$p $PATCH_BASE_URL$p
  patch -p0 </usr/src/$p || rm -f /usr/src/$p
 done
fi

if grep -q CONFIG_XEN=y /boot/config-$(uname -r) && [ ${VER/.*} -ge 165 ]; then
 export IGNORE_XEN_PRESENCE=1
 patch -p1 <<'EOT'
diff -N'urpx*~' nv/nv.c NVIDIA-Linux-x86_64-169.04-pkg2-f/usr/src/nv/nv.c
--- nv/nv.c	2007-11-15 02:43:30.000000000 +0200
+++ NVIDIA-Linux-x86_64-169.04-pkg2-f/usr/src/nv/nv.c	2007-12-07 15:56:38.000000000 +0200
@@ -1995,7 +1995,8 @@ int nv_kern_open(
         nvl->tasklet.data = (unsigned long) nv;
         tasklet_enable(&nvl->tasklet);
 
-#if defined(CONFIG_XEN)
+#if defined(CONFIG_XEN) && 0
+	/* (anssi 12/2007) FATAL: modpost: GPL-incompatible module nvidia.ko uses GPL-only symbol 'hypercall_page' */
         if ((nvl->dev->dma_mask <= 0xffffffff) &&
             (HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL) > 0xfffff))
         {
diff -N'urpx*~' nv/nv-linux.h NVIDIA-Linux-x86_64-169.04-pkg2-f/usr/src/nv/nv-linux.h
--- nv/nv-linux.h	2007-11-15 02:43:30.000000000 +0200
+++ NVIDIA-Linux-x86_64-169.04-pkg2-f/usr/src/nv/nv-linux.h	2007-12-07 15:59:44.000000000 +0200
@@ -109,7 +109,10 @@
 #include <linux/highmem.h>
 
 #ifdef CONFIG_XEN
-#include <asm/maddr.h>
+/* HYPERVISOR_memory_op() in nv.c */
+#include <asm/xen/hypercall.h>
+/* phys_to_machine() */
+#include <xen/page.h>
 #include <xen/interface/memory.h>
 #endif
 
@@ -712,8 +715,9 @@ static inline int nv_execute_on_all_cpus
 #define NV_TASK_STRUCT_RLIM(current)  ((current)->rlim)
 #endif
 
-#ifdef CONFIG_XEN
-#define NV_GET_DMA_ADDRESS(phys_addr) phys_to_machine(phys_addr)
+#if defined(CONFIG_XEN) && 0
+/* (anssi 12/2007) FATAL: modpost: GPL-incompatible module nvidia.ko uses GPL-only symbol 'xen_features' */
+#define NV_GET_DMA_ADDRESS(phys_addr) phys_to_machine(XPADDR(phys_addr)).maddr
 #else
 #define NV_GET_DMA_ADDRESS(phys_addr) (phys_addr)
 #endif
EOT
fi

if [ "$FW" = "1" ]; then
 perl -pi -e 's/(NVreg_EnableAGPFW =) .*/\1 1;/' usr/src/nv/os-registry.c
fi
if [ "$AGPRATE" != "0" ]; then
 perl -pi -e "s/^(static int NVreg_ReqAGPRate =) 15/\1 $AGPRATE/" usr/src/nv/os-registry.c
 perl -pi -e "s/(\s*&NVreg_ReqAGPRate,\s*)0/\${1}1/" usr/src/nv/os-registry.c
fi
CC=gcc-$(perl -pe '($_)=/gcc.[Vv]ersion\s(4\.\d|3\.\d|2\.\d+)/;' /proc/version)
if [ -e /usr/bin/$CC ]; then
 CC=/usr/bin/$CC
else
 unset CC
fi
export CC

BAD_INSTALL=0
if [ -f Makefile -a ! -x ./nvidia-installer ]; then
 if [ -e /usr/bin/$CC ]; then
  make install CC=$CC IGNORE_CC_MISMATCH=1
 else
  make install IGNORE_CC_MISMATCH=1
 fi
else
 rm -f /tmp/.X0-lock
 if [ -x /usr/bin/nvidia-installer ]; then
  /usr/bin/nvidia-installer --uninstall -s -N 2>/dev/null
 elif [ -f /usr/lib/libnvidia-tls.so.1 ]; then
  ./nvidia-installer --uninstall -s -N 2>/dev/null
 fi
 if [ -d /usr/lib/xorg/modules ]; then
  #CC=$CC 
  ./nvidia-installer -s --x-module-path=/usr/lib/xorg/modules -N 2>/dev/null
 else
  #CC=$CC
  ./nvidia-installer -s -N 2>/dev/null
 fi
fi
[ $? -eq 0 ] || BAD_INSTALL=1
rm -rf NVIDIA-Linux-x86$ARCH-$VER-pkg$PKG
rm -f /etc/modutils/nvidia
perl -pi -e 's|.*char-major-195.*[\n]?||' /etc/modutils/aliases
perl -pi -e 's|[\n]?$|\n|' /etc/modutils/aliases
echo alias char-major-195 nvidia >> /etc/modutils/aliases
depmod -a

# Remove header hotfix
[ -d /lib/modules/$(uname -r)/build/include/asm-x86 -a -L /lib/modules/$(uname -r)/build/include/asm-i386 ] && rm -f /lib/modules/$(uname -r)/build/include/asm-i386

if ! [ -e /etc/X11/$X_CONF.1st ]; then
 cp /etc/X11/$X_CONF /etc/X11/$X_CONF.1st
fi
perl -p -e 's/^[\s]*Load\s*"GLcore"/#\tLoad  "GLcore"/;
            s/^[\s]*Load\s*"dri"/#\tLoad  "dri"/;
            s/^[\s]*#*[\s]*Load\s*"glx"/\tLoad  "glx"/;
            s/^[\s]*Driver\s*"nv"/\tDriver      "nvidia"/g;
            s/^[\s]*Driver\s*"fbdev"/\tDriver      "nvidia"/g;
            s/^[\s]*Driver\s*"vesa"/\tDriver      "nvidia"/g;
     	    s/DefaultColorDepth.*/DefaultColorDepth 24/;
	    s/DefaultDepth.*/DefaultDepth 24/;
            s/^[\s]*Option\s*"sw_cursor"/\t#Option     "sw_cursor"/;
            s/^.*IgnoreDisplayDevices.*\n?//g;
            s/^.*Coolbits.*\n?//g;
            s/^([\s]*Driver\s*"nvidia")/\1\n\tOption      "IgnoreDisplayDevices"\t"TV"\n\tOption      "Coolbits"\t"1"/g' \
            < /etc/X11/$X_CONF.1st > /etc/X11/$X_CONF
if which Xorg >/dev/null; then
 perl -pi -e 's/^.*RenderAccel.*\n?//g;s/^.*AllowGLXWithComposite.*\n?//g;s/^.*RandRRotation.*\n?//g;
              s/^.*AddARGBGLXVisuals.*\n?//g;s/^.*DisableGLXRootClipping.*\n?//g;s/^.*TripleBuffer.*\n?//g;
	      s/^.*UseEDID.*\n?//g;s/^.*UseEdidFreqs.*\n?//g;s/^.*DynamicTwinView.*\n?//g;s/^.*ModeValidation.*\n?//g;
	      s/^.*UseDisplayDevice.*\n?//g;
              s/^([\s]*Driver\s*"nvidia")/\1\n\tOption      "RenderAccel"\t"1"\n\tOption      "AllowGLXWithComposite"\t"1"\n\tOption      "RandRRotation"\t"1"\n\tOption      "AddARGBGLXVisuals"\t"1"\n\tOption      "DisableGLXRootClipping"\t"1"\n\tOption      "TripleBuffer"\t"1"\n\tOption      "UseEDID"\t"1"\n\tOption      "UseEdidFreqs"\t"1"\n\tOption      "DynamicTwinView"\t"0"\n#\tOption      "ModeValidation"\t"NoDFPNativeResolutionCheck"\n#\tOption      "UseDisplayDevice"\t"DFP"/g' /etc/X11/$X_CONF
 if ! grep -q Extensions /etc/X11/$X_CONF; then
  printf "%b\n" '\nSection "Extensions"' >> /etc/X11/$X_CONF
  printf "%b\n" '\tOption\t"Composite"\t"1"' >> /etc/X11/$X_CONF
  printf "%b\n" '#\tOption\t"RENDER"\t"1"' >> /etc/X11/$X_CONF
  echo 'EndSection' >> /etc/X11/$X_CONF
 fi
 perl -pi -e 's/^[\s]*#*[\s]*Option\s*"Composite".*/\tOption\t"Composite"\t"1"/' /etc/X11/$X_CONF
 # disable composite for cards without dx9 support
 for card in $CARDS; do
  DEVICE=${card#*:}
  case $DEVICE in
   0020|0028|0029|002c|002d|00a0|00ff|0100|0101|0103|0110|0111|0112|0113|0150|0151|0152|0153|0170|0171|0172|0173|0174|0175|0176|0177|0179|017a|017d|0178|017c|0181|0182|0183|0185|0188|018c|018a|018b|01a0|01f0|0200|0201|0202|0203|0250|0251|0253|0258|0259|025b|0280|0281|0282|0286|0288|0289|028c)
    #perl -pi -e 's/^[\s]*#*[\s]*Option\s*"Composite".*/#\tOption\t"Composite"\t"1"/' /etc/X11/$X_CONF
    #perl -pi -e 's/^.*RenderAccel.*\n?//g;s/^.*AllowGLXWithComposite.*\n?//g;s/^.*RandRRotation.*\n?//g' /etc/X11/$X_CONF ;;
    #perl -pi -e 's/^(.*RenderAccel"\s*).*/\1"1"/g;s/^.*AllowGLXWithComposite.*\n?//g;s/^.*RandRRotation.*\n?//g;
    #             s/^.*AddARGBGLXVisuals.*\n?//g;s/^.*DisableGLXRootClipping.*\n?//g;s/^.*TripleBuffer.*\n?//g' /etc/X11/$X_CONF
    perl -pi -e 's/^(.*RenderAccel"\s*).*/\1"0"/g' /etc/X11/$X_CONF
    ;;
  esac
 done
 # disable composite if -c option used
 [ "$COMPOSITE" = "0" ] && perl -pi -e 's/^[\s]*#*[\s]*Option\s*"Composite".*/#\tOption\t"Composite"\t"1"/' /etc/X11/$X_CONF
 # disable UseEDID if -e option used
 [ "$USEEDID" = "0" ] && perl -pi -e 's/^(.*UseEDID"\s*).*/\1"0"/g' /etc/X11/$X_CONF
 # disable UseEdidFreqs if -q option used
 [ "$USEEDIDFREQS" = "0" ] && perl -pi -e 's/^(.*UseEdidFreqs"\s*).*/\1"0"/g' /etc/X11/$X_CONF
 # remove modelines if -r option used
 [ "$RMML" = "1" ] && perl -pi -e 's/^.*Mode.ine.*\n?//g' /etc/X11/$X_CONF
 # enable TV Out if -t option used
 [ "$TV" = "1" ] && perl -pi -e 's/^.*IgnoreDisplayDevices.*\n?//g' /etc/X11/$X_CONF
 # enable DynamicTwinView if -d option used
 [ "$DYN_TWINVIEW" = "1" ] && perl -pi -e 's/^(.*DynamicTwinView"\s*).*/\1"1"/g' /etc/X11/$X_CONF
 # disable DFP Native Resolution Check if -n option used
 [ "$NRC" = "0" ] && perl -pi -e 's/^([\s]*)#*([\s]*Option\s*"ModeValidation".*)/\1\2/g' /etc/X11/$X_CONF
 # force UseDisplayDevice if -g option used
 [ -n "$UDD" ] && perl -pi -e 's/^([\s]*)#*([\s]*Option\s*"UseDisplayDevice").*/\1\2\t"'"$UDD"'"/g' /etc/X11/$X_CONF
 # Xorg 7.1 hack
 #X -version 2>&1|grep -q -e "X Window System Version 7.1" && perl -pi -e 's/^(.*RenderAccel"\s*).*/\1 "0"/' /etc/X11/$X_CONF
 # Use nvidia-auto-select if -a option used
 [ "$AUTO_SELECT" = "1" ] && perl -pi -e's/^(\s*Modes\s+).*/\1"nvidia-auto-select"/' /etc/X11/$X_CONF
fi
rm -f /etc/X11/XvMCConfig
if [ "$XVMC" = "1" -a "$LEGACY" = "0" -a -x /usr/lib/libXvMCNVIDIA_dynamic.so.1 ]; then
 echo libXvMCNVIDIA_dynamic.so.1 > /etc/X11/XvMCConfig
else
 echo libXvMC.so.1 > /etc/X11/XvMCConfig
fi
[ "$HACK" = "1" ] && rm -rf /usr/lib/tls
if [ "$ARCH" = "_64" -a "$OLDEMUL" = "1" ]; then
 mkdir -p /emul/ia32-linux/usr/lib/tls/
 rm -f /emul/ia32-linux/usr/lib/tls/libnvidia-tls.so.*
 rm -f /emul/ia32-linux/usr/lib/libGLcore.so.*
 rm -f /emul/ia32-linux/usr/lib/libGL.so.*
 rm -f /emul/ia32-linux/usr/lib/libnvidia-tls.so.*
 cp -v /usr/src/NVIDIA-Linux-x86-$VER-pkg$PKG/usr/lib/tls/libnvidia-tls.so.$DVER /emul/ia32-linux/usr/lib/tls/
 cp -v /usr/src/NVIDIA-Linux-x86-$VER-pkg$PKG/usr/lib/libGLcore.so.$DVER /emul/ia32-linux/usr/lib/
 cp -v /usr/src/NVIDIA-Linux-x86-$VER-pkg$PKG/usr/lib/libGL.so.$DVER /emul/ia32-linux/usr/lib/
 cp -v /usr/src/NVIDIA-Linux-x86-$VER-pkg$PKG/usr/lib/tls/libnvidia-tls.so.$DVER /emul/ia32-linux/usr/lib/
 ln -sf tls/libnvidia-tls.so.$DVER /emul/ia32-linux/usr/lib/libnvidia-tls.so.1
 ln -sf libGLcore.so.$DVER /emul/ia32-linux/usr/lib/libGLcore.so
 ln -sf libGL.so.$DVER /emul/ia32-linux/usr/lib/libGL.so.1
 ln -sf libGL.so.1 /emul/ia32-linux/usr/lib/libGL.so
 rm -rf /usr/src/NVIDIA-Linux-x86-$VER-pkg$PKG
fi
mkdir -p /etc/udev/rules.d
rm -f /etc/udev/nvidia.rules /etc/udev/rules.d/nvidia.rules
if [ "$UDEVHACK" = "1" ]; then
 cat <<EOF > /etc/udev/nvidia.rules
 KERNEL=="nvidia", PROGRAM="/bin/sh -c '/bin/mknod /dev/nvidia0 -m 666 c 195 0 2>/dev/null;/bin/mknod /dev/nvidiactl -m 666 c 195 255 2>/dev/null'"
EOF
 ln -s ../nvidia.rules /etc/udev/rules.d/nvidia.rules
fi
ldconfig
sync

# X Server 1.4 workaround
case $VER in 
 100.14.11|100.14.09|100.14.06|100.14.03|1.0-9755|1.0-9746|1.0-9742|1.0-9639|1.0-9631|1.0-9629|1.0-9626|1.0-9625|1.0-8776|1.0-8774|1.0-8762|1.0-8756|1.0-8178|1.0-8174|1.0-7676|1.0-7667|1.0-7664) 
 if X -version 2>&1|grep -q -e "X Server 1.4"; then
  DDM=$(cat /etc/X11/default-display-manager 2>/dev/null|sed 's|.*/||')
  if [ -z "$DDM" ]; then
   for DDM in kdm gdm xdm; do
    [ -x /etc/init.d/$DDM ] && break
   done
  fi
  case $DDM in
   gdm)
    if ! grep -q [[]server-Standard[]] /etc/gdm/gdm.conf; then
     echo Warning: Using X Server 1.4 GDM workaround ! >&2
     echo [server-Standard] >> /etc/gdm/gdm.conf
     echo command=/usr/bin/X -audit 0 -ignoreABI >> /etc/gdm/gdm.conf
    fi
    ;;
   kdm)
    for k in 3 4; do
     if [ -f /etc/kde$k/kdm/kdmrc ] && ! grep -qe -ignoreABI /etc/kde$k/kdm/kdmrc; then
      echo "Warning: Using X Server 1.4 KDM (KDE$k) workaround !" >&2
      sed -i 's|^\(\s*ServerCmd=.*\)|\1 -ignoreABI|' /etc/kde$k/kdm/kdmrc
     fi
    done
    ;;
   xdm)
    if ! grep -qe -ignoreABI /etc/X11/xdm/Xservers; then
     echo Warning: Using X Server 1.4 XDM workaround ! >&2
     sed -i 's|^\(\s*:.*\)|\1 -ignoreABI|' /etc/X11/xdm/Xservers
    fi
    ;;
  esac
 fi
 ;;
esac

if [ "$BAD_INSTALL" = "1" ]; then
 cp /etc/X11/$X_CONF.1st /etc/X11/$X_CONF
 echo Error: Something went wrong, using old $X_CONF file! >&2
fi

if ! [ "$NO_X_KILL" = "1" ]; then
 if [ "$INSTALLED" = "yes" ]; then
  if [ -z "$DM" ]; then
   if [ -x /etc/init.d/kdm ]; then
    /etc/init.d/kdm start
   elif [ -x /etc/init.d/gdm ]; then
    /etc/init.d/gdm start
   elif [ -x /etc/init.d/xdm ]; then
    /etc/init.d/xdm start
   fi
  else
   [ -x /etc/init.d/$DM ] && /etc/init.d/$DM start
  fi
 else
  if [ -x /etc/init.d/xsession ]; then
   /etc/init.d/xsession restart
  else
   nohup sh -c "/etc/init.d/kdm stop; sleep 5; rmmod nvidia 2>/dev/null; /etc/init.d/kdm start"
  fi
 fi
fi
