#!/bin/sh

XORGCONF="$1"
CONFIG="/etc/sysconfig/x11_autosetup"
PATH="/usr/lib/alterator-x11:$PATH"
[ -s "$CONFIG" ] && source "$CONFIG"

tail_res()
{
    sed -n "/$1/,\$ p" /usr/share/alterator-x11/resolutions|
	tac|
	tr '\n' ','|
	sed 's/,$//'
}

try_xconf() 
{
	[ -n "$xresolution" ] && {
		xconf -R "${xresolution/ /,}" "$XORGCONF" "$XORGCONF"
		exit 0
	} ||:
}

# x11_autosetup -x HxV
xresolution="$OPT_XRES"
try_xconf

# sysconfig
xresolution="$XRESOLUTION"
try_xconf

# DDC autodetect
# FIXME: this is geared towards CRT, TFTs should just get native resolution!
size="$(ddcsize)"
[ -n "$size" ] && ddcxres="$(ddcreslist|ddcresbest "${size% *}" "${size#* }")"
[ -n "$ddcxres" ] && xresolution="$(tail_res "$ddcxres")"
try_xconf

# finally...
xresolution="auto"
try_xconf
