# functions used by the LTSP-based X configuration scripts for lessdisks

# XkbVariant options added by wolfgang schweer

# load various default configuration files...
test -r /etc/lessdisks/ltsp/x_ltsp.conf && . /etc/lessdisks/ltsp/x_ltsp.conf
test -r /etc/lessdisks/server.config && . /etc/lessdisks/server.config
test -r /etc/lessdisks/x.config && . /etc/lessdisks/x.config
test -z "$hostname" && hostname=$(hostname)
if [ -n "$hostname" ]; then
  test -r /etc/lessdisks/terminals/$hostname && . /etc/lessdisks/terminals/$hostname 
fi

get_cfg(){
    #
    # Usage:  get_cfg PARM [DEFAULT]
    #
    #VALUE=`/bin/getltscfg $1`
    VALUE=$(eval echo "$""$1")
    if [ "$#" -gt 1 ] && [ -z "${VALUE}" ]; then
        echo $2
    else
        echo ${VALUE}
    fi
}

get_x4_server_layout() {
cat <<-EOF
Section "ServerLayout"
        Identifier "XFree86 Configured"
        Screen      0   "Screen0" 0 0
        InputDevice     "Keyboard0" "CoreKeyboard"
        InputDevice     "Mouse0" "CorePointer"
EOF

#-----------------------------------------------------------------------
#
# See if touchscreen is set up
#
if [ "${USE_TOUCH}" = "Y" ]; then
    echo "\tInputDevice\t\"Elographics\" \"CorePointer\""
fi
echo EndSection
echo
}

get_files() {
  $echo "Section \"Files\""
  $echo "\tRgbPath\t\t\"/usr/X11R6/lib/X11/rgb\""
  # add a line for each path defined in font_paths
  for font in $font_paths; do
    $echo "\tFontPath\t\"$font\""
  done
  echo EndSection
  echo
}

get_module() {
  $echo "Section \"Module\""
  for module in $x_modules; do
    $echo "\tLoad\t\"$module\""
  done
  echo EndSection
}

get_x4_keyboard() {
cat <<-EOF

Section "InputDevice"
       Identifier  "Keyboard0"
       Driver      "keyboard"
       Option      "XkbLayout" "${XKBLAYOUT:-"us"}"
       Option	   "XkbVariant" "${XkbVariant}"
EndSection

EOF
}

get_x4_mouse() {
cat <<-EOF

Section "InputDevice"
       Identifier  "Mouse0"
       Driver      "mouse"
       Option      "Device"          "${X_MOUSE_DEVICE:-"/dev/psaux"}"
       Option      "Protocol"        "${X_MOUSE_PROTOCOL:-"PS/2"}"
       Option      "BaudRate"        "${X_MOUSE_BAUD}"
       Option      "Resolution"      "${X_MOUSE_RESOLUTION}"
       Option      "Emulate3Buttons" "${EMULATE_3_BUTTONS:-"off"}"
       Option      "ZAxisMapping"     "4 5"
       Option      "Buttons"         "${X_MOUSE_BUTTONS:-"3"}"
EndSection

EOF
}

get_x4_touchdevice() {
cat <<-EOF

Section "InputDevice"
       Identifier "Elographics"
       Driver      "elographics"
       Option      "Device"	      "${X_TOUCH_DEVICE:-/dev/ttyS0}"
       Option      "DeviceName"       "Elo"
       Option      "MinimumXPosition" "${X_TOUCH_MINX:-433}"
       Option      "MaximumXPosition" "${X_TOUCH_MAXX:-3588}"
       Option      "MinimumYPosition" "${X_TOUCH_MINY:-569}"
       Option      "MaximumYPosition" "${X_TOUCH_MAXY:-3526}"
       Option      "UntouchDelay"     "${X_TOUCH_UNDELAY:-10}"
       Option      "ReportDelay"      " ${X_TOUCH_RPTDELAY:-10}"
       Option      "AlwaysCore"
EndSection

EOF
}

get_modelines() {
  # TODO support modelines
  for line in $@ ; do
    $echo "\tModeline\t$line"
  done
}

get_monitor() {
  cat <<-EOF

Section "Monitor"
        Identifier      "My Monitor"
        VendorName      "Unknown"
        ModelName       "Unknown"
        HorizSync       ${X_HORZSYNC}
        VertRefresh     ${X_VERTREFRESH}
EOF

  get_modelines $modelines

  echo EndSection
}

get_x4_videocard() {
cat <<-EOF
Section "Device"
        Identifier "My Video Card"
        Driver     "${X4_DRIVER}"
        ${BUSID}
        ${XVIDRAM}
EOF

for option in $x_device_options ; do
  $echo "\tOption\t\"$option\""
done

echo EndSection
}

get_x4_screen() {
cat <<-EOF

Section "Screen"
        Identifier "Screen0" 
        Device "My Video Card" 
        Monitor "My Monitor"
        DefaultDepth  ${X_COLOR_DEPTH:-16}
        Subsection "Display"
                Depth ${X_COLOR_DEPTH:-16}
EOF
if [ -n "$all_modes" ]; then
  $echo "\t\tModes $all_modes"
fi
$echo "\tEndSubSection"
echo EndSection
}

get_x4_dri() {
cat <<-EOF
Section "DRI"
        ${X4_DRI}
EndSection

EOF
}
