#/bin/sh
#
# Boot time script to select an X11 configuration to use based on PCI IDs.

errmsg()
{
	echo "."
	echo "$@"
	echo -n "Resuming local package initialization:"
	exit 0
}

if [ "$1" != "start" ]; then
	exit 0
fi

if ! [ -r /etc/X11/XF86Config-4 -o -L /etc/X11/XF86Config-4 ]; then
    chip=""
    id=`pciconf -r pci1:0:0 0 2>/dev/null | sed -e 's/ //g'`
    case "${id}" in
	0x514c1002)
	    # Radeon 8500
	    agpline=`dmesg | grep agp0 | tail -1`
	    memrange=`echo $agpline | sed -e 's/^.*mem \([0-9a-fx]*\)-\([0-9a-fx]*\) at.*$/\1 \2/'`
	    high=`echo $memrange | cut -d' ' -f 2`
	    high=`printf %u $high`
	    low=`echo $memrange | cut -d' ' -f 1`
	    low=`printf %u $low`
	    aperture=$(( (`expr $high - $low` + 1) / 1024 / 1024 ))
	    case "${aperture}" in
		64)
		    chip="8500.wxscan"
		    ;;
		128)
		    chip="8500"
		    ;;
		*)
		    errmsg "Radeon 8500: Invalid AGP Aperture Size: $aperture"
		    ;;
	    esac
	    ;;
	0x59601002)
	    # Radeon 9200 Pro
	    chip="9200"
	    ;;
	0xffffffff)
	    # No device, check for Intel Graphics
	    id=`pciconf -r pci0:2:0 0 2>/dev/null | sed -e 's/ //g'`
	    case "${id}" in
		0x25728086)
		    # Intel 865G
		    chip="865"
		    ;;
	    esac
	    ;;
    esac
    if [ -n "${chip}" ]; then
	if [ -r /etc/X11/XF86Config-4.${chip} ]; then
	    ( cd /etc/X11 ; ln -s XF86Config-4.${chip} XF86Config-4 )
	    echo -n " X11 Config ${chip}"
	else
	    errmsg "Missing XFree86 Config file for chip \"${chip}\""
	fi
    else
	errmsg "Unknown graphics adapter: ${id}"
    fi
fi
