#!/bin/sh
# build_x4_cfg
#
# This script will generate the XF86Config-4 file for a diskless workstation, as
# part of the Linux Terminal Server Project (http://www.LTSP.org)
# 

X4_DRIVER=$1

. /etc/ltsp_functions

################################################################################
#
# Get the IP address of the default server.  This is used for XDM_SERVER,
# TELNET_HOST and SYSLOG_HOST if any of them are not set explicitly.
# Default to '192.168.0.254' if it is NOT set in the config file.
#

DEFAULT_SERVER=${SERVER:-"192.168.0.254"}

################################################################################
#
# Build the XF86Config file
#
echo "Building the /tmp/XF86Config-4 file" >/dev/tty

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

X_DPMS=${X_DPMS:-"N"}


echo "#X_MOUSE_DEVICE=$X_MOUSE_DEVICE"
# If no mouse (no lts.conf), or PS/2 (default lts.conf), configure extra mice
[ -z "${X_MOUSE_DEVICE}" ] && EXTRAMICE="1"
[ "${X_MOUSE_DEVICE}" = "/dev/psaux" ] && EXTRAMICE="1"
echo "#Extra Mice = $EXTRAMICE"

#-----------------------------------------------------------------------
#
# Setup server layout
#
cat <<-EOF
Section "ServerFlags"
        AllowMouseOpenFail
EndSection
Section "ServerLayout"
        Identifier "XFree86 Configured"
        Screen      0   "Screen0" 0 0
        InputDevice     "Keyboard0" "CoreKeyboard"
        InputDevice     "Mouse0" "CorePointer"
EOF

if [ "${EXTRAMICE}" = "1" ];then
if [ -e /dev/ttyS0 ];then
cat <<-EOF
        InputDevice     "ExtraSerialMouse0" "SendCoreEvents"
EOF
fi
if [ -e /dev/ttyS1 ];then
cat <<-EOF
        InputDevice     "ExtraSerialMouse1" "SendCoreEvents"
EOF
fi
if [ -e /dev/input/mouse0 ];then
cat <<-EOF
        InputDevice     "ExtraUSBMouse" "SendCoreEvents"
EOF
fi
fi

if [ -e /etc/vncpasswd ]; then
cat <<-EOF
	InputDevice "vncMouse"    "ExtraPointer"
	InputDevice "vncKeyboard" "ExtraKeyboard"
EOF
fi

#-----------------------------------------------------------------------
#
# See if touchscreen is set up
#
USE_TOUCH=${USE_TOUCH:-"N"}
if [ "${USE_TOUCH}" = "Y" ]; then
    cat <<-EOF
        InputDevice     "Elographics" "CorePointer"
EOF
fi

# See if DPMS is set up
if [ "${X_DPMS}" = "Y" ]; then
cat <<-EOF
        Option          "StandbyTime" "${X_DPMS_STANDBYTIME:-"0"}"  # DPMS "standby" state
        Option          "SuspendTime" "${X_DPMS_SUSPENDTIME:-"0"}"  # DPMS "suspend" state
        Option          "OffTime" "${X_DPMS_OFFTIME:-"0"}"  # DPMS "off" state
EOF
fi


echo EndSection

#-----------------------------------------------------------------------
#
# Setup the FontPath info
#
USE_XFS=${USE_XFS:-"N"}

cat <<-EOF
Section "Files"
EOF

if [ "${USE_XFS}" = "Y" ]; then
    XFS_SERVER=${XFS_SERVER:-${DEFAULT_SERVER}}

# MODIF : SUPPORT FOR LOCAL XFS SERVER
  if [ "${XFS_SERVER}" = "0.0.0.0" ]; then
cat <<-EOF
        FontPath   "unix/:-1"
EOF
  else
cat <<-EOF
        FontPath   "tcp/${XFS_SERVER}:7100"
EOF
  fi
# END MODIF

fi
cat <<-EOF
        RgbPath    "/usr/X11R6/lib/X11/rgb"
        FontPath   "/usr/X11R6/lib/X11/fonts/misc"
        FontPath   "/usr/X11R6/lib/X11/fonts/75dpi"
        FontPath   "/usr/X11R6/lib/X11/fonts/drakfont/Type1"
        FontPath   "/usr/X11R6/lib/X11/fonts/drakfont/ttf"
        FontPath   "/usr/X11R6/lib/X11/fonts/100dpi"
        FontPath   "/usr/X11R6/lib/X11/fonts/Type1"
        FontPath   "/usr/X11R6/lib/X11/fonts/TTF"
        FontPath   "/usr/X11R6/lib/X11/fonts/Speedo"
        FontPath   "/usr/X11R6/lib/X11/fonts/mdk"
        FontPath   "/usr/X11R6/lib/X11/fonts/75dpi"
        FontPath   "/usr/X11R6/lib/X11/fonts/fluxbox-artwiz-fonts"
        FontPath   "/usr/share/fonts/default/Type1"
        FontPath   "/usr/share/fonts/ttf/decoratives"
        FontPath   "/usr/share/fonts/ttf/western"
        FontPath   "/usr/share/fonts/ttf/latex"
        FontPath   "/usr/share/fonts/default/Type1/adobestd35"
        FontPath   "/usr/share/fonts/webcore"
EndSection
EOF

#-----------------------------------------------------------------------
#
# Setup module info
#
cat <<-EOF

Section "Module"
        Load   "extmod"
EOF
#
# Load any optional modules for the X server
#
for i in 01 02 03 04 05 06 07 08 09 10; do
    MODVAR=X4_MODULE_${i}
    MODULE=${!MODVAR}
    if [ "${MODULE}" ]; then
        echo "        Load   \"${MODULE}\" "
    fi
done

if [ -e /etc/vncpasswd ]; then
cat <<-EOF
        Load   "vnc"
EOF
fi

#
# If the xfs is not being used the following two modules are required
#
if [ "${USE_XFS}" = "N" ]; then
    echo "        Load   \"freetype\""
    echo "        Load   \"type1\""
fi
echo "EndSection"

#-----------------------------------------------------------------------
#
# Setup Keyboard and Mouse info
#

cat <<-EOF

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

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

if [ "${EXTRAMICE}" = "1" ];then
if [ -e /dev/ttyS0 ];then
cat <<-EOF
Section "InputDevice"
       Identifier  "ExtraSerialMouse0"
       Driver      "mouse"
       Option      "Device"          "/dev/ttyS0"
#       Option      "Protocol"        "Auto"
       Option      "Protocol"        "Microsoft"
       Option      "Emulate3Buttons" 
EndSection
EOF
fi
if [ -e /dev/ttyS1 ];then
cat <<-EOF
Section "InputDevice"
       Identifier  "ExtraSerialMouse1"
       Driver      "mouse"
       Option      "Device"          "/dev/ttyS1"
#       Option      "Protocol"        "Auto"
       Option      "Protocol"        "Microsoft"
       Option      "Emulate3Buttons"
EndSection
EOF
fi
if [ -e /dev/input/mouse0 ];then
cat <<-EOF
Section "InputDevice"
       Identifier  "ExtraUSBMouse"
       Driver      "mouse"
       Option      "Device"          "/dev/input/mice"
       Option      "Protocol"        "PS/2"
       Option      "Emulate3Buttons" 
EndSection
EOF
fi
fi

if [ -e /etc/vncpasswd ]; then
cat <<-EOF 
Section "InputDevice"
       # vncKeyboard: keyboard actions from vnc
       Identifier "vncKeyboard"
       Driver "rfbkeyb"
EndSection

Section "InputDevice"
       # vncMouse: mouse actions from vnc
       Identifier "vncMouse"
       Driver "rfbmouse"
EndSection
EOF
fi

#-----------------------------------------------------------------------
#
# Setup the Touch Screen operating parameters
#
if [ "${USE_TOUCH}" = "Y" ]; then
    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
fi

#-----------------------------------------------------------------------
#
# Setup the Monitor info
#

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

#
# Extract Modelines and Video modes from the custom modes.
#
MODE[0]=`echo ${X_MODE_0} | cut -f1 -d" "`
LINE[0]="`echo ${X_MODE_0} | cut -f2- -d" " -s`"
MODE[1]=`echo ${X_MODE_1} | cut -f1 -d" "`
LINE[1]="`echo ${X_MODE_1} | cut -f2- -d" " -s`"
MODE[2]=`echo ${X_MODE_2} | cut -f1 -d" "`
LINE[2]="`echo ${X_MODE_2} | cut -f2- -d" " -s`"

cat <<-EOF

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

if [ -n "${LINE[0]}" ]; then
    echo "        Modeline \"${MODE[0]}\"  ${LINE[0]}"
fi
if [ -n "${LINE[1]}" ]; then
    echo "        Modeline \"${MODE[1]}\"  ${LINE[1]}"
fi
if [ -n "${LINE[2]}" ]; then
    echo "        Modeline \"${MODE[2]}\"  ${LINE[2]}"
fi

# Set up DPMS
if [ "${X_DPMS}" = "Y" ]; then
cat <<-EOF
        Option          "DPMS"  # enable DPMS
EOF
fi

cat <<-EOF
EndSection

Section "Device"
        Identifier "My Video Card"
        Driver     "${X4_DRIVER}"
        ${BUSID}
        ${XVIDRAM}
EOF

    for i in 01 02 03 04 05 06 07 08 09 10; do
        X_DEV_VAR=X_DEVICE_OPTION_${i}
        X_DEVICE_OPTION=${!X_DEV_VAR}
        if [ "${X_DEVICE_OPTION}" ]; then
            echo "        Option   \"${X_DEVICE_OPTION}\""
        fi
    done


if [ -s /etc/vncpasswd ]; then
cat <<-EOF
        Option "rfbauth"     "/etc/vncpasswd"
EOF
fi

if [ -e /etc/vncpasswd ]; then
cat <<-EOF
	Option "rfbport"     "5901"
	Option "alwaysshared"
	Option "dontdisconnect"
	Option "usevnc"
EOF
fi

cat <<-EOF
EndSection

EOF

#-----------------------------------------------------------------------
#
# Setup the Screen info
#

if [ -z "${MODE[0]}${MODE[1]}${MODE[2]}" ]; then
    MODE[0]="1024x768"
    MODE[1]="800x600"
    MODE[2]="640x480"
fi

if [ -n "${MODE[0]}" ]; then
    MODE[0]=\"${MODE[0]}\"
fi

if [ -n "${MODE[1]}" ]; then
    MODE[1]=\"${MODE[1]}\"
fi

if [ -n "${MODE[2]}" ]; then
    MODE[2]=\"${MODE[2]}\"
fi

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}
                Modes ${MODE[*]}
        EndSubSection
EndSection

EOF

#-----------------------------------------------------------------------
#
# Any dri options that may be required
#
cat <<-EOF
Section "DRI"
        ${X4_DRI}
EndSection
EOF
