#!/bin/sh
PATH=$PATH:/usr/X11/bin:/opt/aldccp/bin:/usr/lib64/qt5/bin

export PATH

if test -f /etc/aldccp/system; then
. /etc/aldccp/system
else
  ENV_LANG=en_US.utf8
  XkbLayout=en
fi
export LANG=$ENV_LANG

rm -rf /tmp/.ALDccp
rm -f /var/run/ALDesk/aldesk

XF86="/tmp/XF86auto"

#------------------------------------------------------------------

if test -r /tmp/autoconfig.txt; then
. /tmp/autoconfig.txt
else
  USE_915resolution=n
fi

if test -e /proc/fb ; then
  fb=`cat /proc/fb`
  fb=${fb#0 }
  case $fb in
  amdgpu*)
    X11DriverSet="amdgpu" ;;
  *)
    X11DriverSet="fbdev"
    ;;
  esac

  for option ; do
    case $option in
    fb*)
      X11DriverSet="fbdev" ;;
    ve*)
      KMS=n
      X11DriverSet="vesa" ;;
    X11)
      X11DriverSet=$X11Driver ;;
    auto)
      X11DriverSet=$X11Driver ;;
    *)
      echo "ERROR: $option"
      exit 1
      ;;
    esac
  done
else
  X11DriverSet="vesa"
fi

function chk_VMode16() {

  while read mod whd ignored; do
    case "$mod" in
    0x*) 
      case "$whd" in
      *x*x16) 
	echo $whd
      ;;
      esac
    ;;
    esac
  done
}

function chk_VMode24() {

  while read mod whd ignored; do
    case "$mod" in
    0x*) 
      case "$whd" in
      *x*x24) 
	echo $whd
      ;;
      *x*x32) 
	echo $whd
      ;;
      esac
    ;;
    esac
  done
}

WHD="800x600x16"

function Get_VMode() {
  local lineDialog m h mx
  m=0
  mx="n"
  for i in $1; do
    m=`expr $m + 1`
    lineDialog="$lineDialog $m $i"
#    if test $m = "14"; then break; fi
    if test $m = "14"; then mx=$m; fi
  done
  if test $mx != "n"; then
    m=$mx
  fi
  h=`expr $m + 7`

  exec 3>&1
  value=`dialog --clear --title " DISPLAY SIZE " \
	  --menu "Start Display size \n\n" \
  $h 60 $m $lineDialog \
  2>&1 1>&3`

  m=1
  if test $? = "0"; then
    for i in $1; do
      if test x$value = x"$m"; then
	  WHD=$i
	  break;
      fi
      m=`expr $m + 1`
    done
  fi
}

function Set_XF86_File() {

cat > ${XF86} <<END_cat
Section "Files"
  FontPath  	"/usr/share/fonts/X11/misc:unscaled"
  FontPath  	"/usr/share/fonts/X11/75dpi:unscaled"
  FontPath  	"/usr/share/fonts/X11/Type1"
  FontPath	"/usr/share/fonts/X11/TTF"
  ModulePath  	"/usr/X11R6/lib64/modules"
EndSection

Section "ServerFlags"
  Option  	"AllowMouseOpenFail"
#  Option  	"DontZap" "off"
EndSection

Section "Module"
  Load  	"ddc"
#  Load  	"dbe"
  Load  	"i2c"
  Load  	"type1"
  Load  	"extmod"
  Load  	"freetype"
#  Load  	"xaa"
#  Load  	"glx"
#  Load  	"dri"
#	Load  "synaptics"
EndSection

Section "InputDevice"
  Driver  	"keyboard"
  Identifier  	"Keyboard[0]"
  Option 	"Protocol"  	"Standard"
  Option 	"XkbKeyCodes"  	"xfree86"
  Option 	"XkbRules"  	"base"
  Option 	"XkbModel"  	"pc105"
  Option 	"XkbLayout"  	"$XkbLayout"
  Option 	"XkbVariant"  	"nodeadkeys"
  Option 	"XkbOptions"  	""
EndSection

END_cat

cat >> ${XF86} <<END_cat
Section "InputDevice"
  Driver  	"mouse"
  Identifier  	"Mouse[1]"
#  Option 	"Device"  	"/dev/psaux"
  Option 	"Device"  	"/dev/input/mice"
#  Option 	"Device"  	"/dev/input/mouse1"
#  Option 	"Emulate3Buttons"	"on"
#  Option 	"EmulateWheel"	"on"
#  Option 	"ChordMiddle"	"on"
  Option "Buttons" "5"
  Option "ZAxisMapping" "4 5"
  Option 	"Name"  	"AutoDetected"
#  Option 	"Protocol"  	"ImPS/2"
  Option 	"Protocol"  	"ExplorerPS/2"
  Option 	"Vendor"  	"AutoDetected"
EndSection
END_cat


if [ x$KMS = x"y" ]; then

cat >> ${XF86} <<END_cat
Section "Monitor"
  Identifier  	"Monitor[0]"
  ModelName  	"AutoDetected"
  VendorName  	"AutoDetected"
EndSection

Section "Screen"
  Identifier  	"Screen[0]"
  DefaultDepth	24
  SubSection "Display"
    Viewport   0 0
    Depth	24
  EndSubSection
  Device  	"Device[0]"
  Monitor  	"Monitor[0]"
#  Option	"Accel" "1"
EndSection

Section "Device"
  BoardName  	"AutoDetected"
  Driver  	"$X11DriverSet"
#  Driver  	"vesa"
#  BusID           "PCI:00:02:0"
  #Videoram        64000
  #Option     "AccelMethod"	"EXA"
  #Option     "AccelDFS"		"1"
  #Option     "ColorTiling"		"1"
  #Option     "DRI"		"0"
  #Option     "ColorKey"           	# <i>
  #Option     "VideoKey"           	# <i>
  #Option     "FallbackDebug"      	# [<bool>]
  #Option     "Tiling"		"0"
  #Option     "CacheLines"		"3"
  #Option     "Shadow"		"1"
  #Option     "SwapbuffersWait"		"1"
  #Option     "XvPreferOverlay"    	# [<bool>]
  #Option     "DebugFlushBatches"  	# [<bool>]
  #Option     "DebugFlushCaches"   	# [<bool>]
  #Option     "DebugWait"          	# [<bool>]
  Identifier  	"Device[0]"
  VendorName  	"AutoDetected"
EndSection

END_cat

else

cat >> ${XF86} <<END_cat
Section "Monitor"
  Identifier  	"Monitor[0]"
  ModelName  	"AutoDetected"
  VendorName  	"AutoDetected"
  HorizSync  	20-100
  VertRefresh  	20-100
  UseModes  	"Modes[0]"
EndSection

Section "Modes"
  Identifier 	"Modes[0]"
  Modeline	"640x480" 25.2 640 656 752 800 480 490 492 525
  Modeline	"800x600" 40.0 800 840 968 1056 600 601 605 628
  modeline	"1024x768" 65.0 1024 1048 1184 1344 768 771 777 806
  Modeline	"1024x768" 78.53 1024 1040 1216 1400 768 768 778 802
# 1280x800 @ 60.00 Hz (GTF) hsync: 49.68 kHz; pclk: 83.46 MHz
  Modeline "1280x800"  83.46  1280 1344 1480 1680  800 801 804 828
# 1280x1024 @ 60.00 Hz hsync: xx.xx kHz; pclk: 108.0 MHz
  Modeline  "1280x1024" 108.0 1280 1328 1440 1688 1024 1025 1028 1066
# 1400x1050 @ 60.00 Hz (GTF) hsync: 65.22 kHz; pclk: 122.61 MHz
  ModeLine "1400x1050"  122.61  1400 1488 1640 1880  1050 1051 1054 1087
# 1400x1050 @ 72.00 Hz (GTF) hsync: 78.77 kHz; pclk: 149.34 MHz
  ModeLine "1400x1050"  149.34  1400 1496 1648 1896  1050 1051 1054 1094
# 1400x1050 @ 75.00 Hz (GTF) hsync: 82.20 kHz; pclk: 155.85 MHz
  ModeLine "1400x1050"  155.85  1400 1496 1648 1896  1050 1051 1054 1096
  ModeLine	"1280x768" 111.69 1280 1336 1616 1728 768 770 782 808
  Modeline	"1440x900" 106.47 1440 1520 1672 1904 900 901 904 932
  Modeline	"1440x900" 108 1440 1520 1672 1904 900 903 909 934
EndSection

END_cat

if test x$GRD = x ; then
cat >> ${XF86} <<END_cat
Section "Screen"
  Identifier  	"Screen[0]"
  SubSection "Display"
    Modes  	"800x600" "640x480"
  EndSubSection
  Device  	"Device[0]"
  Monitor  	"Monitor[0]"
EndSection
END_cat
else
cat >> ${XF86} <<END_cat
Section "Screen"
  Identifier  	"Screen[0]"
  DefaultDepth	$GRD
  SubSection "Display"
    Depth	$GRD
    Modes  	"$GR_WH" "800x600" "640x480"
  EndSubSection
  Device  	"Device[0]"
  Monitor  	"Monitor[0]"
EndSection
END_cat
fi

cat >> ${XF86} <<END_cat

Section "Device"
  BoardName  	"AutoDetected"
  Driver  	"$X11DriverSet"
#  Driver  	"i965"
#  BusID           "PCI:00:02:0"
END_cat

if test x$VideoRam != x; then
echo "  VideoRam        $VideoRam" >>${XF86}
fi

cat >> ${XF86} <<END_cat
  Identifier  	"Device[0]"
  VendorName  	"AutoDetected"
EndSection

END_cat
fi

cat >> ${XF86} <<END_cat
Section "ServerLayout"
  Identifier  	"Layout[all]"
  InputDevice	"Keyboard[0]" 	"CoreKeyboard"
  InputDevice	"Mouse[1]" 	"CorePointer"
END_cat

cat >> ${XF86} <<END_cat
  Screen	"Screen[0]"
EndSection

#Section "DRI"
#  Group  	"video"
#  Mode   	0660
#EndSection

END_cat
}
# -------------------------------------------------------------------------
if [ x$KMS = x"n" ]; then

if which dialog > /dev/null; then
  if which Xvesa > /dev/null && test x$USE_915resolution = x"n"; then
    MODES16=`Xvesa -listmodes 2>&1 | chk_VMode16`
    MODES24=`Xvesa -listmodes 2>&1 | chk_VMode24`
  else
    MODES16="800x600x16 1024x768x16 1280x800x16 1280x1024x16 1440x900x16 1680x1050x16 1920x1440x16 640x480x16"
    MODES24="800x600x24 1024x768x24 1280x800x24 1280x1024x24 1440x900x24 1680x1050x24 1920x1440x24 640x480x24"
  fi

  Get_VMode "$MODES16 $MODES24"
fi


GRW=`echo $WHD | cut -d 'x' -f 1`
GRH=`echo $WHD | cut -d 'x' -f 2`
GRD=`echo $WHD | cut -d 'x' -f 3`


if [ x$KMS = x"n" ]; then
  if [ ! x$GRW = x"640" ]; then
    if [ x$USE_915resolution = x"y" ]; then
      915resolution 49 $GRW $GRH $GRD
cat > /tmp/i915res <<END_cat
GRW="$GRW"
GRH="$GRH"
GRD="$GRD"
END_cat
    fi
  fi
fi

GR_WH=$GRW"x"$GRH
GR_WHD=$GRW"x"$GRH"x"$GRD

  Set_XF86_File

else
  GR_WH="800x600"
  GR_WHD="800x600x16"
  Set_XF86_File

fi

#------------------------------------------------------------------

#sysresources=/usr/X11/lib/X11/xinit/.Xresources
#sysmodmap=/usr/X11/lib/X11/xinit/.Xmodmap
#userresources="/root/.Xresources"
#usermodmap="/etc/X11/Xmodmap"

xinitres=/tmp/ALDxinitres

cat > ${xinitres} <<END_xinitres
aldesk
# su fred -c aldesk
END_xinitres

xinit ${xinitres} -- /usr/X11/bin/X -s 0 -xf86config $XF86

rm -f /tmp/ALDxinitres
if test x$KMS = x"n"; then
  setfont lat1-12
  setfont lat1-08
fi

if test -f /var/run/ALDesk/aldesk; then
  ALD_stat=`cat /var/run/ALDesk/aldesk`
  if test X$ALD_stat = X"ShutDown"; then
    rm -f /var/run/ALDesk/aldesk
    halt
  fi
fi
