# 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)
# 

#
# The XSERVER being used (Either detected or specified) is passed as the
# only argument to this script
#
ACTUAL_XSERVER=$1

. /etc/ltsp_functions

DEFAULT_SERVER=${SERVER:-"192.168.0.254"}
X_MOUSE_BAUD=${X_MOUSE_BAUD:-"1200"}
X_MOUSE_EMULATE3BTN=${X_MOUSE_EMULATE3BTN:-"N"}
USE_TOUCH=${USE_TOUCH:-"N"}
USE_XFS=${USE_XFS:-"N"}

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

X_DPMS=${X_DPMS:-"N"}

################################################################################
# Build the xorg configuration file
################################################################################
echo "Building the Xorg configuration file" >/dev/tty

#####################
# Setup server layout
#####################
cat <<-EOF
Section "ServerLayout"
        Identifier      "Xorg Configured"
        Screen          "Screen00" 0 0
EOF

for i in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15; do
  X_SCREEN_POS=X_SCREEN_POS_$i
	if [ ! -z "${!X_SCREEN_POS}" ]; then
SCREEN_POS=`echo ${!X_SCREEN_POS}|sed -e 's/Screen[0-9][0-9]/"\0"/'`
cat <<-EOF
        Screen          "Screen$i" ${SCREEN_POS}
EOF
	fi
done

# See if Xinerama is set up
if [ "${X_XINERAMA}" = "Y" ]; then
cat <<-EOF
        Option	        "Xinerama" "on"
EOF
fi

cat <<-EOF
        InputDevice     "Keyboard0" "CoreKeyboard"
        InputDevice     "Mouse0" "CorePointer"
        InputDevice     "Mouse1" "AlwaysCore"
EOF

# See if touchscreen is set up
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 Files
#####################

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

Section "Files"
        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 module info
#####################
cat <<-EOF

Section "Module"
        Load   "extmod"
	Load   "dbe"
	Load   "dri"
	Load   "glx"
	Load   "freetype"
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}\" "
        if [ ${MODULE} = "vnc" ]; then
           VNC_MODULE_LOADED=1
        fi
    fi
done

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

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

cat <<-EOF

Section "InputDevice"
       Identifier  "Keyboard0"
       Driver      "keyboard"
       Option      "XkbLayout" "${XKBLAYOUT:-"us"}"
	   Option      "XkbOptions" "grp:ctrl_shift_toggle,grp_led:scroll"
	   Option      "XkbVariant" ",winkeys,winkeys"
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

Section "InputDevice"
       Identifier  "Mouse1"
       Driver      "mouse"
       Option      "Device"          "${X_USBMOUSE_DEVICE:-"/dev/input/mice"}"
       Option      "Protocol"        "${X_USBMOUSE_PROTOCOL:-"PS/2"}"
       Option      "BaudRate"        "${X_USBMOUSE_BAUD}"
       Option      "Resolution"      "${X_USBMOUSE_RESOLUTION}"
       Option      "Emulate3Buttons" "${USBEMULATE_3_BUTTONS:-"off"}"
       Option      "ZAxisMapping"     "4 5"
       Option      "Buttons"         "${X_USBMOUSE_BUTTONS:-"3"}"
EndSection
EOF

# 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 Monitors
########################

for i in 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15; do
  X_SCREEN_POS=X_SCREEN_POS_$i
  if [ "$i" == "00" -o ! -z "${!X_SCREEN_POS}" ]; then
    TEMP=X_HORZSYNC_$i
    HORIZSYNC=${!TEMP:="$X_HORZSYNC"}
    TEMP=X_VERTREFRESH_$i
    VERTREFRESH=${!TEMP:="$X_VERTREFRESH"}
    TEMP=X_MODE_0_$i
    MODE_0=${!TEMP:="$X_MODE_0"}
    TEMP=X_MODE_1_$i
    MODE_1=${!TEMP:="$X_MODE_1"}
    TEMP=X_MODE_2_$i
    MODE_2=${!TEMP:="$X_MODE_2"}
cat <<-EOF

Section "Monitor"
        Identifier      "Monitor${i}"
        VendorName      "Unknown"
        ModelName       "Unknown"
        HorizSync       ${HORIZSYNC}
        VertRefresh     ${VERTREFRESH}
EOF

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

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

EOF
  fi
done

########################
# Setup Devices
########################

for j in 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15; do
  X_SCREEN_POS=X_SCREEN_POS_$j
  if [ "$j" == "00" -o ! -z "${!X_SCREEN_POS}" ]; then
    TEMP=X4_BUSID_$j
    X4_BUSID=${!TEMP:="$X4_BUSID"}
    TEMP=X_VIDEORAM_$j
    X_VIDEORAM=${!TEMP:="$X_VIDEORAM"}
    TEMP=XSERVER_$j
    X4_DRIVER=${!TEMP:="$XSERVER"}

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

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

cat <<-EOF
Section "Device"
        Identifier "Card${j}"
        Driver     "${X4_DRIVER}"
		Option "RandRRotation" "yes"
		Option "Rotate" "left"
        ${BUSID}
        ${XVIDRAM}
EOF
    for i in 01 02 03 04 05 06 07 08 09 10; do
        TEMP1=X_DEVICE_OPTION_${i}
        TEMP2=X_DEVICE_OPTION_${i}_${j}
        X_DEV_VAR=${!TEMP2:-"${TEMP1}"}
        X_DEVICE_OPTION=${!X_DEV_VAR}
        X_DEVICE_OPTION_A=`echo ${!X_DEV_VAR}|cut -d" " -f1`
        X_DEVICE_OPTION_B=`echo ${!X_DEV_VAR}|cut -d" " -f2-`

        if [ "${X_DEVICE_OPTION_A}" ]; then
            echo -n "        Option   \"${X_DEVICE_OPTION_A}\" "
        if [ "${X_DEVICE_OPTION_B}" != "${X_DEVICE_OPTION_A}" ]; then
            echo \"${X_DEVICE_OPTION_B}\"
        else
            echo ""
        fi
        fi
    done

cat <<-EOF
EndSection

EOF
  fi
done

#######################
# Setup the Screens
#######################

for i in 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15; do
  X_SCREEN_POS=X_SCREEN_POS_$i
  if [ "$i" == "00" -o ! -z "${!X_SCREEN_POS}" ]; then
    TEMP=X_MODE_0_$i
    MODE_0=${!TEMP:="$X_MODE_0"}
    TEMP=X_MODE_1_$i
    MODE_1=${!TEMP:="$X_MODE_1"}
    TEMP=X_MODE_2_$i
    MODE_2=${!TEMP:="$X_MODE_2"}
    TEMP=X_COLOR_DEPTH_$i
    COLOR_DEPTH=${!TEMP:="$X_COLOR_DEPTH"}

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

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 "Screen${i}" 
        Device "Card${i}" 
        Monitor "Monitor${i}"
        DefaultDepth  ${COLOR_DEPTH:-16}
        Subsection "Display"
                Depth ${COLOR_DEPTH:-16}
                Modes ${MODE[*]}
EOF

if [ "$VNC_MODULE_LOADED" ]; then
   echo '           Option "httpdir" "/usr/share/vnc/classes"'
   echo '           Option "PasswordFile" "/root/.vnc/passwd"'
   echo '           Option "rfbport" "5900"'
   echo '           Option "usevnc"'
fi
cat <<-EOF
        EndSubSection
EndSection

EOF

fi
done

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