#!/bin/sh
# a script to update Driver "..." in xorg.conf as seen on PCI
# by Michael Shigorin <mike@osdn.org.ua>

debug() {
	test -n "$DEBUG" && echo $0: $*
}

error() {
	echo $0: $* >&2; exit 1
}

( which x11createconfig && which x11setupdrv && \
  which x11setupdrv ) >&/dev/null || {
	error "needed utilities missing"
}

CARD=`x11createconfig -c | awk -F: '/^card:/ { print $2; }'` || {
	error "x11createconfig failed"
}

debug "card: [$CARD]"

DRIVER=`vcardinfo "$CARD" | awk '/^xdriver\t/ { print $2; }'` || {
	error "pciscan failed"
}

[ -z "$DRIVER" ] && DRIVER="vesa"
debug "driver: [$DRIVER]"

CURRENT=`x11setupdrv -d --nosetup | sed 's/driver name: //'` || {
	error "x11setupdrv failed"
}

debug "current: [$CURRENT]"

[ "$DRIVER" == "$CURRENT" ] && {
	debug "nothing to modify, driver is the same"
	exit 0	# nothing to be done
}

debug ">> setting driver to [$DRIVER]"

x11setupdrv -s "$DRIVER" || {
	error "x11setupdrv failed to modify configuration"
}
