#!/bin/sh

# dexconf: Debian X Configurator
#
# Copyright 2000,2001 Progeny Linux Systems, Inc.
# Author: Branden Robinson
#
# Licensed under the GNU GPL, version 2 or (at your option) any later version.
#
# This tool is a backend which uses debconf database values.

set -e

# the error-out function
bomb () {
  echo "$PROGNAME: $*" | fold -s -w ${COLUMNS:-80} >&2
  exit 1;
}

# wrapper around db_get to ensure that the info we try to retrieve exists; it
# is (almost) always a fatal error for the values to be null
fetch () {
  db_get $1 || true
  if [ -z "$RET" ]; then
    ERRMSG="cannot generate configuration file; $1 not set.  Aborting.  Reconfigure the X server with \"dpkg-reconfigure"
    if [ -n "$XSERVERPKG" ]; then
      ERRMSG="$ERRMSG $XSERVERPKG"
    fi
    ERRMSG="$ERRMSG\" to correct this problem."
    bomb "$ERRMSG"
  fi;
}

PROGNAME=$(basename "$0")

#TODO: use getopt(1)
while [ -n "$1" ]; do
  case $1 in
    -o|--output)
      shift
      XF86CONFIG=$1
      ;;
    -h|--help)
      echo "Usage: $PROGNAME [ (-o | --output) outputfile ]"
      exit 0
      ;;
    *)
      bomb "argument $1 not understood; aborting."
  esac
  shift
done

DEXCONFTMPDIR=
TMP=

# $DEXCONFTMPDIR may not be set yet, and $TMP may not be set yet or have
# embedded whitespace
trap 'rm -r "$DEXCONFTMPDIR"; \
      rm -f "$TMP"; \
      fi; \
      bomb "received signal; aborting."' 1 2 3 15

# source debconf library
. /usr/share/debconf/confmodule

# which file and format are we using?
fetch shared/default-x-server
XSERVERPKG="$RET"
case "$XSERVERPKG" in
  xserver-3dlabs|xserver-8514|xserver-agx|xserver-fbdev|xserver-i128|xserver-mach32|xserver-mach64|xserver-mach8|xserver-mono|xserver-p9000|xserver-s3|xserver-s3v|xserver-svga|xserver-tga|xserver-vga16|xserver-w32)
    FORMAT=3
    : ${XF86CONFIG:=/etc/X11/XF86Config}
    ;;
  xserver-xfree86)
    FORMAT=4
    : ${XF86CONFIG:=/etc/X11/XF86Config-4}
    ;;
  *)
    bomb "this tool does not know how to configure the \"$XSERVERPKG\" X server."
esac

# setup temp dir
for DIRTEST in /etc/X11/dexconf-tmp ./dexconf-tmp-$$ /tmp/dexconf-tmp-$$; do
  if [ -w $(dirname $DIRTEST) ]; then
    rm -rf $DIRTEST
    mkdir -p -m 0700 $DIRTEST && break
  fi
done
DEXCONFTMPDIR=$DIRTEST

if [ ! -w $DEXCONFTMPDIR ]; then
  bomb "unable to create temporary work directory \"$DEXCONFTMPDIR\"."
fi

# Format 3:
#        Files (File pathnames)
#        Module (Dynamic module loading)
#        ServerFlags (Server flags)                         NOT USED BY DEXCONF
#        Keyboard (Keyboard configuration)
#        Pointer (Pointer configuration)
#        Monitor (Monitor description)
#        Device (Graphics device description)
#        Screen (Screen configuration)
#        XInput (Extended Input devices configuration)       NOT USED BY DEXCONF

# Format 4:
#           Files          File pathnames
#           ServerFlags    Server flags                      NOT USED BY DEXCONF
#           Module         Dynamic module loading
#           InputDevice    Input device description
#           Device         Graphics device description
#           VideoAdaptor   Xv video adaptor description      NOT USED BY DEXCONF
#           Monitor        Monitor description
#           Modes          Video modes descriptions          NOT USED BY DEXCONF
#           Screen         Screen configuration
#           ServerLayout   Overall layout
#           DRI            DRI-specific configuration
#           Vendor         Vendor-specific configuration     NOT USED BY DEXCONF

### HEADER

if [ "$FORMAT" = "3" ]; then
  cat > $DEXCONFTMPDIR/Header << SECTION
### BEGIN DEBCONF SECTION
# XF86Config (XFree86 server configuration file) generated by dexconf, the
# Debian X Configuration tool, using values from the debconf database.
#
# Edit this file with caution, and see the XF86Config-v3 manual page.
# (Type "man XF86Config-v3" at the shell prompt.)
#
# If you want your changes to this file preserved by dexconf, only make changes
# before the "### BEGIN DEBCONF SECTION" line above, and/or after the
# "### END DEBCONF SECTION" line below.
#
# To change things within the debconf section, run the command:
#   dpkg-reconfigure $XSERVERPKG
# as root.  Also see "How do I add custom sections to a dexconf-generated
# XF86Config or XF86Config file?" in /usr/share/doc/xfree86-common/FAQ.gz.
SECTION
else
  cat > $DEXCONFTMPDIR/Header << SECTION
### BEGIN DEBCONF SECTION
# XF86Config-4 (XFree86 server configuration file) generated by dexconf, the
# Debian X Configuration tool, using values from the debconf database.
#
# Edit this file with caution, and see the XF86Config-4 manual page.
# (Type "man XF86Config-4" at the shell prompt.)
#
# If you want your changes to this file preserved by dexconf, only make changes
# before the "### BEGIN DEBCONF SECTION" line above, and/or after the
# "### END DEBCONF SECTION" line below.
#
# To change things within the debconf section, run the command:
#   dpkg-reconfigure $XSERVERPKG
# as root.  Also see "How do I add custom sections to a dexconf-generated
# XF86Config or XF86Config-4 file?" in /usr/share/doc/xfree86-common/FAQ.gz.
SECTION
fi

### FILES

if [ "$FORMAT" = "3" ]; then
  fetch shared/xfree86v3/config/write_files_section
  if [ "$RET" = "true" ]; then
    cat > $DEXCONFTMPDIR/Files << SECTION
Section "Files"
	FontPath	"unix/:7100"			# local font server
	# if the local font server has problems, we can fall back on these
	FontPath	"/usr/lib/X11/fonts/misc"
	FontPath	"/usr/lib/X11/fonts/cyrillic"
	FontPath	"/usr/lib/X11/fonts/100dpi/:unscaled"
	FontPath	"/usr/lib/X11/fonts/75dpi/:unscaled"
	FontPath	"/usr/lib/X11/fonts/Type1"
	FontPath	"/usr/lib/X11/fonts/CID"
	FontPath	"/usr/lib/X11/fonts/Speedo"
	FontPath	"/usr/lib/X11/fonts/100dpi"
	FontPath	"/usr/lib/X11/fonts/75dpi"
EndSection
SECTION
  fi
else
  fetch xserver-xfree86/config/write_files_section
  if [ "$RET" = "true" ]; then
    cat > $DEXCONFTMPDIR/Files << SECTION
Section "Files"
	FontPath	"unix/:7100"			# local font server
	# if the local font server has problems, we can fall back on these
	FontPath	"/usr/lib/X11/fonts/misc"
	FontPath	"/usr/lib/X11/fonts/cyrillic"
	FontPath	"/usr/lib/X11/fonts/100dpi/:unscaled"
	FontPath	"/usr/lib/X11/fonts/75dpi/:unscaled"
	FontPath	"/usr/lib/X11/fonts/Type1"
	FontPath	"/usr/lib/X11/fonts/Speedo"
	FontPath	"/usr/lib/X11/fonts/100dpi"
	FontPath	"/usr/lib/X11/fonts/75dpi"
EndSection
SECTION
  fi
fi

### MODULE

if [ "$FORMAT" = "3" ]; then
  # module list may be null
  db_get shared/xfree86v3/config/modules || true
  if [ -n "$RET" ]; then
    # debconf doesn't return the list in the format we need
    MODULES=$( echo $(IFS=", "; set -- $RET; while [ $# -gt 0 ]; do echo $1; shift; done) )
    printf "Section \"Module\"\n" > $DEXCONFTMPDIR/Module
    for MODULE in $MODULES; do
      printf "\tLoad\t\"$MODULE.so\"\n" >> $DEXCONFTMPDIR/Module
    done
    printf "EndSection\n" >> $DEXCONFTMPDIR/Module
  fi
else
  # module list may be null
  db_get xserver-xfree86/config/modules || true
  if [ -n "$RET" ]; then
    # debconf doesn't return the list in the format we need
    MODULES=$( echo $(IFS=", "; set -- $RET; while [ $# -gt 0 ]; do echo $1; shift; done) )
    printf "Section \"Module\"\n" > $DEXCONFTMPDIR/Module
    for MODULE in $MODULES; do
      printf "\tLoad\t\"$MODULE\"\n" >> $DEXCONFTMPDIR/Module
    done
    printf "EndSection\n" >> $DEXCONFTMPDIR/Module
  fi
fi

### KEYBOARD / INPUTDEVICE

if [ "$FORMAT" = "3" ]; then
  fetch shared/xfree86v3/config/inputdevice/keyboard/rules
  XKB_RULES="$RET"
  fetch shared/xfree86v3/config/inputdevice/keyboard/model
  XKB_MODEL="$RET"
  fetch shared/xfree86v3/config/inputdevice/keyboard/layout
  XKB_LAYOUT="$RET"
  # XkbVariant and XkbOptions are permitted to be null
  db_get shared/xfree86v3/config/inputdevice/keyboard/variant
  XKB_VARIANT="$RET"
  db_get shared/xfree86v3/config/inputdevice/keyboard/options
  XKB_OPTIONS="$RET"
  cat > $DEXCONFTMPDIR/InputDeviceKeyboard << SECTION
Section "Keyboard"
	Protocol	"Standard"
	XkbRules	"$XKB_RULES"
	XkbModel	"$XKB_MODEL"
	XkbLayout	"$XKB_LAYOUT"
SECTION
  if [ -n "$XKB_VARIANT" ]; then
    printf "\tXkbVariant\t\"$XKB_VARIANT\"\n" >> $DEXCONFTMPDIR/InputDeviceKeyboard
  fi
  if [ -n "$XKB_OPTIONS" ]; then
    printf "\tXkbOptions\t\"$XKB_OPTIONS\"\n" >> $DEXCONFTMPDIR/InputDeviceKeyboard
  fi
  printf "EndSection\n" >> $DEXCONFTMPDIR/InputDeviceKeyboard
else
  fetch xserver-xfree86/config/inputdevice/keyboard/rules
  XKB_RULES="$RET"
  fetch xserver-xfree86/config/inputdevice/keyboard/model
  XKB_MODEL="$RET"
  fetch xserver-xfree86/config/inputdevice/keyboard/layout
  XKB_LAYOUT="$RET"
  # XkbVariant and XkbOptions are permitted to be null
  db_get xserver-xfree86/config/inputdevice/keyboard/variant
  XKB_VARIANT="$RET"
  db_get xserver-xfree86/config/inputdevice/keyboard/options
  XKB_OPTIONS="$RET"
  cat > $DEXCONFTMPDIR/InputDeviceKeyboard << SECTION
Section "InputDevice"
	Identifier	"Generic Keyboard"
	Driver		"keyboard"
	Option		"CoreKeyboard"
	Option		"XkbRules"	"$XKB_RULES"
	Option		"XkbModel"	"$XKB_MODEL"
	Option		"XkbLayout"	"$XKB_LAYOUT"
SECTION
  if [ -n "$XKB_VARIANT" ]; then
    printf "\tOption\t\t\"XkbVariant\"\t\"$XKB_VARIANT\"\n" >> $DEXCONFTMPDIR/InputDeviceKeyboard
  fi
  if [ -n "$XKB_OPTIONS" ]; then
    printf "\tOption\t\t\"XkbOptions\"\t\"$XKB_OPTIONS\"\n" >> $DEXCONFTMPDIR/InputDeviceKeyboard
  fi
  printf "EndSection\n" >> $DEXCONFTMPDIR/InputDeviceKeyboard
fi

### MOUSE / INPUTDEVICE

DO_EMULATE3BUTTONS=
DO_ZAXISMAPPING=

if [ "$FORMAT" = "3" ]; then
  fetch shared/xfree86v3/config/inputdevice/mouse/port
  MOUSE_PORT="$RET"
  fetch shared/xfree86v3/config/inputdevice/mouse/protocol
  MOUSE_PROTOCOL="$RET"
  fetch shared/xfree86v3/config/inputdevice/mouse/emulate3buttons
  if [ "$RET" = "true" ]; then
    DO_EMULATE3BUTTONS=true
  fi
  fetch shared/xfree86v3/config/inputdevice/mouse/zaxismapping
  if [ "$RET" = "true" ]; then
    DO_ZAXISMAPPING=true
  fi
  printf "Section \"Pointer\"\n" > $DEXCONFTMPDIR/InputDeviceMouse
  printf "\tDevice\t\t\"$MOUSE_PORT\"\n" >> $DEXCONFTMPDIR/InputDeviceMouse
  printf "\tProtocol\t\"$MOUSE_PROTOCOL\"\n" >> $DEXCONFTMPDIR/InputDeviceMouse
  if [ -n "$DO_EMULATE3BUTTONS" ]; then
    printf "\tEmulate3Buttons\n" >> $DEXCONFTMPDIR/InputDeviceMouse
  fi
  if [ -n "$DO_ZAXISMAPPING" ]; then
    printf "\tZAxisMapping\t4 5\n" >> $DEXCONFTMPDIR/InputDeviceMouse
  fi
  printf "EndSection\n" >> $DEXCONFTMPDIR/InputDeviceMouse
else # $FORMAT == 4
  fetch xserver-xfree86/config/inputdevice/mouse/port
  MOUSE_PORT="$RET"
  fetch xserver-xfree86/config/inputdevice/mouse/protocol
  MOUSE_PROTOCOL="$RET"
  fetch xserver-xfree86/config/inputdevice/mouse/emulate3buttons
  if [ "$RET" = "true" ]; then
    DO_EMULATE3BUTTONS=true
  fi
  fetch xserver-xfree86/config/inputdevice/mouse/zaxismapping
  if [ "$RET" = "true" ]; then
    DO_ZAXISMAPPING=true
  fi

  printf "Section \"InputDevice\"\n" > $DEXCONFTMPDIR/InputDeviceMouse
  printf "\tIdentifier\t\"Configured Mouse\"\n" >> $DEXCONFTMPDIR/InputDeviceMouse
  printf "\tDriver\t\t\"mouse\"\n" >> $DEXCONFTMPDIR/InputDeviceMouse
  printf "\tOption\t\t\"CorePointer\"\n" >> $DEXCONFTMPDIR/InputDeviceMouse
  printf "\tOption\t\t\"Device\"\t\t\"$MOUSE_PORT\"\n" >> $DEXCONFTMPDIR/InputDeviceMouse
  printf "\tOption\t\t\"Protocol\"\t\t\"$MOUSE_PROTOCOL\"\n" >> $DEXCONFTMPDIR/InputDeviceMouse
  if [ -n "$DO_EMULATE3BUTTONS" ]; then
    printf "\tOption\t\t\"Emulate3Buttons\"\t\"true\"\n" >> $DEXCONFTMPDIR/InputDeviceMouse
  fi
  if [ -n "$DO_ZAXISMAPPING" ]; then
    printf "\tOption\t\t\"ZAxisMapping\"\t\t\"4 5\"\n" >> $DEXCONFTMPDIR/InputDeviceMouse
  fi
  printf "EndSection\n" >> $DEXCONFTMPDIR/InputDeviceMouse

  # Only write this stanza -- designed for USB pointers -- if the Configured
  # Mouse isn't USB.  This isn't a problem even on systems without
  # CONFIG_INPUT_MOUSEDEV, because this is not the core pointer, and failure to
  # open it is not harmful.
  if [ "$MOUSE_PORT" != "/dev/input/mice" ]; then
    printf "\nSection \"InputDevice\"\n" >> $DEXCONFTMPDIR/InputDeviceMouse
    printf "\tIdentifier\t\"Generic Mouse\"\n" >> $DEXCONFTMPDIR/InputDeviceMouse
    printf "\tDriver\t\t\"mouse\"\n" >> $DEXCONFTMPDIR/InputDeviceMouse
    printf "\tOption\t\t\"SendCoreEvents\"\t\"true\"\n" >> $DEXCONFTMPDIR/InputDeviceMouse
    printf "\tOption\t\t\"Device\"\t\t\"/dev/input/mice\"\n" >> $DEXCONFTMPDIR/InputDeviceMouse
    printf "\tOption\t\t\"Protocol\"\t\t\"ImPS/2\"\n" >> $DEXCONFTMPDIR/InputDeviceMouse
    if [ -n "$DO_EMULATE3BUTTONS" ]; then
      printf "\tOption\t\t\"Emulate3Buttons\"\t\"true\"\n" >> $DEXCONFTMPDIR/InputDeviceMouse
    fi
    if [ -n "$DO_ZAXISMAPPING" ]; then
      printf "\tOption\t\t\"ZAxisMapping\"\t\t\"4 5\"\n" >> $DEXCONFTMPDIR/InputDeviceMouse
    fi
    printf "EndSection\n" >> $DEXCONFTMPDIR/InputDeviceMouse
  fi
fi

### DEVICE

if [ "$FORMAT" = "3" ]; then
  fetch shared/xfree86v3/config/device/vendor
  DEVICE_VENDOR="$RET"
  fetch shared/xfree86v3/config/device/model
  DEVICE_MODEL="$RET"
  DEVICE_IDENTIFIER="$DEVICE_VENDOR $DEVICE_MODEL"
  # VideoRam is permitted to be null
  db_get shared/xfree86v3/config/device/video_ram
  DEVICE_VIDEO_RAM="$RET"
  printf "Section \"Device\"\n" > $DEXCONFTMPDIR/Device
  printf "\tIdentifier\t\"$DEVICE_IDENTIFIER\"\n" >> $DEXCONFTMPDIR/Device
  printf "\tVendorName\t\"$DEVICE_VENDOR\"\n" >> $DEXCONFTMPDIR/Device
  printf "\tBoardName\t\"$DEVICE_MODEL\"\n" >> $DEXCONFTMPDIR/Device
  if [ -n "$DEVICE_VIDEO_RAM" ]; then
     printf "\tVideoRam\t$DEVICE_VIDEO_RAM\n" >> $DEXCONFTMPDIR/Device
  fi
  printf "EndSection\n" >> $DEXCONFTMPDIR/Device
else # $FORMAT == 4
  fetch xserver-xfree86/config/device/identifier
  DEVICE_IDENTIFIER="$RET"
  fetch xserver-xfree86/config/device/driver
  DEVICE_DRIVER="$RET"
  # BusID, VideoRam, and UseFBDev are permitted to be null
  db_get xserver-xfree86/config/device/bus_id
  DEVICE_BUSID="$RET"
  db_get xserver-xfree86/config/device/video_ram
  DEVICE_VIDEO_RAM="$RET"
  db_get xserver-xfree86/config/device/use_fbdev
  DEVICE_USE_FBDEV="$RET"
  printf "Section \"Device\"\n" > $DEXCONFTMPDIR/Device
  printf "\tIdentifier\t\"$DEVICE_IDENTIFIER\"\n" >> $DEXCONFTMPDIR/Device
  printf "\tDriver\t\t\"$DEVICE_DRIVER\"\n" >> $DEXCONFTMPDIR/Device
  if [ -n "$DEVICE_BUSID" ]; then
    printf "\tBusID\t\t\"$DEVICE_BUSID\"\n" >> $DEXCONFTMPDIR/Device
  fi
  if [ -n "$DEVICE_VIDEO_RAM" ]; then
     printf "\tVideoRam\t$DEVICE_VIDEO_RAM\n" >> $DEXCONFTMPDIR/Device
  fi
  if [ "$DEVICE_USE_FBDEV" = "true" ]; then
    printf "\tOption\t\t\"UseFBDev\"\t\t\"$DEVICE_USE_FBDEV\"\n" >> $DEXCONFTMPDIR/Device
  fi
  printf "EndSection\n" >> $DEXCONFTMPDIR/Device
fi

### MONITOR

if [ "$FORMAT" = "3" ]; then
  fetch shared/xfree86v3/config/monitor/vendor
  MONITOR_VENDOR="$RET"
  fetch shared/xfree86v3/config/monitor/model
  MONITOR_MODEL="$RET"
  MONITOR_IDENTIFIER="$MONITOR_VENDOR $MONITOR_MODEL"
  fetch shared/xfree86v3/config/monitor/horiz-sync
  MONITOR_HORIZ_SYNC="$RET"
  fetch shared/xfree86v3/config/monitor/vert-refresh
  MONITOR_VERT_REFRESH="$RET"
  cat > $DEXCONFTMPDIR/Monitor << SECTION
Section "Monitor"
	Identifier	"$MONITOR_IDENTIFIER"
	VendorName	"$MONITOR_VENDOR"
	ModelName	"$MONITOR_MODEL"
	HorizSync	$MONITOR_HORIZ_SYNC
	VertRefresh	$MONITOR_VERT_REFRESH
# 640x350 @ 85Hz (VESA) hsync: 37.9kHz
ModeLine "640x350"    31.5  640  672  736  832    350  382  385  445 +hsync -vsync

# 640x400 @ 85Hz (VESA) hsync: 37.9kHz
ModeLine "640x400"    31.5  640  672  736  832    400  401  404  445 -hsync +vsync

# 720x400 @ 85Hz (VESA) hsync: 37.9kHz
ModeLine "720x400"    35.5  720  756  828  936    400  401  404  446 -hsync +vsync

# 640x480 @ 60Hz (Industry standard) hsync: 31.5kHz
ModeLine "640x480"    25.2  640  656  752  800    480  490  492  525 -hsync -vsync

# 640x480 @ 72Hz (VESA) hsync: 37.9kHz
ModeLine "640x480"    31.5  640  664  704  832    480  489  491  520 -hsync -vsync

# 640x480 @ 75Hz (VESA) hsync: 37.5kHz
ModeLine "640x480"    31.5  640  656  720  840    480  481  484  500 -hsync -vsync

# 640x480 @ 85Hz (VESA) hsync: 43.3kHz
ModeLine "640x480"    36.0  640  696  752  832    480  481  484  509 -hsync -vsync

# 800x600 @ 56Hz (VESA) hsync: 35.2kHz
ModeLine "800x600"    36.0  800  824  896 1024    600  601  603  625 +hsync +vsync

# 800x600 @ 60Hz (VESA) hsync: 37.9kHz
ModeLine "800x600"    40.0  800  840  968 1056    600  601  605  628 +hsync +vsync

# 800x600 @ 72Hz (VESA) hsync: 48.1kHz
ModeLine "800x600"    50.0  800  856  976 1040    600  637  643  666 +hsync +vsync

# 800x600 @ 75Hz (VESA) hsync: 46.9kHz
ModeLine "800x600"    49.5  800  816  896 1056    600  601  604  625 +hsync +vsync

# 800x600 @ 85Hz (VESA) hsync: 53.7kHz
ModeLine "800x600"    56.3  800  832  896 1048    600  601  604  631 +hsync +vsync

# 1024x768i @ 43Hz (industry standard) hsync: 35.5kHz
ModeLine "1024x768"   44.9 1024 1032 1208 1264    768  768  776  817 +hsync +vsync Interlace

# 1024x768 @ 60Hz (VESA) hsync: 48.4kHz
ModeLine "1024x768"   65.0 1024 1048 1184 1344    768  771  777  806 -hsync -vsync

# 1024x768 @ 70Hz (VESA) hsync: 56.5kHz
ModeLine "1024x768"   75.0 1024 1048 1184 1328    768  771  777  806 -hsync -vsync

# 1024x768 @ 75Hz (VESA) hsync: 60.0kHz
ModeLine "1024x768"   78.8 1024 1040 1136 1312    768  769  772  800 +hsync +vsync

# 1024x768 @ 85Hz (VESA) hsync: 68.7kHz
ModeLine "1024x768"   94.5 1024 1072 1168 1376    768  769  772  808 +hsync +vsync

# 1152x864 @ 75Hz (VESA) hsync: 67.5kHz
ModeLine "1152x864"  108.0 1152 1216 1344 1600    864  865  868  900 +hsync +vsync

# 1280x960 @ 60Hz (VESA) hsync: 60.0kHz
ModeLine "1280x960"  108.0 1280 1376 1488 1800    960  961  964 1000 +hsync +vsync

# 1280x960 @ 85Hz (VESA) hsync: 85.9kHz
ModeLine "1280x960"  148.5 1280 1344 1504 1728    960  961  964 1011 +hsync +vsync

# 1280x1024 @ 60Hz (VESA) hsync: 64.0kHz
ModeLine "1280x1024" 108.0 1280 1328 1440 1688   1024 1025 1028 1066 +hsync +vsync

# 1280x1024 @ 75Hz (VESA) hsync: 80.0kHz
ModeLine "1280x1024" 135.0 1280 1296 1440 1688   1024 1025 1028 1066 +hsync +vsync

# 1280x1024 @ 85Hz (VESA) hsync: 91.1kHz
ModeLine "1280x1024" 157.5 1280 1344 1504 1728   1024 1025 1028 1072 +hsync +vsync

# 1600x1200 @ 60Hz (VESA) hsync: 75.0kHz
ModeLine "1600x1200" 162.0 1600 1664 1856 2160   1200 1201 1204 1250 +hsync +vsync

# 1600x1200 @ 65Hz (VESA) hsync: 81.3kHz
ModeLine "1600x1200" 175.5 1600 1664 1856 2160   1200 1201 1204 1250 +hsync +vsync

# 1600x1200 @ 70Hz (VESA) hsync: 87.5kHz
ModeLine "1600x1200" 189.0 1600 1664 1856 2160   1200 1201 1204 1250 +hsync +vsync

# 1600x1200 @ 75Hz (VESA) hsync: 93.8kHz
ModeLine "1600x1200" 202.5 1600 1664 1856 2160   1200 1201 1204 1250 +hsync +vsync

# 1600x1200 @ 85Hz (VESA) hsync: 106.3kHz
ModeLine "1600x1200" 229.5 1600 1664 1856 2160   1200 1201 1204 1250 +hsync +vsync

# 1792x1344 @ 60Hz (VESA) hsync: 83.6kHz
ModeLine "1792x1344" 204.8 1792 1920 2120 2448   1344 1345 1348 1394 -hsync +vsync

# 1792x1344 @ 75Hz (VESA) hsync: 106.3kHz
ModeLine "1792x1344" 261.0 1792 1888 2104 2456   1344 1345 1348 1417 -hsync +vsync

# 1856x1392 @ 60Hz (VESA) hsync: 86.3kHz
ModeLine "1856x1392" 218.3 1856 1952 2176 2528   1392 1393 1396 1439 -hsync +vsync

# 1856x1392 @ 75Hz (VESA) hsync: 112.5kHz
ModeLine "1856x1392" 288.0 1856 1984 2208 2560   1392 1393 1396 1500 -hsync +vsync

# 1920x1440 @ 60Hz (VESA) hsync: 90.0kHz
ModeLine "1920x1440" 234.0 1920 2048 2256 2600   1440 1441 1444 1500 -hsync +vsync

# 1920x1440 @ 75Hz (VESA) hsync: 112.5kHz
ModeLine "1920x1440" 297.0 1920 2064 2288 2640   1440 1441 1444 1500 -hsync +vsync
EndSection
SECTION
else # $FORMAT == 4
  fetch xserver-xfree86/config/monitor/identifier
  MONITOR_IDENTIFIER="$RET"
  fetch xserver-xfree86/config/monitor/horiz-sync
  MONITOR_HORIZ_SYNC="$RET"
  fetch xserver-xfree86/config/monitor/vert-refresh
  MONITOR_VERT_REFRESH="$RET"
  printf "Section \"Monitor\"\n" > $DEXCONFTMPDIR/Monitor
  printf "\tIdentifier\t\"$MONITOR_IDENTIFIER\"\n">> $DEXCONFTMPDIR/Monitor
  printf "\tHorizSync\t$MONITOR_HORIZ_SYNC\n" >> $DEXCONFTMPDIR/Monitor
  printf "\tVertRefresh\t$MONITOR_VERT_REFRESH\n" >> $DEXCONFTMPDIR/Monitor
  printf "\tOption\t\t\"DPMS\"\n" >> $DEXCONFTMPDIR/Monitor
  printf "EndSection\n" >> $DEXCONFTMPDIR/Monitor
fi

### SCREEN

if [ "$FORMAT" = "3" ]; then
  # mode list may be null
  db_get shared/xfree86v3/config/display/modes
  if [ -n "$RET" ]; then
    # debconf doesn't return the list in the format we need
    DISPLAY_MODES=$( echo $(IFS=", "; set -- $RET; while [ $# -gt 0 ]; do echo \"$1\"; shift; done) )
  fi
  fetch shared/xfree86v3/config/display/default_bpp
  DISPLAY_DEFAULT_BPP="$RET"
  for OLDDRIVER in Accel SVGA; do
    printf "Section \"Screen\"\n" > $DEXCONFTMPDIR/Screen$OLDDRIVER
    printf "\tDriver\t\t\t\"$OLDDRIVER\"\n" >> $DEXCONFTMPDIR/Screen$OLDDRIVER
    printf "\tDevice\t\t\t\"$DEVICE_IDENTIFIER\"\n" >> $DEXCONFTMPDIR/Screen$OLDDRIVER
    printf "\tMonitor\t\t\t\"$MONITOR_IDENTIFIER\"\n" >> $DEXCONFTMPDIR/Screen$OLDDRIVER
    printf "\tDefaultColorDepth\t$DISPLAY_DEFAULT_BPP\n" >> $DEXCONFTMPDIR/Screen$OLDDRIVER
    for BPP in 8 15 16 24 32; do
      printf "\tSubSection \"Display\"\n" >> $DEXCONFTMPDIR/Screen$OLDDRIVER
      printf "\t\tDepth\t\t$BPP\n" >> $DEXCONFTMPDIR/Screen$OLDDRIVER
      if [ -n "$DISPLAY_MODES" ]; then
        printf "\t\tModes\t\t$DISPLAY_MODES\n" >> $DEXCONFTMPDIR/Screen$OLDDRIVER
      fi
      printf "\tEndSubSection\n" >> $DEXCONFTMPDIR/Screen$OLDDRIVER
    done
    printf "EndSection\n" >> $DEXCONFTMPDIR/Screen$OLDDRIVER
  done
  # write out section for FBDev
  printf "Section \"Screen\"\n" > $DEXCONFTMPDIR/ScreenFBDev
  printf "\tDriver\t\t\t\"FBDev\"\n" >> $DEXCONFTMPDIR/ScreenFBDev
  printf "\tMonitor\t\t\t\"$MONITOR_IDENTIFIER\"\n" >> $DEXCONFTMPDIR/ScreenFBDev
  printf "\tDefaultColorDepth\t$DISPLAY_DEFAULT_BPP\n" >> $DEXCONFTMPDIR/ScreenFBDev
  for BPP in 8 15 16 24 32; do
    printf "\tSubSection \"Display\"\n" >> $DEXCONFTMPDIR/ScreenFBDev
    printf "\t\tDepth\t\t$BPP\n" >> $DEXCONFTMPDIR/ScreenFBDev
    printf "\t\tModes\t\t\"default\"\n" >> $DEXCONFTMPDIR/ScreenFBDev
    printf "\tEndSubSection\n" >> $DEXCONFTMPDIR/ScreenFBDev
  done
  printf "EndSection\n" >> $DEXCONFTMPDIR/ScreenFBDev
  # write out section for Mono
  printf "Section \"Screen\"\n" > $DEXCONFTMPDIR/ScreenMono
  printf "\tDriver\t\t\t\"Mono\"\n" >> $DEXCONFTMPDIR/ScreenMono
  printf "\tMonitor\t\t\t\"$MONITOR_IDENTIFIER\"\n" >> $DEXCONFTMPDIR/ScreenMono
  printf "\tSubSection \"Display\"\n" >> $DEXCONFTMPDIR/ScreenMono
  printf "\tEndSubSection\n" >> $DEXCONFTMPDIR/ScreenMono
  printf "EndSection\n" >> $DEXCONFTMPDIR/ScreenMono
  # write out section for VGA2
  printf "Section \"Screen\"\n" > $DEXCONFTMPDIR/ScreenVGA2
  printf "\tDriver\t\t\t\"VGA2\"\n" >> $DEXCONFTMPDIR/ScreenVGA2
  printf "\tMonitor\t\t\t\"$MONITOR_IDENTIFIER\"\n" >> $DEXCONFTMPDIR/ScreenVGA2
  printf "\tSubSection \"Display\"\n" >> $DEXCONFTMPDIR/ScreenVGA2
  printf "\tEndSubSection\n" >> $DEXCONFTMPDIR/ScreenVGA2
  printf "EndSection\n" >> $DEXCONFTMPDIR/ScreenVGA2
  # write out section for VGA16
  printf "Section \"Screen\"\n" > $DEXCONFTMPDIR/ScreenVGA16
  printf "\tDriver\t\t\t\"VGA16\"\n" >> $DEXCONFTMPDIR/ScreenVGA16
  printf "\tMonitor\t\t\t\"$MONITOR_IDENTIFIER\"\n" >> $DEXCONFTMPDIR/ScreenVGA16
  printf "\tSubSection \"Display\"\n" >> $DEXCONFTMPDIR/ScreenVGA16
  printf "\tEndSubSection\n" >> $DEXCONFTMPDIR/ScreenVGA16
  printf "EndSection\n" >> $DEXCONFTMPDIR/ScreenVGA16
else # $FORMAT == 4
  # mode list may be null
  db_get xserver-xfree86/config/display/modes
  if [ -n "$RET" ]; then
    # debconf doesn't return the list in the format we need
    DISPLAY_MODES=$( echo $(IFS=", "; set -- $RET; while [ $# -gt 0 ]; do echo \"$1\"; shift; done) )
  fi
  fetch xserver-xfree86/config/display/default_depth
  DISPLAY_DEFAULT_DEPTH="$RET"
  printf "Section \"Screen\"\n" > $DEXCONFTMPDIR/Screen
  printf "\tIdentifier\t\"Default Screen\"\n" >> $DEXCONFTMPDIR/Screen
  printf "\tDevice\t\t\"$DEVICE_IDENTIFIER\"\n" >> $DEXCONFTMPDIR/Screen
  printf "\tMonitor\t\t\"$MONITOR_IDENTIFIER\"\n" >> $DEXCONFTMPDIR/Screen
  printf "\tDefaultDepth\t$DISPLAY_DEFAULT_DEPTH\n" >> $DEXCONFTMPDIR/Screen
  for DEPTH in 1 4 8 15 16 24; do
    printf "\tSubSection \"Display\"\n" >> $DEXCONFTMPDIR/Screen
    printf "\t\tDepth\t\t$DEPTH\n" >> $DEXCONFTMPDIR/Screen
    if [ -n "$DISPLAY_MODES" ]; then
      printf "\t\tModes\t\t$DISPLAY_MODES\n" >> $DEXCONFTMPDIR/Screen
    fi
    printf "\tEndSubSection\n" >> $DEXCONFTMPDIR/Screen
  done
  printf "EndSection\n" >> $DEXCONFTMPDIR/Screen
fi

### SERVERLAYOUT

if [ "$FORMAT" = "4" ]; then
  cat > $DEXCONFTMPDIR/ServerLayout << SECTION
Section "ServerLayout"
	Identifier	"Default Layout"
	Screen		"Default Screen"
	InputDevice	"Generic Keyboard"
	InputDevice	"Configured Mouse"
SECTION
  if [ "$MOUSE_PORT" != "/dev/input/mice" ]; then
    printf "\tInputDevice\t\"Generic Mouse\"\n" >> $DEXCONFTMPDIR/ServerLayout
  fi
  printf "EndSection\n" >> $DEXCONFTMPDIR/ServerLayout
fi

### DRI

if [ "$FORMAT" = "4" ]; then
  fetch xserver-xfree86/config/write_dri_section
  if [ "$RET" = "true" ]; then
    cat > $DEXCONFTMPDIR/DRI << SECTION
Section "DRI"
	Mode	0666
EndSection
SECTION
  fi
fi

# all done, spit it out

if [ "$FORMAT" = "3" ]; then
  for SECTION in Header Files Module InputDeviceKeyboard InputDeviceMouse \
                 Monitor Device ScreenAccel ScreenFBDev ScreenSVGA ScreenMono \
                 ScreenVGA2 ScreenVGA16; do
    if [ -e $DEXCONFTMPDIR/$SECTION ]; then
      cat $DEXCONFTMPDIR/$SECTION >> $DEXCONFTMPDIR/dexconf-out
      printf "\n" >> $DEXCONFTMPDIR/dexconf-out
    fi
  done
else # $FORMAT == 4
  for SECTION in Header Files Module InputDeviceKeyboard InputDeviceMouse \
                 Device Monitor Screen ServerLayout DRI; do
    if [ -e $DEXCONFTMPDIR/$SECTION ]; then
      cat $DEXCONFTMPDIR/$SECTION >> $DEXCONFTMPDIR/dexconf-out
      printf "\n" >> $DEXCONFTMPDIR/dexconf-out
    fi
  done
fi

printf "### END DEBCONF SECTION\n" >> $DEXCONFTMPDIR/dexconf-out

TMP="$XF86CONFIG".dexconf-tmp

mkdir -p -m 0755 $(dirname "$TMP")
rm -f "$TMP"

if [ -e "$XF86CONFIG" ]; then
  # are we "clobbering" the existing config file?
  if [ "$FORMAT" = "3" ]; then
    db_get shared/xfree86v3/move_existing_nondebconf_config
  else
    db_get xserver-xfree86/move_existing_nondebconf_config
  fi
  if [ "$RET" = "true" ]; then
    # the config script copied the config file to a backup, so we can stomp all
    # over the existing one now
    cp $DEXCONFTMPDIR/dexconf-out "$TMP"
  else
    # update the config file in place; does the file have debconf markers in
    # it?
    if egrep -q '^### BEGIN DEBCONF SECTION' < "$XF86CONFIG" && \
       egrep -q '^### END DEBCONF SECTION' < "$XF86CONFIG"; then
      sed -n '/^### BEGIN DEBCONF SECTION/q;p' < "$XF86CONFIG" > "$TMP"
      cat $DEXCONFTMPDIR/dexconf-out >> "$TMP"
      sed '1,/^### END DEBCONF SECTION/d' < "$XF86CONFIG" >> "$TMP"
    else
      bomb "$PROGNAME: existing $XF86CONFIG has missing or half-open debconf" \
           "region; not writing X server configuration file."
    fi
  fi
else
  cp $DEXCONFTMPDIR/dexconf-out "$TMP"
fi

mv "$TMP" "$XF86CONFIG"

rm -rf $DEXCONFTMPDIR "$TMP"

exit 0

# vim:ai:et:sts=2:sw=2:tw=0:
