#!/bin/sh

if [ ! $DISPLAY = "" ]; then
DCMD="Xdialog --wrap" 
CMD="rxvt -e "
else
DCMD="dialog"
CMD=""
fi


TMPFILE="/tmp/mkxf86config-$$"
MONITORTMP="$TMPFILE-monitor"

rm -f "$TMPFILE" "$MONITORTMP"

# Read in what vlsetup has found for X
# [ -f /etc/sysconfig/vlocity ] && . /etc/sysconfig/vlocity

#if [ "$XMODULE" = "nv" ] && [ -e /lib/modules/`uname -r`/kernel/drivers/video/NVdriver ]; then
#XMODULE="nvidia"
#fi

#if [ "$XMODULE" = "nv" ] && [ -e /lib/modules/`uname -r`/kernel/drivers/video/nvidia ]; then
#XMODULE="nvidia"
#fi

#if [ "$XMODULE" = "nv" ]; then
#installpkg /tmp/nvidia/NVIDIA_GLX-1.0-3123-i586-1.tgz 1>/dev/null 2>/dev/null
#kern=`uname -r`
#if echo $kern | grep -F ide ; then
#installpkg /tmp/nvidia/NVIDIA_kernel-1.0-3123-pentide-1-i586-1.tgz 1>/dev/null 2>/dev/null
#else
#installpkg /tmp/nvidia/NVIDIA_kernel-1.0-3123-pentscsi-1-i586-1.tgz 1>/dev/null 2>/dev/null
#fi
#XMODULE="nvidia"
#rm -rf /tmp/nvidia
#ldconfig 1>/dev/null 2>/dev/null
#depmod -a 1>/dev/null 2>/dev/null
#fi
#Lets test keyboard for non-us users
kmapset
. /etc/sysconfig/vlocity

# Wheel
$DCMD --title "Wheel Mouse Setup" --yesno "Does your mouse have a scroll wheel?" 0 0
case $? in
0)
WHEEL='s|"PS/2"|"IMPS/2"\
Option          "ZAxisMapping"          "4 5"|g;'
;;
1)
WHEEL=""
;;
255)
WHEEL=""
;;
esac

# Create mouse link and insert a mouse default type into XF86Config
# if not already done by vlsetup
[ -e /dev/mouse ] || ln -sf /dev/ttyS0 /dev/mouse
# PROTO="${XMOUSETYPE:-Auto}"
PROTO="${XMOUSETYPE0:-Microsoft}"
NOEMU=""
[ "${XEMU3}" = "no" ] && NOEMU='s|^.*Emulate3|# No 2 -> 3 Button emulation|g'
#DEADKEYS=""
#[ "$XKEYBOARD" = "de" ] || DEADKEYS='s|^.*nodeadkeys.*$||g;'
if [ -n "XMODULE" ]; then
# Check for Framebuffer X-Modules and initialize framebuffer module
case "$XMODULE" in
pvr2fb) modprobe "$XMODULE" >/dev/null 2>&1 ; XMODULE="fbdev"; ;;
esac
fi

# Try to get Monitor data via kudzu ddcxinfo


MONITOROK=""
MODEL="$(ddcprobe 2>/dev/null | awk 'BEGIN{m="Generic";i="Monitor"}/^Manufacturer:/{m=$2};/^ID:/{i=$2};END{print m " "i}')"
HREFRESH="$(ddcxinfo -hsync 2>/dev/null)"
VREFRESH="$(ddcxinfo -vsync 2>/dev/null)"

MINHREFRESH=""
MAXHREFRESH=""
MINVREFRESH=""
MAXVREFRESH=""


rm -f "$MONITORTMP"
TFT=""
case "$MODEL" in *[Ll][Cc][Dd]*|*[Tt][Ff][Tt]*) TFT="true"; ;; esac
MINHREFRESH="${HREFRESH%%-*}"
MAXHREFRESH="${HREFRESH##*-}"
MINVREFRESH="${VREFRESH%%-*}"
MAXVREFRESH="${VREFRESH##*-}"

$DCMD --title "Screen Resolution Selection" \
--menu "Use the cursor or [+] [-] keys to navigate. \n\  Press [enter] to select the largest screen size you wish to use." 18 76 7 \
 "1800" "1800x1440" \
 "1600" "1600x1200" \
 "1280" "1280x1024" \
 "1152" "1152x864" \
 "1024" "1024x768" \
 "800"  "800x600" 2> /tmp/screen
if [ $? = 1 -o $? = 255 ]; then
  rm -f /tmp/screen
 clear
  exit 0
fi

SCREEN="`cat /tmp/screen`"
rm -f /tmp/screen

 if [ "$SCREEN" = "1800" ]; then
 MODES='Modes "1800x1440" "1600x1200" "1280x1024" "1152x864" "1024x768" "800x600" "640x480"'
 fi
 if [ "$SCREEN" = "1600" ]; then
 MODES='Modes "1600x1200" "1280x1024" "1152x864" "1024x768" "800x600" "640x480"'
 fi
 if [ "$SCREEN" = "1280" ]; then
 MODES='Modes "1280x1024" "1152x864" "1024x768" "800x600" "640x480"'
 fi
 if [ "$SCREEN" = "1152" ]; then
 MODES='Modes "1152x864" "1024x768" "800x600" "640x480"'
 fi
 if [ "$SCREEN" = "1024" ]; then
 MODES='Modes "1024x768" "800x600" "640x480"'
 fi
 if [ "$SCREEN" = "800" ]; then
 MODES='Modes "800x600" "640x480"'
 fi




  # Everything should be OK now.
  MONITOROK="yes"
# else
# HREFRESH=""
# VREFRESH=""
# fi

#if [ -n "$MONITOROK" ]; then
if [ "$HREFRESH" = "0-0" ] || [ "$VREFRESH" = "0-0" ]; then

cat >"$MONITORTMP" <<EOF
Section "Monitor"
        Identifier   "Monitor0"
        ModelName    "$MODEL"
EndSection
EOF
else
cat >"$MONITORTMP" <<EOF
Section "Monitor"
	Identifier   "Monitor0"
	ModelName    "$MODEL"
# These are the DDC-probed settings and modelines
	HorizSync    $HREFRESH
	VertRefresh  $VREFRESH
EndSection
EOF
fi
#else



DEPTH=""
# We need to check this because serial mice have long timeouts
SERIALMOUSE="$(ls -l1 /dev/mouse* 2>/dev/null | awk '/ttyS/{print $NF ; exit 0}')"
if [ -n "$SERIALMOUSE" ]; then
SERIALMOUSE="s|/dev/ttyS0|$SERIALMOUSE|g;"
else
SERIALMOUSE='s|^.*InputDevice.*"Serial Mouse".*$|# Serial Mouse not detected|g;'
fi

# PS/2 bug: Some keyboards are incorrectly used as mice in XFree. :-(
PSMOUSE="$(ls -l1 /dev/mouse* 2>/dev/null | awk '/psaux/{print $NF ; exit 0}')"
if [ -n "$PSMOUSE" ]; then
PSMOUSE=""
else
PSMOUSE='s|^.*InputDevice.*"PS/2 Mouse".*$|# PS/2 Mouse not detected|g;'
fi

USBMOUSE="$(ls -l1 /dev/mouse* 2>/dev/null | awk '/input/{print $NF ; exit 0}')"
if [ -n "$USBMOUSE" ]; then
USBMOUSE=""
else
USBMOUSE='s|^.*InputDevice.*"USB Mouse".*$|# USB Mouse not detected|g;'
fi

if [ ! -f /etc/XF86Config-4 -a ! -f /etc/X11/XF86Config-4 ]; then 
rm -f /etc/XF86Config-4 /etc/X11/XF86Config-4 2>/dev/null
fi
# VMWare special handling
VMWARE=""
if [ "$XMODULE" = "vmware" ]; then
VMWARE='s|^.*BusID.*PCI.*$|BusID "PCI:0:15:0"|g;'
DEPTH='s|DefaultColorDepth |# DefaultColorDepth |g;'
fi

# These drivers need the sw_cursor option
SWCURSOR=""
case "$XMODULE" in ati|radeon|trident) SWCURSOR='s|^.*#Option.*"sw_cursor".*$|Option "sw_cursor"|g;';; esac

sed -e 's|@@PROTOCOL@@|'"$PROTO"'|g;'"$NOEMU" \
    -e '/@@MONITOR@@/r '"$MONITORTMP" \
    -e 's|@@MONITOR@@||g' \
    -e 's|Identifier  *"Monitor0"|Identifier   "Monitor0"\
Option "DPMS"|g' \
    -e 's|@@XMODULE@@|'"$XMODULE"'|g;'"$VMWARE""$SERIALMOUSE""$USBMOUSE""$PSMOUSE""$SWCURSOR""$WHEEL" \
    -e 's|@@MODES@@|'"$MODES"'|g;'"$DEPTH" \
    -e 's|"XkbLayout" *"[^"]*"|"XkbLayout" "'"$XKEYBOARD"'"|g;'"$DEADKEYS" \
    /etc/X11/XF86Config-4.in > /etc/X11/XF86Config-4

rm -f "$TMPFILE" "$MONITORTMP" 2>/dev/null

