#!/bin/sh

# dexconf: Debian X Configurator
#
# This tool is a backend which uses debconf database values.
#
# Author: Branden Robinson

# Copyright 2000--2003 Progeny Linux Systems, Inc.
#
# This is free software; you may redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2,
# or (at your option) any later version.
#
# This is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License with
# the Debian operating system, in /usr/share/common-licenses/GPL;  if
# not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA

# $Id: dexconf 846 2003-12-16 17:26:57Z branden $

set -e

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

# display a usage message
usage () {
  cat << EOF
Usage: $PROGNAME [OPTION ...]
  write an XFree86 X server configuration file based on debconf database values
Options:
  -f FORMAT, --format=FORMAT         write file in XFree86 FORMAT style (3 or 4)
  -h, --help                                 display this usage message and exit
  -o FILE, --output=FILE                        write configuration file to FILE
This help message is intended only as a quick reference.  For a description of
the usage of $PROGNAME, see the $PROGNAME(1) manual page.
EOF
  :;
}

# 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=${0##*/}
SHOWHELP=
EARLYEXIT=

GETOPT_OUTPUT=$(getopt --options f:ho: \
                       --longoptions format:,help,output: \
                       -n "$PROGNAME" -- "$@")

if [ $? -ne 0 ]; then
    bomb "error while getting options; use \"$PROGNAME --help\" for help"
fi

eval set -- "$GETOPT_OUTPUT"

while :; do
    case "$1" in
        -f|--format) FORMAT="$2"; shift ;;
        -h|--help) SHOWHELP=yes EARLYEXIT=yes ;;
        -o|--output) XF86CONFIG="$2"; shift ;;
        --) shift; break ;;
        *) bomb "error while parsing options; use \"$PROGNAME --help\" for help" ;;
    esac
    shift
done

if [ -n "$SHOWHELP" ]; then
    usage
fi

if [ -n "$EARLYEXIT" ]; then
    exit 0
fi

DEXCONFTMPDIR=

trap 'rm -rf "$DEXCONFTMPDIR"; \
      fi; \
      bomb "received signal; aborting."' HUP INT QUIT TERM

# 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|xserver-xfree86-dbg)
    : ${FORMAT:=4}
    : ${XF86CONFIG:=/etc/X11/XF86Config-4}
    ;;
  *)
    bomb "this program does not know how to configure the \"$XSERVERPKG\" X server."
esac

# setup temp dir
TDIR_PARENT="${TMPDIR:-/tmp}"
TDIR="${TMPDIR:-/tmp}/dexconf-tmp-$$"

if [ ! -d "$TDIR_PARENT" ]; then
  bomb "cannot create temporary work directory; \"$TDIR_PARENT\" does not exist or is not a directory."
fi

if [ ! -w "$TDIR_PARENT" ]; then
  bomb "cannot create temporary work directory in \"$TDIR_PARENT\"; directory not writable."
fi

rm -rf "$TDIR"

if mkdir -m 0700 "$TDIR"; then
  DEXCONFTMPDIR="$TDIR"
else
  bomb "creation of temporary work directory \"$TDIR\" failed."
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 device 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 X Window System server configuration file)
#
# This file was 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 file?" in /usr/share/doc/xfree86-common/FAQ.gz.
SECTION
else
  cat > "$DEXCONFTMPDIR/Header" << SECTION
# XF86Config-4 (XFree86 X Window System server configuration file)
#
# This file was 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.)
#
# This file is automatically updated on $XSERVERPKG package upgrades *only*
# if it has not been modified since the last upgrade of the $XSERVERPKG
# package.
#
# If you have edited this file but would like it to be automatically updated
# again, run the following commands as root:
#
#   cp /etc/X11/XF86Config-4 /etc/X11/XF86Config-4.custom
#   md5sum /etc/X11/XF86Config-4 > /var/lib/xfree86/XF86Config-4.md5sum
#   dpkg-reconfigure $XSERVERPKG
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/Type1"
	FontPath	"/usr/lib/X11/fonts/CID"
	FontPath	"/usr/lib/X11/fonts/Speedo"
	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/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/Type1"
	FontPath	"/usr/lib/X11/fonts/CID"
	FontPath	"/usr/lib/X11/fonts/Speedo"
	FontPath	"/usr/lib/X11/fonts/misc"
	FontPath	"/usr/lib/X11/fonts/cyrillic"
	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.
  #
  # Also don't write this stanza if the Configured Mouse is the GPM repeater.
  # We have no way of knowing what device GPM is repeating for, and it might be
  # a USB mouse.
  if [ "$MOUSE_PORT" != "/dev/input/mice" -a "$MOUSE_PORT" != "/dev/gpmdata" ]; 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

# 832x624 @ 75Hz (74.55Hz) (fix if the official/Apple spec is different) hsync: 49.725kHz
ModeLine "832x624" 57.284 832  864  928 1152  624  625  628  667 -Hsync -Vsync

# 1152x768 @ 54.8Hz (Titanium PowerBook) hsync: 44.2kHz
ModeLine "1152x768"   64.995 1152 1178 1314 1472  768  771  777  806 +hsync +vsync

# 1400x1050 @ 60Hz (VESA GTF) hsync: 65.5kHz
ModeLine "1400x1050" 122.0 1400 1488 1640 1880   1050 1052 1064 1082 +hsync +vsync

# 1400x1050 @ 75Hz (VESA GTF) hsync: 82.2kHz
ModeLine "1400x1050" 155.8 1400 1464 1784 1912   1050 1052 1064 1090 +hsync +vsync

# 1600x1024 @ 60Hz (SGI 1600SW) hsync: 64.0kHz
Modeline "1600x1024" 106.910 1600 1620 1640 1670 1024 1027 1030 1067 -hsync -vsync

# 1920x1440 @ 85Hz (VESA GTF) hsync: 128.5kHz
Modeline "1920x1440" 341.35  1920 2072 2288 2656  1440 1441 1444 1512 -hsync +vsync

# 2048x1536 @ 60Hz (VESA GTF) hsync: 95.3kHz
Modeline "2048x1536" 266.95  2048 2200 2424 2800  1536 1537 1540 1589 -hsync +vsync

# 2048x1536 @ 75Hz (VESA GTF) hsync: 120.2kHz
Modeline "2048x1536" 340.48  2048 2216 2440 2832  1536 1537 1540 1603 -hsync +vsync

# 2048x1536 @ 85Hz (VESA GTF) hsync: 137.0kHz
Modeline "2048x1536" 388.04  2048 2216 2440 2832  1536 1537 1540 1612 -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" -a "$MOUSE_PORT" != "/dev/gpmdata" ]; 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

# XXX: when xfree86v3 has migrated to the new configuration style and declares
# a conflict with xserver-common (<< 4.2.1-10); the following if block can die,
# leaving only the simple part in its else clause to be executed
# unconditionally
if [ "$FORMAT" = "3" ]; then
  printf "### END DEBCONF SECTION\n" >> "$DEXCONFTMPDIR/dexconf-out"
  TMP="$DEXCONFTMPDIR/${XF86CONFIG##*/}"
  if [ -e "$XF86CONFIG" ]; then
    # are we "clobbering" the existing config file?
    db_get shared/xfree86v3/move_existing_nondebconf_config
    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 "existing \"$XF86CONFIG\" file has missing or half-open debconf " \
             "region; not writing X server configuration file."
      fi
    fi
  else
    cp "$DEXCONFTMPDIR/dexconf-out" "$TMP"
  fi
  if ! mv "$TMP" "$XF86CONFIG"; then
    bomb "unable to write to \"$XF86CONFIG\"."
  fi
else
  if ! mv "$DEXCONFTMPDIR/dexconf-out" "$XF86CONFIG"; then
    bomb "unable to write to \"$XF86CONFIG\"."
  fi
fi

rm -rf "$DEXCONFTMPDIR"

exit 0

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