#! /bin/sh
# /etc/init.d/fglrx-driver: build and load fglrx kernel module
# update-rc.d fglrx-driver start 90 2 3 4 5 .
# gurkan@linuks.mine.nu, http://www.linuks.mine.nu/ati/ or
# http://kaerpf.ethz.ch/pub/debian-local/share/kernel/

case "$1" in
    start)
	echo -n "Starting ATI fglrx magic: "
	if [ -f /lib/modules/`uname -r`/misc/fglrx.o ]; then
	    echo "."
	    exit 0
	fi
	if (lspci | grep VGA | grep Radeon > /dev/null); then
	    echo -n "ATI fglrx found..."
	    #modprobe fglrx; lsmod | grep Radeon | grep -v unused || (
	    (
		rmmod fglrx
		apt-get install kernel-headers-`uname -r`
		cd /usr/src/
		rm -rf linux
		ln -sf kernel-headers-`uname -r` linux
		
		cd /usr/src/linux
		cp /boot/config-`uname -r` .config
		
		cd /usr/src
		NVBIN=ati-driver-installer-8.28.8.run
		rm ${NVBIN}
		wget http://debian.ethz.ch/pub/debian-local/share/kernel/${NVBIN}
		chmod +x ./${NVBIN}
		./${NVBIN} --buildpkg Debian/sarge
		dpkg -i fglrx-driver_*deb fglrx-kernel-src_*deb
		m-a prepare
		m-a update
		m-a a-i fglrx
		dpkg -i fglrx-kernel-2*.deb
		
		modprobe fglrx
	    )
	    echo "."
	else
	    echo "no ATI fglrx found."
	fi
        ;;
    restart|force-reload)
	# nothing to do
	:
	;;
    stop)
	# nothing to do
	:
	;;
    *)
        echo "Usage: /etc/init.d/fglrx-driver start"
        exit 1
	;;
esac

exit 0
