#!/bin/sh

if [ `whoami` == "root" ]; then
	echo "Never work as root, password will be asked when necessary"
	exit
fi;

echo "CVS login (password is ENTER)"
cvs -d:pserver:anonymous@cvs.dri.sourceforge.net:/cvsroot/dri login
if [ "$?" != "0" ]; then echo "CVS login failed, exiting"; exit; fi;

echo CVS checkout
cvs -z3 -d:pserver:anonymous@cvs.dri.sourceforge.net:/cvsroot/dri \
		co -r mach64-0-0-6-branch xc
if [ "$?" != "0" ]; then echo "CVS checkout failed, exiting"; exit; fi;

cd xc/xc
cd programs/Xserver/hw/xfree86/drivers/ati

echo Getting Leif Delglass\' patch
wget http://www.retinalburn.net/linux/dri-xv/mach64-dri-xv-20030331.diff.gz
gzip -d mach64-dri-xv-20030331.diff.gz
patch -p1 < mach64-dri-xv-20030331.diff 
if [ "$?" != "0" ]; then echo "patch failed, exiting"; exit; fi;

# useless now with XFree 4.3 
#echo Getting my compilation fixing patch
#wget /geek/patches/files/driplusxv.fix.diff
#patch -p0 < driplusxv.fix.diff
#if [ "$?" != "0" ]; then echo "patch failed, exiting"; exit; fi;

cd ../../../../../../

echo "Making (log available in make.log)"
make World 2>&1 | tee make.log
if [ "$?" != "0" ]; then echo "make failed (see make.log), exiting"; exit; fi;

echo "Ready to install, backing up /usr/X11R6 to ./X11R6-bck... (I need the root password to read every file)"
su -c "cp -r /usr/X11R6 ./X11R6-bck"
echo Enter root password to install
su -c "make install"
if [ "$?" != "0" ]; then echo "argh, make install failed, use the backup in ./X11R6-bck"; exit; fi;

echo Compiling kernel module
cd programs/Xserver/hw/xfree86/os-support/linux/drm/kernel/
make -f Makefile.linux clean all mach64.o
if [ "$?" != "0" ]; then echo "module make failed, use the backup in ./X11R6-bck"; exit; fi;

echo Enter root password to install kernel module

su -c "mkdir /lib/modules/`uname -r`/kernel/drivers/char/drm/; cp mach64.o /lib/modules/`uname -r`/kernel/drivers/char/drm/"
if [ "$?" != "0" ]; then echo "module install failed"; exit; fi;

echo All done
echo 
echo Edit your /etc/X11/XF86Config-4 file so that your video card entry contains
echo Driver	"ati"
echo Option 	"DMAMode" "mmio"
echo DMAMode mmio is for powerpc ; x86 can use faster "async"
echo
echo "and your Screen section should use 16 bpp DefaultDepth (or DRI will be disabled)"
wget /geek/powerbook/XF86Config-4
echo "A sample XF86Config-4 file, working for my powerbook lombard, has been downloaded in this directory."
echo
echo "After that just restart X."
