#!/bin/sh
if [ $(id -u) != 0 ]; then
 echo "Error: You must be root to run this script!" >&2
 exit 1
fi

apt-get update
apt-get --yes install git-core
apt-get --yes build-dep xserver-xorg-video-nouveau
cd /tmp
rm -rf xf86-video-nouveau
git clone --depth 1 git://anongit.freedesktop.org/nouveau/xf86-video-nouveau
cd xf86-video-nouveau
patch -p1 <<'EOT'
diff --git a/configure.ac b/configure.ac
index 6a143dc..c905830 100644
--- a/configure.ac
+++ b/configure.ac
@@ -71,7 +71,7 @@ PKG_CHECK_MODULES(LIBDRM_NOUVEAU, libdrm_nouveau)
 AC_SUBST(LIBDRM_NOUVEAU_CFLAGS)
 AC_SUBST(LIBDRM_NOUVEAU_LIBS)
 
-PKG_CHECK_MODULES(XORG, [xorg-server >= 1.8] xproto fontsproto libdrm xf86driproto $REQUIRED_MODULES)
+PKG_CHECK_MODULES(XORG, [xorg-server >= 1.7] xproto fontsproto libdrm xf86driproto $REQUIRED_MODULES)
 PKG_CHECK_MODULES(XEXT, [xextproto >= 7.0.99.1],
 		  HAVE_XEXTPROTO_71="yes"; AC_DEFINE(HAVE_XEXTPROTO_71, 1, [xextproto 7.1 available]),
 		  HAVE_XEXTPROTO_71="no")
diff --git a/src/nouveau_dri2.c b/src/nouveau_dri2.c
index 1a68ed3..3d69de7 100644
--- a/src/nouveau_dri2.c
+++ b/src/nouveau_dri2.c
@@ -16,6 +16,11 @@ struct nouveau_dri2_buffer {
 	PixmapPtr ppix;
 };
 
+#if XORG_VERSION_CURRENT <= XORG_VERSION_NUMERIC(1,7,99,3,0)
+typedef void (*DRI2SwapEventPtr)(ClientPtr client, void *data, int type,
+                                 CARD64 ust, CARD64 msc, CARD64 sbc);
+#endif
+
 static inline struct nouveau_dri2_buffer *
 nouveau_dri2_buffer(DRI2BufferPtr buf)
 {
@@ -442,9 +447,11 @@ nouveau_dri2_init(ScreenPtr pScreen)
 	dri2.CreateBuffer = nouveau_dri2_create_buffer;
 	dri2.DestroyBuffer = nouveau_dri2_destroy_buffer;
 	dri2.CopyRegion = nouveau_dri2_copy_region;
+#if XORG_VERSION_CURRENT > XORG_VERSION_NUMERIC(1,7,99,3,0)
 	dri2.ScheduleSwap = nouveau_dri2_schedule_swap;
 	dri2.ScheduleWaitMSC = nouveau_dri2_schedule_wait;
 	dri2.GetMSC = nouveau_dri2_get_msc;
+#endif
 
 	return DRI2ScreenInit(pScreen, &dri2);
 }
EOT
if [ "$(uname -m)" = "x86_64" ]; then
 PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig ./autogen.sh --prefix=/usr
 PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig make -j`grep -c ^processor /proc/cpuinfo 2>/dev/null||echo 1`
else
 ./autogen.sh --prefix=/usr
 make -j`grep -c ^processor /proc/cpuinfo 2>/dev/null||echo 1`
fi
make install
rm -f /etc/X11/xorg.conf*
dpkg-reconfigure -phigh xserver-xorg
screen /etc/init.d/kdm restart
