#!/bin/sh

# failsafeDexconf: Debian X server configuration file writer for failsafe X mode
#
# This tool is a backend which uses debconf database values along with
# some overrides for running X in vesa (or vga) modes.  It outputs an
# XFree86 X server configuration file based on the information in the
# database.
#
# This script is derived from the dexconf program, written by
# Branden Robinson.

# Copyright 2007 Canonical, Ltd.
# Copyright 2000--2004 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

DEVICE_DRIVER=${1:-"vesa"}
OUTFILE=${2:-"/etc/X11/xorg.conf.failsafe"}
SERVER="xorg"

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

# the error-out function
bomb () {
  echo "$PROGNAME: error: $*" | fold -s -w "${COLUMNS:-80}" 1>&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."
    ERRMSG="$ERRMSG  Reconfigure the X server with \"dpkg-reconfigure"
    ERRMSG="$ERRMSG xserver-xorg\" to correct this problem."
    bomb "$ERRMSG"
  fi
}

if [ -e ${OUTFILE} ]; then
    if [ ! -w ${OUTFILE} ]; then
        bomb "Cannot write to existing ${OUTFILE}"
    else
        mv ${OUTFILE} ${OUTFILE}.bak
        cat /dev/null > ${OUTFILE}
    fi
fi

if which laptop-detect >/dev/null 2>&1; then
  if laptop-detect > /dev/null ; then
    LAPTOP=true
  fi
fi

### HEADER

# Because debconf hijacks standard output and its confmodule uses file
# descriptor 3 for its own purposes, we will write our output to file descriptor
# 4 instead of standard output.

cat <<SECTION >> ${OUTFILE}
# xorg.conf (X.Org X Window System server configuration file)
#
# This file was generated by failsafeDexconf, using
# values from the debconf database and some overrides to use vesa mode.
#
# You should use dexconf or another such tool for creating a "real" xorg.conf
# For example:
#   sudo dpkg-reconfigure -phigh xserver-xorg
SECTION

### KEYBOARD / INPUTDEVICE

fetch xserver-$SERVER/config/inputdevice/keyboard/rules
XKB_RULES="$RET"
fetch xserver-$SERVER/config/inputdevice/keyboard/model
XKB_MODEL="$RET"
fetch xserver-$SERVER/config/inputdevice/keyboard/layout
XKB_LAYOUT="$RET"
# XkbVariant and XkbOptions are permitted to be null.
db_get xserver-$SERVER/config/inputdevice/keyboard/variant
XKB_VARIANT="$RET"
db_get xserver-$SERVER/config/inputdevice/keyboard/options
XKB_OPTIONS="$RET"

cat <<SECTION >> ${OUTFILE}
Section "InputDevice"
	Identifier	"Generic Keyboard"
	Driver		"kbd"
	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" >> ${OUTFILE}
fi
if [ -n "$XKB_OPTIONS" ]; then
  printf "\tOption\t\t\"XkbOptions\"\t\"$XKB_OPTIONS\"\n" >> ${OUTFILE}
fi
printf "EndSection\n\n" >> ${OUTFILE}

### MOUSE / INPUTDEVICE

DO_EMULATE3BUTTONS=

fetch xserver-$SERVER/config/inputdevice/mouse/emulate3buttons
if [ "$RET" = "true" ]; then
  DO_EMULATE3BUTTONS=true
fi

MOUSE_DRIVER=mouse
IS_VIRT=
if which vmmouse_detect >/dev/null 2>&1; then
  if vmmouse_detect > /dev/null ; then
    MOUSE_DRIVER=vmmouse
    IS_VIRT=true
  fi
fi

cat <<SECTION >> ${OUTFILE}
Section "InputDevice"
	Identifier	"Configured Mouse"
	Driver		"$MOUSE_DRIVER"
SECTION
if [ -n "$DO_EMULATE3BUTTONS" ]; then
  printf "\tOption\t\t\"Emulate3Buttons\"\t\"true\"\n" >> ${OUTFILE}
fi
if [ -n "$IS_VIRT" ]; then
  printf "\tOption\t\t\"Device\"\t\"/dev/input/mice\"\n" >> ${OUTFILE}
fi
printf "EndSection\n\n" >> ${OUTFILE}

if [ -n "$LAPTOP" ]; then
  cat <<SECTION >> ${OUTFILE}

Section "InputDevice"
	Identifier	"Synaptics Touchpad"
	Driver		"synaptics"
	Option		"SendCoreEvents"	"true"
	Option		"Device"		"/dev/psaux"
	Option		"Protocol"		"auto-dev"
	Option		"HorizEdgeScroll"	"0"
EndSection
SECTION
fi

### DEVICE

db_get xserver-$SERVER/config/device/bus_id
DEVICE_BUSID="$RET"
cat <<SECTION >> ${OUTFILE}
Section "Device"
	Identifier	"Configured Video Device"
	Driver		"$DEVICE_DRIVER"
SECTION
printf "EndSection\n\n" >> ${OUTFILE}

### MONITOR

cat <<SECTION >> ${OUTFILE}
Section "Monitor"
	Identifier	"Configured Monitor"
EndSection

SECTION

### SCREEN

DISPLAY_MODES="800x600"

cat <<SECTION >> ${OUTFILE}
Section "Screen"
	Identifier	"Default Screen"
	Device		"Configured Video Device"
	Monitor		"Configured Monitor"
	SubSection "Display"
		Modes   "$DISPLAY_MODES"
	EndSubSection
EndSection

SECTION

### SERVERLAYOUT

cat <<SECTION >> ${OUTFILE}
Section "ServerLayout"
	Identifier	"Default Layout"
	Screen		"Default Screen"
SECTION
if [ -n "$LAPTOP" ]; then
  printf "\tInputDevice\t\"Synaptics Touchpad\"\n" >> ${OUTFILE}
fi
printf "EndSection\n" >> ${OUTFILE}

# Tell debconf to stop listening to us.
db_stop

exit 0

# vim:set ai et sts=2 sw=2 tw=80:
