:
#
# build_x3_cfg
#
# This script will generate the XF86Config file for a diskless workstation, as
# part of the Linux Terminal Server Project (http://www.LTSP.org)
#
# Copyright: James A. McQuillan <jam@Ltsp.org>  December 23, 2003
#

. /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 XF86Config file" >/dev/tty

X_MOUSE_EMULATE3BTN=${X_MOUSE_EMULATE3BTN:-"N"}

if [ "${X_MOUSE_EMULATE3BTN}" = "Y" ]; then
    EMULATE3BUTTONS="Emulate3Buttons"
else
    EMULATE3BUTTONS=""
fi

#-----------------------------------------------------------------------
#
# Setup the FontPath info
#

USE_XFS=${USE_XFS:-"N"}

if [ "${USE_XFS}" = "Y" ]; then
    XFS_SERVER=${XFS_SERVER:-${DEFAULT_SERVER}}
    cat <<-EOF
        Section "Files"
           RgbPath    "/usr/X11R6/lib/X11/rgb"
           FontPath   "tcp/${XFS_SERVER}:7100"
        EndSection
EOF
else
    cat <<-EOF
        Section "Files"
           RgbPath    "/usr/X11R6/lib/X11/rgb"
           FontPath   "/usr/X11R6/lib/X11/fonts/misc/"
           FontPath   "/usr/X11R6/lib/X11/fonts/Type1/"
           FontPath   "/usr/X11R6/lib/X11/fonts/75dpi/"
           FontPath   "/usr/X11R6/lib/X11/fonts/100dpi/"
        EndSection
EOF
fi

#-----------------------------------------------------------------------
#
# Setup Touch Screen info
#

USE_TOUCH=${USE_TOUCH:-"N"}
if [ "${USE_TOUCH}" = "Y" ]; then
    cat <<-EOF
        Section "Module"
            Load "xf86Elo.so"
        EndSection
EOF
fi

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

XkbTypes=${XKBTYPES:-"default"}
XkbCompat=${XKBCOMPAT:-"default"}
XkbSymbols=${XKBSYMBOLS:-"us(pc101)"}
XkbModel=${XKBMODEL:-"pc101"}
XkbLayout=${XKBLAYOUT:-"us"}

cat <<-EOF
    Section "ServerFlags"
    EndSection

    Section "Keyboard"
        Protocol        "Standard"
        AutoRepeat      500 5
        LeftAlt         Meta
        RightAlt        Meta
        ScrollLock      Compose
        RightCtl        Control
        XkbKeycodes     "xfree86"
        XkbTypes        "${XkbTypes}"
        XkbCompat       "${XkbCompat}"
        XkbSymbols      "${XkbSymbols}"
        XkbGeometry     "pc"
        XkbRules        "xfree86"
        XkbModel        "${XkbModel}"
        XkbLayout       "${XkbLayout}"
    EndSection

    Section "Pointer"
        Protocol        "${X_MOUSE_PROTOCOL:-"PS/2"}"
        Device          "${X_MOUSE_DEVICE:-"/dev/psaux"}"
        Resolution      ${X_MOUSE_RESOLUTION:-400}
        Buttons         ${X_MOUSE_BUTTONS:-3}
        BaudRate        ${X_MOUSE_BAUD:-1200}
        AlwaysCore
        ZAxisMapping    4 5
        ${EMULATE3BUTTONS}
    EndSection
EOF

#-----------------------------------------------------------------------
#
# Setup the Touch Screen operating parameters
#
if [ "${USE_TOUCH}" = "Y" ]; then
    cat <<-EOF
        Section "Xinput"
            SubSection "Elographics"
                Port              "${X_TOUCH_DEVICE:-/dev/ttyS0}"
                DeviceName        "Elo"
                MinimumXPosition  ${X_TOUCH_MINX:-433}
                MaximumXPosition  ${X_TOUCH_MAXX:-3588}
                MinimumYPosition  ${X_TOUCH_MINY:-569}
                MaximumYPosition  ${X_TOUCH_MAXY:-3526}
                UntouchDelay      ${X_TOUCH_UNDELAY:-10}
                ReportDelay       ${X_TOUCH_RPTDELAY:-10}
                AlwaysCore
            EndSubSection
        EndSection
EOF
fi

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

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

#
# 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`"

#
# Remove any trailing spaces from the modelines, thus if they arent defined the 
# length of X_MODE_?_LINE is zero, allowing for logical operations.
#

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

#
# Check if a modeline is defined from X_MODE1,2,3 , if not
# then write a set of default ones. The X server will delete 
# the ones it cant use for that monitor/video card
#
if [ -z "${LINE[0]}${LINE[1]}${LINE[2]}" ]; then
    cat <<-EOF
        Modeline "640x400"    25.175 640  664  760  800  400  409  411  450
        Modeline "640x400"    31.5   640  672 736   832  400  401  404  445  -HSync +VSync
        Modeline "640x480"    45.8   640  672  768  864  480  488  494  530  -HSync -VSync
        Modeline "640x480"    25.175 640  664  760  800  480  491  493  525
        Modeline "640x480"    31.5   640  680  720  864  480  488  491  521
        Modeline "640x480"    31.5   640  656  720  840  480  481  484  500  -HSync -VSync
        Modeline "640x480"    36     640  696  752  832  480  481  484  509  -HSync -VSync
        Modeline "800x600"    69.65  800  864  928 1088  600  604  610  640  -HSync -VSync
        Modeline "800x600"    36     800  824  896 1024  600  601  603  625
        Modeline "800x600"    40     800  840  968 1056  600  601  605  628  +hsync +vsync
        Modeline "800x600"    50     800  856  976 1040  600  637  643  666  +hsync +vsync
        Modeline "800x600"    60.75  800  864  928 1088  600  616  621  657  -HSync -VSync
        Modeline "1024x480"   65     1024 1032 1176 1344 480  491  493  525  -HSync -VSync
        Modeline "1024x768"   115.5  1024 1056 1248 1440 768  771  781  802  -HSync -VSync
        Modeline "1024x768"   65     1024 1032 1176 1344 768  771  777  806  -hsync -vsync
        Modeline "1024x768"   75     1024 1048 1184 1328 768  771  777  806  -hsync -vsync
        Modeline "1024x768"   85     1024 1032 1152 1360 768  784  787  823
        Modeline "1024x768"   98.9   1024 1056 1216 1408 768  782  788  822  -HSync -VSync
        Modeline "1152x864"   137.65 1152 1184 1312 1536 864  866  885  902  -HSync -VSync
        Modeline "1152x864"   89.9   1152 1216 1472 1680 864  868  876  892  -HSync -VSync
        Modeline "1152x864"   92     1152 1208 1368 1474 864  865  875  895
        Modeline "1152x864"   110    1152 1240 1324 1552 864  864  876  908
        Modeline "1152x864"   135    1152 1464 1592 1776 864  864  876  908
        Modeline "1280x960"   108    1280 1376 1488 1800 960  961  964  1000 +HSync +VSync
        Modeline "1280x960"   148.5  1280 1344 1504 1728 960  961  964  1011 +HSync +VSync
        Modeline "1280x1024"  181.75 1280 1312 1440 1696 1024 1031 1046 1072 -HSync -VSync
        Modeline "1280x1024"  110    1280 1328 1512 1712 1024 1025 1028 1054
        Modeline "1280x1024"  126.5  1280 1312 1472 1696 1024 1032 1040 1068 -HSync -VSync
        Modeline "1280x1024"  135    1280 1312 1456 1712 1024 1027 1030 1064
        Modeline "1280x1024"  135    1280 1312 1416 1664 1024 1027 1030 1064
        Modeline "1280x1024"  157.5  1280 1344 1504 1728 1024 1025 1028 1072 +HSync +VSync
        Modeline "1400x1050"  129    1400 1464 1656 1960 1050 1051 1054 1100 +HSync +VSync
        Modeline "1400x1050"  151    1400 1464 1656 1960 1050 1051 1054 1100 +HSync +VSync
        Modeline "1400x1050"  162    1400 1464 1656 1960 1050 1051 1054 1100 +HSync +VSync
        Modeline "1400x1050"  184    1400 1464 1656 1960 1050 1051 1054 1100 +HSync +VSync
        Modeline "1600x1200"  162    1600 1664 1856 2160 1200 1201 1204 1250 +HSync +VSync
        Modeline "1600x1200"  189    1600 1664 1856 2160 1200 1201 1204 1250 -HSync -VSync
        Modeline "1600x1200"  202.5  1600 1664 1856 2160 1200 1201 1204 1250 +HSync +VSync
        Modeline "1600x1200"  220    1600 1616 1808 2080 1200 1204 1207 1244 +HSync +VSync
        Modeline "1800X1440"  230    1800 1896 2088 2392 1440 1441 1444 1490 +HSync +VSync
        Modeline "1800X1440"  250    1800 1896 2088 2392 1440 1441 1444 1490 +HSync +VSync
EOF
fi

#
# CASE 1:
# If a mode is not specified then the default ones should be used.
#
if [ -z "${MODE[0]}${MODE[1]}${MODE[2]}" ]; then
    MODE[0]="1024x768"
    MODE[1]="800x600"
    MODE[2]="640x480"
fi    

#
# CASE 2:
# If modelines are defined then use them instead of the defaults plus
# add some ""'s around the modes
#
if [  -n "${LINE[0]}" ]; then
    echo "        Modeline \"${MODE[0]}\"   ${LINE[0]}"
    MODE[0]="\"${MODE[0]}\""
fi
if [  -n "${LINE[1]}" ]; then
    echo "        Modeline \"${MODE[1]}\"   ${LINE[1]}"
    MODE[1]="\"${MODE[1]}\""
fi
if [  -n "${LINE[2]}" ]; then
    echo "        Modeline \"${MODE[2]}\"   ${LINE[2]}"
    MODE[2]="\"${MODE[2]}\""
fi

#
# CASE 3:
# If mode is defined without modelines then we want to use these
# default resolutions.  Plus add some "'s around them
#
if [ -n "${MODE[0]}" ] && [ -z "${LINE[0]}" ];then
     MODE[0]="\"${MODE[0]}\""
fi
if [ -n "${MODE[1]}" ] && [ -z "${LINE[1]}" ];then
     MODE[1]="\"${MODE[1]}\""
fi
if [ -n "${MODE[2]}" ] && [ -z "${LINE[2]}" ];then
     MODE[2]="\"${MODE[2]}\""
fi

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

cat <<-EOF
    EndSection

    Section "Device"
        Identifier        "My Video Card"
        VendorName        "Unknown"
        BoardName         "Unknown"
        ${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

cat <<-EOF
    EndSection

    Section "Screen"
        Driver            "SVGA"
        Device            "My Video Card"
        Monitor           "My Monitor"
        BlankTime         ${X_BLANKTIME:-5}
        SuspendTime       ${X_SUSPENDTIME:-10}
        OffTime           ${X_OFFTIME:-30}
        SubSection "Display"
            Depth         ${X_COLOR_DEPTH:-16}
            Modes         ${MODE[*]}
        EndSubSection
    EndSection

    Section "Screen"
        Driver            "accel"
        Device            "My Video Card"
        Monitor           "My Monitor"
        BlankTime         ${X_BLANKTIME:-5}
        SuspendTime       ${X_SUSPENDTIME:-10}
        OffTime           ${X_OFFTIME:-30}
        Subsection "Display"
            Depth         ${X_COLOR_DEPTH:-16}
            Modes         ${MODE[*]}
        EndSubsection
    EndSection
EOF
