#!/bin/sh
source "$RUNES_INTERFACE_DIR/RUNES/functions.sh"
interface=$RUNES_INTERFACE_DIR/$RUNES_RUNE_NAME

config=$RUNES_CONFIG_PATH
[ -f ./defaults  ] && . ./defaults
[ -f $config/defaults ] && . $config/defaults
# x*=

eval $(runes_cmdline param_ X11)
# xdriver=
# xdepth=
# GL=


if [ -f $config/xorg.conf.in ];then
	if [ -n "$param_GL" ];then
		xconf_in=$config/xorg.conf.glx.in
	else
		xconf_in=$config/xorg.conf.in
	fi
else
	if [ -n "$param_GL" ];then
		xconf_in=./xorg.conf.glx.in
	else
		xconf_in=./xorg.conf.in
	fi
fi

# xdriver= xdpms xnodpms xnopm xnonopm
# xmode= xdepth=
# xhsyncl= xhsynch= xhsync=
# xvsyncl= xvsynch= xvsync=
# xmousedev= xmouseproto= xemu3 xnoemu3

# hwsetup
# rewrite the default parameters with the reports from hwsetup
[ -n "$XMODULE" ] && xdriver=$XMODULE
[ -n "$MOUSE_DEVICE" ] && xmousedev=$MOUSE_DEVICE
[ -n "$XMOUSETYPE" ] && xmouseproto=$XMOUSETYPE

# < Device >
if [ -n "$param_GL" ]; then
	sdx_line=$(./bin/sdx `uname -r` pci.ids)
	if [ -n "$sdx_line" ];then
		DEVICE_DRIVER=$(echo $sdx_line | cut -d ' ' -f 1)
		DEVICE_NAME=$(echo $sdx_line | cut -d ' ' -f 2-)
	fi
	[ -e $config/glfunc ] && $config/glfunc $DEVICE_DRIVER "$DEVICE_NAME"
fi
DEVICE_DRIVER=${param_xdriver:-"$DEVICE_DRIVER"}
[ -z "$DEVICE_DRIVER" ] && DEVICE_DRIVER="vesa"
#[ "$DEVICE_DRIVER" == "vesa" ] && DEVICE_DRIVER="fbdev"

#case "$DEVICE_DRIVER" in ati|radeon|nv|trident)
case "$DEVICE_DRIVER" in radeon|trident)
	ON_DEVICE_SW_CURSOR="	Option		\"sw_cursor\"";;
*)
	ON_DEVICE_SW_CURSOR="";;
esac

if [ "$DEVICE_DRIVER" == "vmware" ];then
	ON_DEVICE_VMWARE="	BusID		\"PCI:0:15:0\""
else
	ON_DEVICE_VMWARE=""
fi

if [ -z "$xnodpms" -a -n "$param_xdpms" -o "$xdpms" == "yes" ];then
	ON_DEVICE_DPMS="	Option		\"DPMS\""
else
	ON_DEVICE_DPMS=""
fi

if [ -z "$xnonopm" -a -n "$param_xnopm" -o "$xnopm" == "yes" ];then
	ON_DEVICE_NOPM="	Option		\"NoPM\""
else
	ON_DEVICE_NOPM=""
fi


# < Screen >
SCREEN_MODE_ADD=${param_xmode:-"$xmode"}

if [ -n "$SCREEN_MODE_ADD" ];then
	SCREEN_MODES="Modes \"$SCREEN_MODE_ADD\" \"1280x1024\" \"1024x768\" \"800x600\" \"640x480\" \"1280x800\" \"1440x900\""
else
	SCREEN_MODES="Modes \"1280x1024\" \"1024x768\" \"800x600\" \"640x480\" \"1280x800\" \"1440x900\""
fi

SCREEN_DEPTH=${param_xdepth:-"$xdepth"}


# < Monitor >
MONITOR_HSYNCL=${param_xhsyncl:-"$xhsyncl"}
MONITOR_VSYNCL=${param_xhsyncl:-"$xvsyncl"}

MONITOR_HSYNCH=${param_xhsynch:-"$param_xhsync"}
MONITOR_HSYNCH=${MONITOR_HSYNCH:-"$xhsynch"}

MONITOR_VSYNCH=${param_xvsynch:-"$param_xvsync"}
MONITOR_VSYNCH=${MONITOR_VSYNCH:-"$xvsynch"}

MONITOR_MODELINES="./modelines"


# < Mouse >
MOUSE_DEVICE=${param_xmousedev:-"$xmousedev"}
MOUSE_PROTO=${param_xmouseproto:-"$xmouseproto"}

if [ -z "$param_xnoemu3" -a -n "$param_xemu3" -o "$xemu3" == "yes" ];then
	ON_MOUSE_EMU3="	Option		\"Emulate3Buttons\"	\"yes\"\n	Option		\"Emulate3Timeout\"	\"70\""
else
	ON_MOUSE_EMU3=""
fi


# Output
cp -f /etc/X11/XF86Config /etc/X11/XF86Config.orig 2>/dev/null
cp -f /etc/X11/XF86Config-4 /etc/X11/XF86Config-4.orig 2>/dev/null
cp -f /etc/X11/xorg.conf /etc/X11/xorg.conf.orig 2>/dev/null
rm -f /etc/X11/XF86Config /etc/X11/XF86Config-4 /etc/X11/xorg.conf 2>/dev/null
ln -fs /etc/X11/xorg.conf /etc/X11/XF86Config 2>/dev/null
ln -fs /etc/X11/xorg.conf /etc/X11/XF86Config-4 2>/dev/null

#mv /usr/bin/Xorg /usr/bin/Xorg_orig
#cp -f ./Xorg_crashhook /usr/bin/Xorg


sed -e "s|@@DEVICE_DRIVER@@|$DEVICE_DRIVER|g" \
    -e "s|@@ON_DEVICE_SW_CURSOR@@|$ON_DEVICE_SW_CURSOR|g" \
    -e "s|@@ON_DEVICE_VMWARE@@|$ON_DEVICE_VMWARE|g" \
    -e "s|@@ON_DEVICE_DPMS@@|$ON_DEVICE_DPMS|g" \
    -e "s|@@ON_DEVICE_NOPM@@|$ON_DEVICE_NOPM|g" \
    -e "s|@@MONITOR_HSYNCL@@|$MONITOR_HSYNCL|g" \
    -e "s|@@MONITOR_VSYNCL@@|$MONITOR_VSYNCL|g" \
    -e "s|@@MONITOR_HSYNCH@@|$MONITOR_HSYNCH|g" \
    -e "s|@@MONITOR_VSYNCH@@|$MONITOR_VSYNCH|g" \
    -e "/@@MONITOR_MODELINES@@/r $MONITOR_MODELINES" -e "s|@@MONITOR_MODELINES@@||g" \
    -e "s|@@MOUSE_DEVICE@@|$MOUSE_DEVICE|g" \
    -e "s|@@MOUSE_PROTO@@|$MOUSE_PROTO|g" \
    -e "s|@@ON_MOUSE_EMU3@@|$ON_MOUSE_EMU3|g" \
    -e "s|@@SCREEN_MODES@@|$SCREEN_MODES|g" \
    -e "s|@@SCREEN_DEPTH@@|$SCREEN_DEPTH|g" \
    $xconf_in > /etc/X11/xorg.conf


# Information
echo "
DEVICE_DRIVER=\"$DEVICE_DRIVER\";
ON_DEVICE_SW_CURSOR=\"$([ -n "$ON_DEVICE_SW_CURSOR" ] && echo 1 || echo 0)\";
ON_DEVICE_VMWARE=\"$([ -n "$ON_DEVICE_VMWARE" ] && echo 1 || echo 0)\";
ON_DEVICE_DPMS=\"$([ -n "$ON_DEVICE_DPMS" ] && echo 1 || echo 0)\";
ON_DEVICE_NOPM=\"$([ -n "$ON_DEVICE_NOPM" ] && echo 1 || echo 0)\";
MONITOR_HSYNCL=\"$MONITOR_HSYNCL\";
MONITOR_VSYNCL=\"$MONITOR_VSYNCL\";
MONITOR_HSYNCH=\"$MONITOR_HSYNCH\";
MONITOR_VSYNCH=\"$MONITOR_VSYNCH\";
MOUSE_DEVICE=\"$MOUSE_DEVICE\";
MOUSE_PROTO=\"$MOUSE_PROTO\";
ON_MOUSE_EMU3=\"$([ -n "$ON_MOUSE_EMU3" ] && echo 1 || echo 0)\";
SCREEN_MODES=\"$SCREEN_MODES\";
SCREEN_DEPTH=\"$SCREEN_DEPTH\";
" > $interface


echo "${INFO}X Window System:
  Driver: $DEVICE_DRIVER     $DEVICE_NAME
  HorizSync: $MONITOR_HSYNCL - $MONITOR_HSYNCH
  VertRefresh: $MONITOR_VSYNCL - $MONITOR_VSYNCH
  Mouse: $MOUSE_DEVICE ($MOUSE_PROTO)$([ -n "$ON_MOUSE_EMU3" ] && echo " + 3 button emulation")
  Modes: ${SCREEN_MODES##Modes }${NORMAL}"

