#!/bin/ash

# generates the contents of an XF86Config file for version 4.x on standard
# output.

# use like so: build_x4_cfg > /tmp/XF86Config

# this script is based on "build_x4_cfg" from LTSP (http://www.LTSP.org)
# 
# it has been re-written as part of the lessdisks project
# http://lessdisks.sourceforge.net

# bash requires "echo -e", while ash and dash do not.
echo="echo"

X4_DRIVER=${1:-"vesa"}

function_file=${2:-"/usr/share/lessdisks/ltsp_functions"}

# get the functions used in generating the X config.

. $function_file

test -n "$3" && . $3

DEFAULT_SERVER=${SERVER:-"xapp"}

X_MOUSE_BAUD=${X_MOUSE_BAUD:-"1200"}

X_MOUSE_EMULATE3BTN=${X_MOUSE_EMULATE3BTN:-"N"}

if [ "${X_MOUSE_EMULATE3BTN}" = "Y" ]; then
    EMULATE_3_BUTTONS="on"
fi

USE_TOUCH=${USE_TOUCH:-"N"}

USE_XFS=${USE_XFS:-"N"}

if [ "${USE_XFS}" = "Y" ]; then
    XFS_SERVER=${XFS_SERVER:-${DEFAULT_SERVER}}
    # TODO only add if not already present.
    font_paths="tcp/${XFS_SERVER}:7100 $font_paths"
fi
 
x_modules="${x4_modules:-"extmod"}"

if [ "${USE_XFS}" = "N" ]; then
  # TODO only add modules if not already present.
  x_modules="$x_modules freetype type1"
fi

if [ "${USE_TOUCH}" = "Y" ]; then
  get_x4_touch_device
fi

X_HORZSYNC=${X_HORZSYNC:-"31-62"}
X_VERTREFRESH=${X_VERTREFRESH:-"55-90"}

if [ -z "${X4_BUSID}" ]; then
  BUSID=""
else
  BUSID="BusID     \"${X4_BUSID}\""
fi

if [ -z "${X_VIDEORAM}" ]; then
  XVIDRAM=""
else
  XVIDRAM="VideoRam ${X_VIDEORAM}"
fi

x_modes=${x_modes:-"1024x768 800x600 640x480"}
for mode in $x_modes; do
  all_modes="$all_modes \"$mode\""
done

get_x4_server_layout

get_files

get_module

get_x4_keyboard

get_x4_mouse

get_monitor

get_x4_videocard

get_x4_screen

if [ -n "$X4_DRI" ]; then
  get_x4_dri
fi
