#!/bin/sh
#
# configurexorg
# Copyright (C) 2006-2011  mariodebian at gmail
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#



template=/conf/xorg.conf.tpl
tmp_xorg_file=/tmp/xorg.configurexorg
outputfile=/etc/X11/xorg.conf
mysettings=/conf/mysettings.conf
touch ${mysettings}

restartxorg=0
autodetect_xdriver=1
force_xdriver=0
xdriver_via="#"
xdriver_amd="#"
TCOS_XORG_CONF=

. /conf/tcos.conf
. /conf/tcos-run-functions

if [ -d /usr/lib/xorg/modules/drivers ]; then
  xdriver_dir=/usr/lib/xorg/modules/drivers

elif [ -d /usr/X11R6/lib/modules/drivers ]; then
  xdriver_dir=/usr/X11R6/lib/modules/drivers
fi

# set to 0 to disable debug
verbose=0

debug () {
  if [ ${verbose} = 1 ]; then
    echo "configurexorg DEBUG: $@" >&2
  fi
}

tftp_download_again() {
  MYIP=$(awk -F"=" '/^ip=/ {print $2}' /tmp/net.data)
  debug "downloading file /tcos/conf/${MYIP}.conf"
  download_file /tcos/conf/${MYIP}.conf /conf/mysettings.conf
  # new versions of tftp don't create file
  touch /conf/mysettings.conf
  . /conf/mysettings.conf
}


# FIXME use getopt

for x in "$@"; do
        case $x in
        --getsettings)
                method="getsettings"
                ;;
        --newsettings)
                method="newsettings"
                ;;
        --changesettings)
                method="changesettings"
                ;;
        --downloadagain)
                tftp_download_again
                method="newsettings"
                ;;
        --restartx*)
                restartxorg=1
                ;;
        --autodetect)
                autodetect_xdriver=1
                ;;
        --noautodetect)
                autodetect_xdriver=0
                ;;
        --verbose)
                verbose=1
                ;;
        esac
done


# default xorg settings
xdriver="$TCOS_XORG_VIDEO_DRIVER"
xres="$TCOS_XORG_VIDEO_RES"
xdepth="16"
xhorizsync="28-49"
xvertsync="43-72"
xrefresh="60Hz"
xdpms=""        # default enabled
xmousewheel="" # wheel enabled
xmouseprotocol="ImPS/2"
xlocalfonts="#" # default disabled
xmousedev="/dev/input/mice"

ENABLE_EVDEV="#"
ENABLE_KBD=""
EVDEV_MODEL="evdev"

KBD_DEV=$(find /dev/input/by-path/ -name "*kbd*")
if [ "$KBD_DEV" != "" ]; then
  # link in /dev/input
  [ ! -e /dev/input/keyboard ] && ln -s $KBD_DEV /dev/input/keyboard
  if [ ! -e /usr/lib/xorg/modules/input/kbd_drv.so ]; then
    # use EVDEV (Ubuntu don't install xserver-xorg-input-kbd)
    ENABLE_EVDEV=""
    ENABLE_KBD="#"
  fi
fi

if [ -d /usr/lib/X11/fonts ] && [ ! -e /usr/share/fonts/X11 ] ; then
   mkdir -p /usr/share/fonts
   ln -s /usr/lib/X11/fonts /usr/share/fonts/X11
fi



# FIXME serial mouse scan !!!
if [ -e /dev/input/mice ]; then
 xmousedev="/dev/input/mice"
elif [ -e /dev/psaux ]; then
 xmousedev="/dev/psaux"
fi

# default is Ctrl Alt <== DISABLED
xdontzap="1"
xzap_txt="Section \"Serverflags\"\\
        Option \"DontZap\"      \"yes\"\\
EndSection"

xzap=$xzap_txt

usemodes_txt="Section \"Modes\"\\
        Identifier \"Cimarron\"\\
        # 640x480 @ 60.00 Hz (GTF) hsync: 29.82 kHz; pclk: 23.86 MHz\\
        Modeline \"640x480-60\" 25.1749  640  656  744  800   480  490  492  525 -HSync -VSync\\
        # 640x480 @ 75.00 Hz (GTF) hsync: 37.65 kHz; pclk: 30.72 MHz\\
        Modeline \"640x480-75\" 31.5000  640  656  720  840   480  481  484  500 -HSync -VSync\\
        # 640x480 @ 85.00 Hz (GTF) hsync: 42.92 kHz; pclk: 35.71 MHz\\
        Modeline \"640x480-85\" 36.0000  640  696  752  832   480  481  484  509 -HSync -VSync\\
        # 640x480 @ 90.00 Hz (GTF) hsync: 45.54 kHz; pclk: 37.89 MHz\\
        Modeline \"640x480-90\" 37.8889  640  672  736  832   480  481  484  506 -HSync -VSync\\
        # 640x480 @ 100.00 Hz (GTF) hsync: 50.90 kHz; pclk: 43.16 MHz\\
        Modeline \"640x480-100\" 43.1629  640  680  744  848   480  481  484  509 -HSync -VSync\\
        # 800x600 @ 60.00 Hz (GTF) hsync: 37.32 kHz; pclk: 38.22 MHz\\
        Modeline \"800x600-60\" 40.0000  800  840  976 1056   600  601  605  628 -HSync -VSync\\
        # 800x600 @ 75.00 Hz (GTF) hsync: 47.02 kHz; pclk: 48.91 MHz\\
        Modeline \"800x600-75\" 49.5000  800  816  896 1056   600  601  604  625 -HSync -VSync\\
        # 800x600 @ 85.00 Hz (GTF) hsync: 53.55 kHz; pclk: 56.55 MHz\\
        Modeline \"800x600-85\" 56.2500  800  832  896 1048   600  601  604  631 -HSync -VSync\\
        # 800x600 @ 90.00 Hz (GTF) hsync: 56.88 kHz; pclk: 60.07 MHz\\
        Modeline \"800x600-90\" 60.0649  800  840  928 1056   600  601  604  632 -HSync -VSync\\
        # 800x600 @ 100.00 Hz (GTF) hsync: 63.60 kHz; pclk: 68.18 MHz\\
        Modeline \"800x600-100\" 68.1789  800  848  936 1072   600  601  604  636 -HSync -VSync\\
        # 1024x768 @ 60.00 Hz (GTF) hsync: 47.70 kHz; pclk: 64.11 MHz\\
        Modeline \"1024x768-60\" 65.0000 1024 1048 1184 1344   768  771  777  806 -HSync -VSync\\
        # 1024x768 @ 75.00 Hz (GTF) hsync: 60.15 kHz; pclk: 81.80 MHz\\
        Modeline \"1024x768-75\" 78.7500 1024 1040 1136 1312   768  769  772  800 -HSync -VSync\\
        # 1024x768 @ 85.00 Hz (GTF) hsync: 68.60 kHz; pclk: 94.39 MHz\\
        Modeline \"1024x768-85\" 94.5000 1024 1072 1168 1376   768  769  772  808 -HSync -VSync\\
        # 1024x768 @ 90.00 Hz (GTF) hsync: 72.81 kHz; pclk: 100.19 MHz\\
        Modeline \"1024x768-90\" 100.1869 1024 1088 1200 1376   768  769  772  809 -HSync -VSync\\
        # 1024x768 @ 100.00 Hz (GTF) hsync: 81.40 kHz; pclk: 113.31 MHz\\
        Modeline \"1024x768-100\" 113.3099 1024 1096 1208 1392   768  769  772  814 -HSync -VSync\\
        # 1280x800 @ 60.00 Hz (GTF) hsync: 49.68 kHz; pclk: 83.46 MHz\\
        Modeline \"1280x800-60\"  83.46  1280 1344 1480 1680  800 801 804 828  -HSync +Vsync\\
        # 1280x1024 @ 60.00 Hz (GTF) hsync: 63.60 kHz; pclk: 108.88 MHz\\
        Modeline \"1280x1024-60\" 108.0000 1280 1328 1440 1688  1024 1025 1028 1066 -HSync -VSync\\
        # 1280x1024 @ 75.00 Hz (GTF) hsync: 80.17 kHz; pclk: 138.54 MHz\\
        Modeline \"1280x1024-75\" 135.0000 1280 1296 1440 1688  1024 1025 1028 1066 -HSync -VSync\\
        # 1280x1024 @ 85.00 Hz (GTF) hsync: 91.38 kHz; pclk: 159.36 MHz\\
EndSection"

usemodes=$usemodes_txt


# other
withnofail=0
setsettings=1

stamp=$(date +'%Y.%m.%d.%H.%M')
cmdline=$@



get_myconf() {
# $1 is var name
# $2 is default value
debug "get_myconf() varname=$1 default=$2"
varname=$1
defvalue=$2
 value=$(grep ^${varname} ${mysettings} | awk -F "=" '{gsub(/\"/,"",$2); print $2}' )
 if [ "${value}" != "" ]; then
   debug "get_myconf() new value=${value}"
   echo ${value}
 else
   echo ${defvalue}
 fi
}

get_myres() {
 echo $(awk -F "=" '/^xres/ { gsub(/\"/,"", $2) ; split($2,A," ") ; print A[1]}' ${mysettings} )
}

get_myrefresh() {
 echo $(awk -F "=" '/^xres/ { gsub(/\"/,"", $2) ; split($2,A," ") ; print A[3]}' ${mysettings} )
}

get_mybool() {
 varname=$1
 value=$(grep ^${varname} ${mysettings} | awk -F "=" '{gsub(/\"/,"",$2); print $2}' )
 if [ "${value}" = "1" ]; then
   echo ""  # enabled
 else
   echo "#" # disabled
 fi
}

detect_driver() {
debug "detect_driver()"
#if [ $force_xdriver = 1 ]; then
#  debug "detect_driver() force OFF"
#  return
#fi

if [ -d /usr/share/xserver-xorg/pci/ ]; then
  xpci=$(get_vga_vendor vendormodel)
  #xpci=$(lspci -nn| grep VGA | grep -E -o '[0-9a-z]{4}\:[0-9a-z]{4}' | tr [a-z] [A-Z])
  #xpci=$(lspci -vn | awk '/VGA/ {print $3}' | tail -1 | tr [a-z] [A-Z])
  #xpci=$(lspci -n -s $(lspci | awk '/VGA/ {print $1}') | tail -1 | awk '{print $3}' | tr [a-z] [A-Z])
  if [ "${xpci}" != "" ]; then
    # get last line
    _xdev=$(grep "${xpci}" /usr/share/xserver-xorg/pci/xorg.pciids| awk '{print $1}' | tail -1)
    xdev=$(basename ${_xdev})
    if [ -f ${xdriver_dir}/${xdev}_drv.so ]; then
      xdriver=$xdev
      debug "detect_driver() FILE ${xdriver_dir}/${xdev}_drv.so FOUND"
    else
      debug "detect_driver() FILE ${xdriver_dir}/${xdev}_drv.so NOT FOUND"
    fi
    return
  fi
fi

# detect Xorg kernel module and driver
# pcimodules --class 0x30000 --classmask 0xffff00 | grep -v fb$
if [ -x $(which discover) ]; then
  counts=0
  all_file_drivers=$(ls $xdriver_dir/|grep "_drv.so"|grep -v vga)
  all_drivers=""
  for file_driver in $all_file_drivers; do
    all_drivers="$all_drivers $(echo $file_driver| sed s/'_drv.so'//g)"
  done
  debug "detect_driver() all_drivers=$all_drivers"
  
  xdevice=$(discover --data-path=xfree86/server/device/driver display)
  debug "detect_driver() xdevice=$xdevice"
  for driver in $all_drivers; do
     if [ "$xdevice" = "$driver" ]; then
        debug "detect_driver() possible driver: $driver"
        xdriver_candidate=$driver
        counts=$((counts + 1))
     fi
  done
  if [ $counts = 1 ]; then
     xdriver=$xdriver_candidate
     debug "detect_driver() xdriver FOUND ===> $xdriver"
  fi
else
  debug "detect_driver() discover not found"
fi
#
#
#  IMPORTANT
#
#  for discover other devices use something like this
#
# allmodules=""
# for module in $(discover --data-path=linux/module/name ethernet sound ide usb modem bridge isdn printer scanner mouse webcam floppy); do
#   if [ "$module" != "" ]; then
#     allmodules="$allmodules $module"
#   fi
# done
# echo $allmodules
#
}

# FIXME use getopt

load_cmdline () {
   for x in $cmdline; do
        case $x in
        --xdriver=*)
                xdriver=${x#--xdriver=}
                force_xdriver=1
                ;;
        --xres=*)
                xres=${x#--xres=}
                ;;
        --xdepth=*)
                xdepth=${x#--xdepth=}
                ;;
        --xrefresh=*)
                xrefresh=${x#--xrefresh=}
                ;;
        #--xhorizsync=*)
        #        xhorizsync=${x#--xhorizsync=}
        #        ;;
        #--xvertsync=*)
        #        xvertsync=${x#--xvertsync=}
        #        ;;
        --xfontserver=*)
                xfontserver=${x#--xfontserver=}
                ;;
        --xmouseprotocol=**)
                xmouseprotocol=${x#--xmouseprotocol=}
                ;;
        --restartx)
                restartxorg=1
                ;;
        --xmousenowheel)
                xmousewheel="#"
                ;;
        --xnodpms)
                xdpms="#"
                ;;
        --xdontzap)
                xdontzap="1"
                ;;
        --dontzap)
                xdontzap="1"
                ;;
        --zap)
                xdontzap="0"
                ;;
        --xzap)
                xdontzap="0"
                ;;
        --xlocalfonts)
                xlocalfonts=""
                ;;
        --withnofail)
                withnofail=1
                ;;
        --outputfile=*)
                outputfile=${x#--outputfile=}
                ;;
        esac
   done
} # end of load_cmdline


set_sync() {
 RES=${1}
 xhorizsync=$(get_sync "$RES@${xrefresh}" | awk -F ":" '{print $1}')
 xvertsync=$(get_sync "$RES@${xrefresh}" | awk -F ":" '{print $2}')
}

get_sync() {
# horiz and vert sync table
# orig by debian debconf
# ==> xserver-xorg.postinst
 debug "get_sync() RES=$1"
 RES=${1}
 case "$RES" in
        "640x480@60Hz")
          horiz_sync="28-33"
          # Some old monitors only support max 60Hz
          vert_sync="43-60"
          ;;
        "640x480@72Hz")
          horiz_sync="28-38"
          vert_sync="43-72"
          ;;
        "800x600@60Hz")
          horiz_sync="28-38"
          vert_sync="43-60"
          ;;
        "800x600@72Hz")
          horiz_sync="28-48"
          vert_sync="43-72"
          ;;
        "800x600@85Hz")
          horiz_sync="30-54"
          vert_sync="50-85"
          ;;
        "832x624@75Hz")
          horiz_sync="30-50"
          vert_sync="50-75"
          ;;
        "1024x768@60Hz")
          horiz_sync="28-49"
          vert_sync="43-72"
          ;;
        "1024x768@70Hz")
          horiz_sync="30-57"
          vert_sync="43-72"
          ;;
        "1024x768@75Hz")
          horiz_sync="30-60"
          vert_sync="50-75"
          ;;
        "1152x768@54.8Hz")
          # This is a 15" PowerBook G4 mode; its video hardware (LCD) was also
          # capable of 896x600 and 720x480 pixels at a 3:2 aspect ratio and
          # 1024x768, 800x600, and 640x480 pixels at a 4:3 aspect ratio, so give
          # its horizontal and vertical ranges a little more "headroom" than
          # that required by this specific mode to accomodate the others.
          horiz_sync="30-50"
          vert_sync="50-72"
          ;;
        "1152x864@60Hz")
          horiz_sync="30-68"
          vert_sync="50-70"
          ;;
        "1152x864@75Hz")
          horiz_sync="30-68"
          vert_sync="50-85"
          ;;
        "1280x768@60Hz")
          horiz_sync="30-65"
          vert_sync="30-60"
          ;;
        "1280x800@60Hz")
          horiz_sync="30-67"
          vert_sync="30-60"
          ;;
        "1280x960@60Hz")
          horiz_sync="30-60"
          vert_sync="50-75"
          ;;
        "1280x960@85Hz")
          horiz_sync="30-92"
          vert_sync="50-85"
          ;;
        "1280x1024@60Hz")
          horiz_sync="30-65"
          vert_sync="50-75"
          ;;
        "1400x1050@60Hz")
          horiz_sync="30-67"
          vert_sync="50-75"
          ;;
        "1400x1050@75Hz")
          horiz_sync="30-85"
          vert_sync="50-80"
          ;;
        "1440x900@60Hz")
          horiz_sync="30-67"
          vert_sync="50-75"
          ;;
        "1440x900@75Hz")
          horiz_sync="30-85"
          vert_sync="50-80"
          ;;
        "1600x1024@85Hz")
          horiz_sync="30-70"
          vert_sync="50-90"
          ;;
        "1600x1200@60Hz")
          horiz_sync="30-75"
          vert_sync="50-85"
          ;;
        "1600x1200@75Hz")
          horiz_sync="30-94"
          vert_sync="50-75"
          ;;
        "1600x1200@85Hz")
          horiz_sync="30-107"
          vert_sync="50-85"
          ;;
        "1680x1050@60Hz")
          horiz_sync="30-90"
          vert_sync="50-60"
          ;;
        "1792x1344@75Hz")
          horiz_sync="30-107"
          vert_sync="50-85"
          ;;
        "1792x1344@60Hz")
          horiz_sync="30-84"
          vert_sync="50-75"
          ;;
        "1856x1392@60Hz")
          horiz_sync="30-87"
          vert_sync="50-75"
          ;;
        "1856x1392@75Hz")
          horiz_sync="30-113"
          vert_sync="50-75"
          ;;
        "1920x1200@60Hz")
          horiz_sync="30-75"
          vert_sync="30-60"
          ;;
        "1920x1440@60Hz")
          horiz_sync="30-90"
          vert_sync="50-75"
          ;;
        "1920x1440@75Hz")
          horiz_sync="30-130"
          vert_sync="60-160"
          ;;
        "1920x1440@85Hz")
          horiz_sync="30-130"
          vert_sync="60-160"
          ;;
        "2048x1536@60Hz")
          horiz_sync="30-100"
          vert_sync="60-85"
          ;;
        "2048x1536@75Hz")
          horiz_sync="30-125"
          vert_sync="60-100"
          ;;
        "2048x1536@85Hz")
          horiz_sync="30-140"
          vert_sync="60-160"
          ;;
         *)
          debug "get_sync() Unkwnow screen resolution ${RES}"
          echo ":"
          ;;
      esac
    debug "get_sync() $horiz_sync:$vert_sync"
    echo "$horiz_sync:$vert_sync"
}


set_settings() {

TCOS_ENABLE_XDRIVER_FULLAUTODETECT=""

if [ "$xdriver" != "auto" ]; then
  autodetect_xdriver=0
fi

if [ "$xdriver" = "xorgauto" ]; then
   # Leave Xorg to autodetect all
   [ -d /usr/share/X11/xorg.conf.d ] && TCOS_XORG_CONF=/usr/share/X11/xorg.conf.d/
   [ -d /usr/lib/X11/xorg.conf.d ]   && TCOS_XORG_CONF=/usr/lib/X11/xorg.conf.d/

   [ "$TCOS_XORG_CONF" = "" ] && xdriver=fullauto
fi

if [ "$xdriver" = "fullauto" ]; then
   # Try Xorg to detect all?
   TCOS_ENABLE_XDRIVER_FULLAUTODETECT="#"
fi


if [ "$xdriver" = "auto" ] || [ $autodetect_xdriver = 1 ]; then
   # Try Xorg to detect?
   detect_driver
   if [ "$xdriver" = "auto" ]; then
      xdriver="vesa"
   fi
fi

if [ "$xdriver" = "via" ] || [ "$xdriver" = "openchrome" ]; then
  xdriver_via=""
fi

if [ "$xdriver" = "amd" ]; then
  xdriver_amd=""
else
  usemodes=""
fi

disablesync=""
if [ "$xdisablesync" = "disable" ]; then
  disablesync="#"
fi

if [ "${xdontzap}" = "0" ] || [ "$xdontzap" = "#" ]; then
  # enable Ctrl Alt <===
  xzap="#"
elif [ "$xdontzap" = "1" -o "$xdontzap" = "" ]; then
  # disable Ctrl Alt <==
  xzap=${xzap_txt}
else
  debug "unknow dontzap"
  xzap=""
fi

if [ "$TCOS_ENABLE_FONT_SERVER" = "1" ]; then
  TCOS_ENABLE_FONT_SERVER=""
else
  TCOS_ENABLE_FONT_SERVER="#"
fi

if [ "$TCOS_XORG_XKB" = "br" ]; then
  TCOS_XORG_XKBMODEL="abnt2"
  EVDEV_MODEL="abnt2"
fi


if [ "$TCOS_COMPOSITE" = "1" ]; then
  ENABLE_COMPOSITE="#"
else
  ENABLE_COMPOSITE=""
fi


debug "set_settings() xdriver=$xdriver xres=$xres xdepth=$xdepth xdpms=$xdpms"
debug "xhorizsync=$xhorizsync xvertsync=$xvertsync xrefresh=$xrefresh xmousewheel=$xmousewheel"
debug "xmousedev=$xmousedev2 xmouseprotocol=$xmouseprotocol2 xlocalfonts=$xlocalfonts"
debug "date=$stamp server=$SERVER xdontzap=$xdontzap dontzap=$xzap"

# sed template to output file
sed -e s/"__xdriver__"/"${xdriver}"/g \
    -e s/"__xres__"/"${xres}"/g \
    -e s/"__xdriver_via__"/"${xdriver_via}"/g \
    -e s/"__xdriver_amd__"/"${xdriver_amd}"/g \
    -e s/"__xdepth__"/"${xdepth}"/g \
    -e s/"__xdpms__"/"${xdpms}"/g \
    -e s/"__xhorizsync__"/"${xhorizsync}"/g \
    -e s/"__xvertsync__"/"${xvertsync}"/g \
    -e s/"__xrefresh__"/"${xrefresh}"/g \
    -e s/"__disablesync__"/"${disablesync}"/g \
    -e s/"__enable_composite__"/"${ENABLE_COMPOSITE}"/g \
    -e s/"__xevdevenable__"/"${ENABLE_EVDEV}"/g \
    -e s/"__xkbdenable__"/"${ENABLE_KBD}"/g \
    -e s/"__evdevmodel__"/"${EVDEV_MODEL}"/g \
    -e s/"__xkbmap__"/"${TCOS_XORG_XKB}"/g \
    -e s/"__xkbmodel__"/"${TCOS_XORG_XKBMODEL}"/g \
    -e s/"__xmousewheel__"/"${xmousewheel}"/g \
    -e s/"__xmousenowheel__"/"${xmousenowheel}"/g \
    -e s/"__xmousedev__"/"${xmousedev2}"/g \
    -e s/"__xmouseprotocol__"/"${xmouseprotocol2}"/g \
    -e s/"__xlocalfonts__"/"${xlocalfonts}"/g \
    -e s/"__date__"/"${stamp}"/g \
    -e s/"unix\/:7100"/"tcp\/${SERVER}:7100"/g \
    -e s/"__xfontserver__"/"${SERVER}"/g \
    -e s/"__enable_font_server__"/"${TCOS_ENABLE_FONT_SERVER}"/g \
    -e s/"__enable_xdriver_fullautodetect__"/"${TCOS_ENABLE_XDRIVER_FULLAUTODETECT}"/g \
    -e s/"__dontzap__"/"${xzap}"/g \
    -e s/"__xdontzap__"/"${xdontzap}"/g \
    -e s/"__usemodes__"/"${usemodes}"/g \
  ${template} > ${tmp_xorg_file}
}





save_settings() {
 # backup old settings
 [ -f ${outputfile} ] && mv ${outputfile} ${outputfile}.${stamp}
 # move new settings
 if [ -f ${tmp_xorg_file} ]; then
  cat ${tmp_xorg_file} > ${outputfile}
 fi

if [ -d "${TCOS_XORG_CONF}" ]; then
  FONTS_DIR="/usr/share/X11/fonts"
  [ -d "/usr/share/fonts/X11" ] && FONTS_DIR="/usr/share/fonts/X11"
  # xorgauto delete /etc/X11/xorg.conf
  rm -f "${outputfile}"

  # create ${TCOS_XORG_CONF}/60-fonts.conf
  cat << EOF > ${TCOS_XORG_CONF}/60-fonts.conf

Section "Files"
	FontPath	"${FONTS_DIR}/misc"
	FontPath	"${FONTS_DIR}/100dpi"
	FontPath        "tcp/${SERVER}:7100"
	FontPath        "tcp/${SERVER}:7101"
EndSection

EOF
fi
}

getsimple() {
 value=$(grep "^#$1=" ${outputfile} 2>/dev/null | tail -1 | awk -F "=" '{print $2}')
 if [ "${value}" = "#" ]; then
  value=0
 elif [ "${value}" = "" ]; then
  value=1
 fi
 echo -n "$1=${value} "
}

getvalue() {
 value=$(grep "^#$1" ${outputfile} 2>/dev/null | tail -1 | awk -F "=" '{print $2}')
 echo "${value}"
}


get_settings() {
 getsimple "xdriver"
 getsimple "xres"
 getsimple "xdepth"
 getsimple "xdpms"
 getsimple "xrefresh"
 getsimple "xhorizsync"
 getsimple "xvertsync"
 getsimple "xfontserver"
 getsimple "xmousedev"
 getsimple "xmousewheel"
 getsimple "xmouseprotocol"
 #getsimple "xmousenowheel"
 getsimple "xdontzap"
 getsimple "xdisablesync"
}

load_settings() {
      xdriver=$(getvalue "xdriver")
         xres=$(getvalue "xres")
       xdepth=$(getvalue "xdepth")
        xdpms=$(getvalue "xdpms")
     xrefresh=$(getvalue "xrefresh")
   xhorixsync=$(getvalue "xhorizsync")
    xvertsync=$(getvalue "xvertsync")
  xfontserver=$(getvalue "xfontserver")
  xmousewheel=$(getvalue "xmousewheel")
xmousenowheel=$(getvalue "xmousenowheel")
     xdontzap=$(getvalue "xdontzap")
 xdisablesync=$(getvalue "xdisablesync")

}

#########   end functions   ###################

############## start app ######################

if [ $(cat ${mysettings} | wc -l) -gt 1 ]; then
 debug "loading settings of remote file..."

 xdriver=$(get_myconf "xdriver" ${xdriver})
 xdepth=$(get_myconf "xdepth" ${xdepth})
 xres=$(get_myres)
 xrefresh=$(get_myrefresh)
 xdpms=$(get_mybool "xdpms")
 xmousewheel=$(get_mybool "xmousewheel")
 xdontzap=$(get_mybool "xdontzap")
 xdisablesync=$(get_myconf "xdisablesync")
fi


SERVER=$(read_server "font-server")
# get first one
for iserver in ${SERVER}; do
 SERVER=${iserver}
 break
done

if [ "${xmousewheel}" = "#" ]; then
 xmousenowheel=""
else
 xmousenowheel="#"
fi
xmousedev2=$(echo ${xmousedev} | sed s/'\/'/'\\\/'/g)
xmouseprotocol2=$(echo ${xmouseprotocol} | sed s/'\/'/'\\\/'/g)

########################################################

if [ "${method}" = "newsettings" ]; then
 load_cmdline
 set_sync "${xres}"
 set_settings
 save_settings
elif [ "${method}" = "getsettings" ]; then
 get_settings
elif [ "${method}" = "changesettings" ]; then
 load_settings
 load_cmdline
 set_sync "${xres}"
 set_settings
 save_settings
else
 echo "Usage configurexorg:
   --getsettings - print xorg settings
   --newsettings - set all new settings based on default and cmdline
   --changesettings - change any settings based on configured
   --downloadagain - download ${mysettings} again
   --restartxorg - restart Xorg after edit/change/create settings"
 exit 1
fi


if [ ${restartxorg} = 1 ]; then
  debug "restarting xorg...."
  /sbin/start-stop-daemon --start --quiet --background --exec /sbin/restartxorg > /dev/null 2>&1
fi

exit 0
