#!/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
    00fa|00fb|00fc|00fd|00fe|0301|0302|0308|0309|0311|0312|0314|031a|031b|031c|0320|0321|0322|0323|0324|0325|0326|0327|0328|032a|032b|032c|032d|0330|0331|0332|0333|0334|0338|033f|0341|0342|0343|0344|0347|0348|034c|034e) LEGACY=3;UNSUPPORTED=0 ;;
    *) UNSUPPORTED=0 ;;
   esac
   ;;
 esac
done

X_VER=$(X -version 2>&1 | grep -E '(X Window System Version|X.Org X Server) [0-9]\.' | sed -e 's/^.*X Window System Version //;s/^.*X.Org X Server //' | cut -d' ' -f1)

case $LEGACY in
 1) case $X_VER in
     1.[7-9]*|1.1[0-9]*) UNSUPPORTED=1 ;;
    esac ;;
esac

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=""
USE_DKMS=1

while getopts :acdefg:mnprtlqxv: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"
     ;;
  m) USE_DKMS=0
     ;;
  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 " -m      Disable DKMS for module build" >&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

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

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

# GeForce 5 series
VER_LEGACY_3=173.14.31

# OpenGL 4.2 (developer)
#VER_SPECIAL=280.10.01.02

# OpenGL 4.1 (developer)
#VER_SPECIAL=256.38.03

# OpenGL 3.3 and 4.0 (developer)
#VER_SPECIAL=195.36.07.04

# Current beta
VER_SPECIAL=295.09

# Current/Last stable
#VER_SPECIAL=290.10

# Latest long-lived
#VER_SPECIAL=275.43

#VER=295.09
VER=290.10
#VER=290.06
#VER=290.03
#VER=285.05.09
#VER=285.03
#VER=280.13
#VER=280.11
#VER=280.04
#VER=275.43
#VER=275.36
#VER=275.28
#VER=275.21
#VER=275.19
#VER=275.09.07
#VER=275.09.04
#VER=275.09
#VER=270.41.19
#VER=270.41.06
#VER=270.41.03
#VER=270.30
#VER=270.29
#VER=270.26
#VER=270.18
#VER=260.19.44
#VER=260.19.36
#VER=260.19.29
#VER=260.19.26
#VER=260.19.21
#VER=260.19.12
#VER=260.19.06
#VER=260.19.04
#VER=256.53
#VER=256.52
#VER=256.44
#VER=256.38.03
#VER=256.38.02
#VER=256.35
#VER=256.29
#VER=256.25
#VER=195.36.31
#VER=195.36.24
#VER=195.36.15
#VER=195.36.07.04
#VER=195.36.07.03
#VER=195.36.08
#VER=195.36.03
#VER=195.30
#VER=195.22
#VER=190.53
#VER=190.42
#VER=190.40
#VER=190.36
#VER=190.32
#VER=190.25
#VER=190.18.03
#VER=190.18.05
#VER=190.18
#VER=190.16
#VER=185.19
#VER=185.18.36
#VER=185.18.31
#VER=185.18.29
#VER=185.18.14
#VER=185.18.10
#VER=185.18.08
#VER=185.18.04
#VER=185.13
#VER=180.60
#VER=180.53
#VER=180.51
#VER=180.44
#VER=180.41
#VER=180.37.05
#VER=180.37.04
#VER=180.37
#VER=180.35
#VER=180.29
#VER=180.27
#VER=180.25
#VER=180.22
#VER=180.18
#VER=180.17
#VER=180.16
#VER=180.11.02
#VER=180.11
#VER=180.08
#VER=180.06
#VER=177.82
#VER=177.80
#VER=177.78
#VER=177.76
#VER=177.70
#VER=177.68
#VER=177.67
#VER=177.61.02
#VER=177.13
#VER=173.14.31
#VER=173.14.30
#VER=173.14.28
#VER=173.14.27
#VER=173.14.25
#VER=173.14.22
#VER=173.14.20
#VER=173.14.19
#VER=173.14.18
#VER=173.14.17
#VER=173.14.16
#VER=173.14.15
#VER=173.14.12
#VER=173.14.09
#VER=173.14.05
#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.20
#VER=96.43.19
#VER=96.43.18
#VER=96.43.17
#VER=96.43.16
#VER=96.43.14
#VER=96.43.13
#VER=96.43.12
#VER=96.43.11
#VER=96.43.10
#VER=96.43.09
#VER=96.43.07
#VER=96.43.05
#VER=96.43.01
#VER=71.86.15
#VER=71.86.14
#VER=71.86.13
#VER=71.86.11
#VER=71.86.10
#VER=71.86.09
#VER=71.86.08
#VER=71.86.07
#VER=71.86.06
#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|96.43.07|96.43.09|96.43.*)
  [ "$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|173.14.05|173.14.09|173.14.12|173.14.15|173.14.*|177.13|177.61.02|177.67|177.68|177.70|177.76|177.78|177.80|177.82|180.06|180.08|180.11|180.11.02|180.16|180.17|180.*|185.*|19[0-9].*|25[6-9].*|26[0-9].*|27[0-9].*|28[0-9].*|29[0-9].*)
  if [ "$LEGACY" = "1" ]; then
   VER=$VER_LEGACY_1
  elif [ "$LEGACY" = "2" ]; then
   VER=$VER_LEGACY_2
  elif [ "$LEGACY" = "3" ]; then
   VER=$VER_LEGACY_3
  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}" = "1" ]; then
  if [ "$VER_FORCE" = "1" ]; then
   VER=$VER_LEGACY_1
  elif [ "$VER_FORCE" = "2" ]; then
   VER=$VER_LEGACY_2
  elif [ "$VER_FORCE" = "3" ]; then
   VER=$VER_LEGACY_3
  elif [ "$VER_FORCE" = "s" ]; then
   VER=$VER_SPECIAL
  fi
 elif [ "$VER_FORCE" = "l" -o "$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

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

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

if [ "$UNSUPPORTED" = "1" ]; then
 echo Error: No supported nVidia VGA adapter found. >&2
 exit 3
fi

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 [ "$(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

X_CONF_CREATE=0
if ! [ -e /etc/X11/$X_CONF ]; then
 X_CONF=xorg.conf
 X_CONF_CREATE=1
fi

unset UP

# create minimalistic xorg.conf when without Driver entry

if [ "$X_CONF_CREATE" = "0" ]; then
 if ! grep -v ^# /etc/X11/$X_CONF|grep Driver|grep -q -e nv -e fbdev -e vesa; then
  echo Warning: /etc/X11/$X_CONF needs some modifications. Trying now. >&2
  if ! [ -x /usr/bin/patch ]; then
   apt-get update && UP=1
   apt-get install --allow-unauthenticated --yes patch
  fi
  if ! [ -x /usr/bin/patch ]; then
   echo Error: No patch tool installed - please install it and try again! >&2
   exit 7
  fi
  rm -f /etc/X11/$X_CONF.pure /etc/X11/$X_CONF.rej
  if ! grep -q "Device.*Configured Video Device" /etc/X11/$X_CONF; then
   echo 'Warning: Adding "Configured Video Device" to "Screen" Section.' >&2
   perl -pi -e 's/(^\tMonitor\t\t"Configured Monitor"\n)/\1\tDevice\t\t"Configured Video Device"\n/' /etc/X11/$X_CONF
  fi
  cp /etc/X11/$X_CONF /etc/X11/$X_CONF.pure
  patch --no-backup-if-mismatch /etc/X11/$X_CONF <<'EOT'
Index: xorg.conf
@@ -31,6 +31,7 @@
 
 Section "Device"
 	Identifier	"Configured Video Device"
+	Driver		"vesa"
 EndSection
 
 Section "Monitor"
@@ -41,6 +42,11 @@
 	Identifier	"Default Screen"
 	Monitor		"Configured Monitor"
 	Device		"Configured Video Device"
+	DefaultDepth	24
+	SubSection	"Display"
+		Depth	24
+		Modes
+	EndSubSection
 EndSection
 
 Section "ServerLayout"
EOT
  if [ $? -ne 0 ]; then
   echo Error: Unable to patch. Please manually fix file! >&2
   rm -f /etc/X11/$X_CONF
   mv /etc/X11/$X_CONF.pure /etc/X11/$X_CONF
   exit 8
  else
   echo Warning: Patch applied successfully. >&2
  fi
 fi
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 ;;
 71.86.07|71.86.08|71.86.09|71.86.10|71.86.11|71.86.13|71.86.15|96.43.09|96.43.10|96.43.11|96.43.12|96.43.13|96.43.14|96.43.16|96.43.17|96.43.18|96.43.19|96.43.20|173.14.15|173.14.16|173.14.17|173.14.19|173.14.20|173.14.22|173.14.25|173.14.27|173.14.28|173.14.30|173.14.31|177.61.02|177.68|177.70|177.76|177.78|180.08|180.11|180.11.02|180.16|180.18|180.25|180.27|180.35|180.37|180.41|180.51|180.53|180.60|185.13|185.18.04|185.18.10|185.18.14|185.19|190.18.05|190.25|190.32|190.36|190.40|190.42|190.53|195.36.03|195.36.08|195.36.15|195.36.24|256.25|256.29|256.35|256.44|256.52|260.19.04|260.19.06|260.19.21|260.19.26|270.26|270.29|270.30|270.41.03|275.09.04|280.04|280.11|285.05.09|290.03|290.06|290.10|295.09) SERVER=download.nvidia.com ;;
esac
URL=http://$SERVER/XFree86/Linux-x86$ARCH/$VER/
EURL=http://$SERVER/XFree86/Linux-x86/$VER/
PKG=0
case $VER in
 165.33.09|180.37.04|180.37.05|190.18.03|195.36.07.03|195.36.07.04) PKG=1 ;;
esac
[ "$ARCH" = "_64" -a "$EMUL" = "1" ] && PKG=2
#[ ${VER/.*} -ge 256 ] && PKG=""
[ "$(echo $VER|sed 's/\..*//')" -ge 256 ] && PKG=""
[ "$VER" = "180.37.04" -o "$VER" = "180.37.05" ] && URL=http://developer.download.nvidia.com/opengl/3.1/linux/
[ "$VER" = "190.18.03" ] && URL=http://developer.download.nvidia.com/opengl/3.2/linux/
[ "$VER" = "195.36.07.03" ] && URL=http://developer.download.nvidia.com/opengl/3.3/linux/
[ "$VER" = "195.36.07.04" ] && URL=http://developer.download.nvidia.com/opengl/4.0/linux/
[ "$VER" = "256.38.02" -o "$VER" = "256.38.03" ] && URL=http://developer.download.nvidia.com/opengl/4.1/linux/
[ "$VER" = "280.10.01.02" ] && URL=http://developer.download.nvidia.com/opengl/4.2/linux/
PATCH=0
HACK=0
UDEVHACK=0
[ "$VER" = "1.0-7174" ] && UDEVHACK=1
KVER=$(uname -r)
KV=$(uname -r|cut -d. -f1-2)
FULLPKG=""
[ "$PKG" ] && FULLPKG="-pkg$PKG"
cd /usr/src
[ -f NVIDIA-Linux-x86$ARCH-$VER$FULLPKG.run ] || wget -Nc "$URL"NVIDIA-Linux-x86$ARCH-$VER$FULLPKG.run
rm -rf NVIDIA-Linux-x86$ARCH-$VER$FULLPKG
bash NVIDIA-Linux-x86$ARCH-$VER$FULLPKG.run --extract-only >/dev/null 2>&1 || (
 rm -rf NVIDIA-Linux-x86$ARCH-$VER$FULLPKG
 rm -f NVIDIA-Linux-x86$ARCH-$VER$FULLPKG.run
 echo NVIDIA-Linux-x86$ARCH-$VER$FULLPKG.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$FULLPKG.run ] || wget -Nc "$EURL"NVIDIA-Linux-x86-$VER$FULLPKG.run
 rm -rf NVIDIA-Linux-x86-$VER$FULLPKG
 bash NVIDIA-Linux-x86-$VER$FULLPKG.run --extract-only >/dev/null 2>&1 || (
  rm -f NVIDIA-Linux-x86-$VER$FULLPKG.run
  echo NVIDIA-Linux-x86-$VER$FULLPKG.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

DKMS=0

if [ "$USE_DKMS" = "1" ]; then

 if ! [ -x /usr/sbin/dkms ]; then
  [ -z "$UP" ] && apt-get update && UP=1
  FROZEN=0
  [ -f /etc/frozen-rc.d ] && FROZEN=1
  [ -x /usr/sbin/unfreeze-rc.d ] && /usr/sbin/unfreeze-rc.d
  apt-get install --allow-unauthenticated --yes dkms
  [ "$FROZEN" = "1" -a -x /usr/sbin/freeze-rc.d ] && /usr/sbin/freeze-rc.d
 fi

 if ! [ -x /usr/sbin/dkms ]; then
  m-a -i prepare
  FROZEN=0
  [ -f /etc/frozen-rc.d ] && FROZEN=1
  [ -x /usr/sbin/unfreeze-rc.d ] && /usr/sbin/unfreeze-rc.d
  #DKMS_DEB=dkms_2.0.20.4-0ubuntu1_all.deb
  #wget -N http://linux.dell.com/dkms/$DKMS_DEB
  DKMS_DEB=dkms_2.0.20.4-0ubuntu3_all.deb
  wget -N http://kanotix.com/files/thorhammer/backports-ubuntu/dkms/$DKMS_DEB
  dpkg -i $DKMS_DEB
  [ "$FROZEN" = "1" -a -x /usr/sbin/freeze-rc.d ] && /usr/sbin/freeze-rc.d
 fi

 if [ -x /usr/sbin/dkms -a ! -f /usr/share/cdbs/1/rules/debhelper.mk ]; then
  apt-get update && UP=1
  apt-get install --allow-unauthenticated --yes cdbs
 fi

 [ -x /usr/sbin/dkms -a -f /usr/share/cdbs/1/rules/debhelper.mk ] && DKMS=1

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)
  [ "$DM" = "kdm" -a -x /etc/init.d/kdm-kde4 ] && DM=kdm-kde4
  [ "$DM" = "kdm" -a -x /etc/init.d/kdm-trinity ] && DM=kdm-trinity
  if [ -z "$DM" ]; then
   if [ -x /etc/init.d/kdm ]; then
    /etc/init.d/kdm stop
    killall kdm >/dev/null 2>&1
   elif [ -x /etc/init.d/gdm ]; then
    /etc/init.d/gdm stop
    killall gdm >/dev/null 2>&1
   elif [ -x /etc/init.d/xdm ]; then
    /etc/init.d/xdm stop
    killall xdm >/dev/null 2>&1
   fi
  else
   if [ -x /etc/init.d/$DM ]; then
    /etc/init.d/$DM stop
    killall $DM >/dev/null 2>&1
   fi
  fi
  killall -9 Xgl >/dev/null 2>&1
  killall -9 Xorg >/dev/null 2>&1
 fi
fi
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
# nouveau kms workaround
#if /sbin/modinfo nouveau >/dev/null 2>&1; then
 grep -q "blacklist nouveau" /etc/modprobe.d/nvidia-kernel-common.conf 2>/dev/null||echo blacklist nouveau > /etc/modprobe.d/nvidia-kernel-common.conf
 update-initramfs -ut
#fi
# this hack would allow to unload nouveau but kills the vt
if [ "$STY" ]; then
 if ! [ "$NO_X_KILL" = "1" ]; then
  DM=$(cat /etc/X11/default-display-manager 2>/dev/null)
  DM=$(basename $DM 2>/dev/null)
  [ "$DM" = "kdm" -a -x /etc/init.d/kdm-kde4 ] && DM=kdm-kde4
  [ "$DM" = "kdm" -a -x /etc/init.d/kdm-trinity ] && DM=kdm-trinity
  if [ -z "$DM" ]; then
   if [ -x /etc/init.d/kdm ]; then
    /etc/init.d/kdm stop
    killall kdm >/dev/null 2>&1
   elif [ -x /etc/init.d/gdm ]; then
    /etc/init.d/gdm stop
    killall gdm >/dev/null 2>&1
   elif [ -x /etc/init.d/xdm ]; then
    /etc/init.d/xdm stop
    killall xdm >/dev/null 2>&1
   fi
  else
   if [ -x /etc/init.d/$DM ]; then
    /etc/init.d/$DM stop
    killall $DM >/dev/null 2>&1
   fi
  fi
  killall -9 Xgl >/dev/null 2>&1
  killall -9 Xorg >/dev/null 2>&1
 fi
 lsmod|grep -q nouveau && for x in $(ls /sys/class/vtconsole/*/bind 2>/dev/null); do echo 0 > $x; done
 INSTALLED="yes"
fi
rmmod nouveau ttm drm_kms_helper drm >/dev/null 2>&1
rmmod nvidia >/dev/null 2>&1

# 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
  unset UP
 fi
fi
# Ubuntu intrepid multiverse workaround
#if [ "$DISTRIB_ID" = "Ubuntu" -a "$DISTRIB_CODENAME" = "intrepid" ]; then
# if ! grep -Eq "^[[:space:]]*deb[[:space:]]+.*multiverse$" /etc/apt/sources.list; then
#  grep -Eq "deb[[:space:]]+.*multiverse$" /etc/apt/sources.list && sed -i 's/^#\s*\(deb\s.*multiverse\)$/\1/' /etc/apt/sources.list
#  unset UP
# 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$FULLPKG
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|2.6.26')" != "" -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|2.6.26')" != "" -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')" != "" -a "$VER" = "71.86.04" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/71.86.04/"
 PATCHES="NVIDIA_kernel-71.86.04-2.6.26.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 -p1 </usr/src/$p || rm -f /usr/src/$p
 done
fi

if [ "$(uname -r|grep -E '2.6.26')" != "" -a "$VER" = "96.43.05" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/96.43.05/"
 PATCHES="NVIDIA_kernel-96.43.05-2.6.26.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')" != "" -a "$VER" = "173.08" ]; 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 [ "$(uname -r|grep -E '2.6.26')" != "" -a "$VER" = "173.14.05" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/173.14.05/"
 PATCHES="NVIDIA_kernel-173.14.05-2419292.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.27')" != "" ] && [ "$VER" = "71.86.06" -o "$VER" = "96.43.07" -o "$VER" = "173.14.12" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/173.14.12/"
 PATCHES="NVIDIA_173.14.12_2.6.27.patch NVIDIA-no-xen.patch"
 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.27')" != "" -a "$VER" = "177.13" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/177.13/"
 PATCHES="NVIDIA_kernel-177.13-2627763.diff NVIDIA-no-xen.patch"
 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.28')" != "" -a "$VER" = "71.86.07" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/71.86.07/"
 PATCHES="NVIDIA_kernel-71.86.07-2.6.28.patch"
  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.28')" != "" ] && [ "$VER" = "177.80" -o "$VER" = "177.61.02" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/177.80/"
 PATCHES="NVIDIA_kernel-177.80-2990799.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.28')" != "" -a "$VER" = "177.82" ]; then
 PATCH_BASE_URL="http://kanotix.com/files/177.82/"
 PATCHES="NVIDIA_kernel-177.82.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

case $VER in 71.86.09|96.43.11|173.14.18|180.37.05)
 PATCH_BASE_URL="http://kanotix.com/files/96.43.11/"
 PATCHES="NVIDIA_kernel-96.43.11-2.6.30.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 -p1 </usr/src/$p || rm -f /usr/src/$p
 done
 ;;
esac

case $VER in 180.37.05|185.18.14)
 PATCH_BASE_URL="http://kanotix.com/files/185.18.14/"
 PATCHES="NVIDIA_kernel-185.18.14-2.6.31.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 -p1 </usr/src/$p || rm -f /usr/src/$p
 done
 ;;
esac

uname -r|grep -q 2.6.33 && case $VER in 190.53)
 PATCH_BASE_URL="http://kanotix.com/files/190.53/"
 PATCHES="nvidia-190.53-2.6.33.patch"
 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
 ;;
esac

uname -r|grep -q 2.6.33 && case $VER in 195.22|195.30)
 PATCH_BASE_URL="http://kanotix.com/files/195.30/"
 PATCHES="nvidia-195.30-2.6.33.patch"
 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
 ;;
esac

case $VER in 96.43.18)
 PATCH_BASE_URL="http://kanotix.com/files/96.43.18/"
 PATCHES="NVIDIA_96.43.18_2.6.36.patch"
 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 -p1 </usr/src/$p || rm -f /usr/src/$p
 done
 ;;
esac

case $VER in 256.53)
 PATCH_BASE_URL="http://kanotix.com/files/256.53/"
 PATCHES="NVIDIA_256.53_2.6.36.patch"
 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
 ;;
esac

if grep -q CONFIG_XEN=y /boot/config-$(uname -r) && [ $(echo $VER|sed 's/\..*//') -ge 165 ] && [ $(echo $VER|sed 's/\..*//') -le 177 -a ${VER##*.} -le 70 ] && [ "$DKMS" != "1" ]; 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 [ -d usr/src/nv ]; then
 # 2.6.39rc1 hotfix
 grep -qs smp_lock.h usr/src/nv/nv-linux.h && perl -pi -e 's/.*smp_lock.h.*\n//' usr/src/nv/nv-linux.h
 # 3.0rc2 hotfix
 grep -qs O2 usr/src/nv/conftest.sh || sed -i 's/\(BASE_CFLAGS="\)\(-D__KERNEL__\)/\1-O2 \2/' usr/src/nv/conftest.sh
 grep -qs 'KERNEL_VERSION(3, 0, 0)' usr/src/nv/nv-linux.h || sed -i 's/KERNEL_VERSION(2, 7, 0)/KERNEL_VERSION(4, 0, 0)/' usr/src/nv/nv-linux.h
 # 3.3rc1 hotfix
 grep -q -- '-I$SOURCES/arch/x86/include/generated' usr/src/nv/conftest.sh || sed -i 's|\(CFLAGS="$CFLAGS -I$SOURCES/arch/x86/include\).*"|\1 -I$SOURCES/arch/x86/include/generated"|' usr/src/nv/conftest.sh
elif [ -d kernel ]; then
 # 2.6.39rc1 hotfix
 grep -qs smp_lock.h kernel/nv-linux.h && perl -pi -e 's/.*smp_lock.h.*\n//' kernel/nv-linux.h
 # 3.0rc2 hotfix
 grep -qs O2 kernel/conftest.sh || sed -i 's/\(BASE_CFLAGS="\)\(-D__KERNEL__\)/\1-O2 \2/' kernel/conftest.sh
 grep -qs 'KERNEL_VERSION(3, 0, 0)' kernel/nv-linux.h || sed -i 's/KERNEL_VERSION(2, 7, 0)/KERNEL_VERSION(4, 0, 0)/' kernel/nv-linux.h
 # 3.3rc1 hotfix
 grep -q -- '-I$SOURCES/arch/x86/include/generated' kernel/conftest.sh || sed -i 's|\(CFLAGS="$CFLAGS -I$SOURCES/arch/x86/include\).*"|\1 -I$SOURCES/arch/x86/include/generated"|' kernel/conftest.sh
fi

if [ "$FW" = "1" ]; then
 perl -pi -e 's/(NVreg_EnableAGPFW =) .*/\1 1;/' usr/src/nv/os-registry.c
 perl -pi -e 's/(NV_DEFINE_REG_ENTRY\(__NV_ENABLE_AGPFW,).*/\1 1\);/' usr/src/nv/nv-reg.h
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 [ "$DKMS" = "1" ]; then
  if [ -d /usr/lib/xorg/modules ]; then
   ./nvidia-installer --no-kernel-module -s --x-module-path=/usr/lib/xorg/modules -N 2>/dev/null || BAD_INSTALL=1
  else
   ./nvidia-installer --no-kernel-module -s -N 2>/dev/null || BAD_INSTALL=1
  fi
  dkms remove $(dkms status 2>/dev/null|awk '/^nvidia/{print "-m "$1" -v "$2}'|sed 's/,//g;s/:$//'|uniq) --all >/dev/null 2>&1
  rm -rf /var/lib/dkms/nvidia /usr/src/nvidia-$VER
  rm -f /usr/src/nvidia-$VER.dkms.tar.gz
  find /lib/modules -name nvidia.ko -exec rm -f {} \;
  [ -d usr/src/nv ] && mv usr/src/nv /usr/src/nvidia-$VER
  [ -d kernel ] && mv kernel /usr/src/nvidia-$VER
[ "$BAD_INSTALL" = "0" ] && cat > /usr/src/nvidia-$VER/dkms.conf <<EOT
PACKAGE_NAME="nvidia"
PACKAGE_VERSION="$VER"
BUILT_MODULE_NAME[0]="nvidia"
DEST_MODULE_LOCATION[0]="/kernel/drivers/char/drm"
DEST_MODULE_NAME[0]="nvidia"
MAKE[0]="make SYSSRC=\${kernel_source_dir} module IGNORE_XEN_PRESENCE=1"
CLEAN="make -f Makefile.kbuild clean"
AUTOINSTALL="yes"
EOT
  dkms add -m nvidia -v $VER
  LANG= dkms mkdeb -m nvidia -v $VER --source-only
  rm -f /usr/src/nvidia-$VER.dkms.tar.gz 
  dpkg -i /var/lib/dkms/nvidia/$VER/deb/nvidia-dkms_${VER}_all.deb
  modinfo nvidia >/dev/null || BAD_INSTALL=1
 else
  mount --bind /bin/true /sbin/rmmod
  mount --bind /bin/true /sbin/insmod
  if grep -q nouveau nvidia-installer; then
   if [ -d /usr/lib/xorg/modules ]; then
    ./nvidia-installer --no-cc-version-check -z -s --x-module-path=/usr/lib/xorg/modules -N 2>/dev/null || BAD_INSTALL=1
   else
    ./nvidia-installer --no-cc-version-check -z -s -N 2>/dev/null || BAD_INSTALL=1
   fi
  else
   if [ -d /usr/lib/xorg/modules ]; then
    ./nvidia-installer --no-cc-version-check -s --x-module-path=/usr/lib/xorg/modules -N 2>/dev/null || BAD_INSTALL=1
   else
    ./nvidia-installer --no-cc-version-check -s -N 2>/dev/null || BAD_INSTALL=1
   fi 
  fi
  umount /sbin/rmmod /sbin/insmod
 fi
fi

cd /usr/src
rm -rf NVIDIA-Linux-x86$ARCH-$VER$FULLPKG

if [ -f /etc/modutils/aliases ]; then
 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
fi

# 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

[ "$BAD_INSTALL" = "0"  -a "$X_CONF_CREATE" = "1" ] && nvidia-xconfig

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 [ -f /etc/X11/$X_CONF ]; then
  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
 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"0"/' /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$FULLPKG/usr/lib/tls/libnvidia-tls.so.$DVER /emul/ia32-linux/usr/lib/tls/
 cp -v /usr/src/NVIDIA-Linux-x86-$VER$FULLPKG/usr/lib/libGLcore.so.$DVER /emul/ia32-linux/usr/lib/
 cp -v /usr/src/NVIDIA-Linux-x86-$VER$FULLPKG/usr/lib/libGL.so.$DVER /emul/ia32-linux/usr/lib/
 cp -v /usr/src/NVIDIA-Linux-x86-$VER$FULLPKG/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$FULLPKG
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|180.22|180.11.02|173.14.15|96.43.09) 
 if X -version 2>&1|grep -q -e "X Server 1.4" -e "X Server 1.5.99" -e "X Server 1.6"; 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
 if [ "$DKMS" = "1" ]; then
  dpkg --purge nvidia-dkms 2>/dev/null
  dkms remove $(dkms status 2>/dev/null|awk '/^nvidia/{print "-m "$1" -v "$2}'|sed 's/,//g;s/:$//'|uniq) --all >/dev/null 2>&1
  rm -rf /var/lib/dkms/nvidia
 fi 
 if [ -f /etc/X11/$X_CONF.1st ]; then
  cp /etc/X11/$X_CONF.1st /etc/X11/$X_CONF
  echo Error: Something went wrong, using old $X_CONF file! >&2
 else
  echo Error: Something went wrong, reverting changes! >&2
 fi
fi

if ! [ "$NO_X_KILL" = "1" ]; then
 if [ "$INSTALLED" = "yes" ]; then
  killall -9 Xorg >/dev/null 2>&1
  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
   nohup sh -c "/etc/init.d/xsession stop; sleep 5; rmmod nouveau ttm drm_kms_helper drm nvidia 2>/dev/null; /etc/init.d/xsession start"
  else
   nohup sh -c "/etc/init.d/kdm stop; sleep 5; rmmod nouveau ttm drm_kms_helper drm nvidia 2>/dev/null; /etc/init.d/kdm start"
  fi
 fi
fi
