#!/bin/sh
#(c) copyright Barry Kauler 2011, bkhome.org
#2011 GPL licence v3 (/usr/share/doc/legal/gpl-3.0.htm)
#called from /usr/bin/xwin.
#20110404 first version.
#110627 slight mod. see also /etc/X11/xorg.conf-auto-pc
#110701 synaptics stuff seems to clash with xorg-server 1.10.1.
#110727 ddcprobe segfaults on my new laptop.
#110912 fix syntax error. Message at exit of script.
#110913 ddcprobe working new laptop latest wary with xorg7.6. fix 1366x768 resolution. fix touchpad.
#120129 jemimah: add Option "SHMConfig" "on".
#120329 correctly set keyboard layout, model and variant.
#120517 arm builds do not work with ddcprobe. for now, just execute some basic code for arm systems.

. /etc/rc.d/MODULESCONFIG

#120517
if [ "`which ddcprobe`" = "" ];then
 #running on an arm board...
 cp -f /etc/X11/xorg.conf-auto-pc /etc/X11/xorg.conf
 echo "#PuppyHardwareProfile=armsystem" >> /etc/X11/xorg.conf
 #create a copy of xorg.conf with the profile in the filename...
 cp -af /etc/X11/xorg.conf /etc/X11/xorg.conf.armsystem
 echo 
 echo "Xorg Wizard Automatic has just run."
 echo "If you are reading this, then you are at the console without X running."
 echo "The usual thing now is to type 'xwin' to start X..."
 exit ###EXIT###
fi

#110701
XORGVER="`Xorg -version 2>&1 | grep '^X\.Org' | rev | cut -f 1 -d ' ' | rev`"
[ ! "$XORGVER" ] && XORGVER="1.3" #110913 precaution.

#120329 broken, use shinobar's keymap-set...
##read the console keymap (country layout), apply to xkb...
#CONSKEYMAP="`cat /etc/keymap | cut -c 1-2`"
#[ "$CONSKEYMAP" = "" ] && CONSKEYMAP="us"
#case $CONSKEYMAP in
#  az) XKEYMAP="fr" ;;     #azerty
#  wa) XKEYMAP="fr" ;;     #wangbe
#  dv) XKEYMAP="dvorak" ;; #dvorak
#  cr) XKEYMAP="cz" ;;     #croat
#  sl) XKEYMAP="si" ;;     #slovene v2.12 bugfix, changed from 'sk' to 'si' (sk is slovakia)
#  sv) XKEYMAP="se" ;;     #sweden
#  uk) XKEYMAP="gb" ;;     #united kingdom
#  *)  XKEYMAP="$CONSKEYMAP" ;;
#esac
#[ -d /usr/share/X11/xkb ] && XKBPATH='/usr/share/X11'
#[ -d /etc/X11/xkb ] && XKBPATH='/etc/X11'
#if [ -d ${XKBPATH}/xkb/symbols/pc ];then #fallback...
# if [ ! -f ${XKBPATH}/xkb/symbols/pc/${XKEYMAP} ];then
#  if [ ! -f ${XKBPATH}/xkb/symbols/${XKEYMAP} ];then
#   echo "ERROR: Console keyboard `cat /etc/keymap` no matching Xorg layout. PLEASE REPORT TO BK"
#   XKEYMAP='us'
#  fi
# fi
#fi
RETVALS="`keymap-set --xopt`"
eval "$RETVALS" #ex: XKBMODEL="pc102", XKBLAYOUT="us", XKBVARIANT=""
[ "$XKBLAYOUT" = "" ] && XKBLAYOUT='us'
[ "$XKBMODEL" = "" ] && XKBMODEL='pc102'

#probe monitor...
ddcprobe >/tmp/ddcprobe.txt
#weird (ddcprobe is very weird), on some hardware, probe fails every alternate run of ddcprobe...
case "`cat /tmp/ddcprobe.txt`" in *edidfail*)
 sleep 1
 ddcprobe >/tmp/ddcprobe.txt
 ;;
esac
case "`cat /tmp/ddcprobe.txt`" in *edidfail*)
 sleep 1
 ddcprobe >/tmp/ddcprobe.txt
 ;;
esac

CHOSENRES=''
if [ -s /tmp/ddcprobe.txt ];then #110727 ddcprobe segfaults on my new laptop.
 #the vesa 'mode:' lines returned by ddcprobe are very limited...
 XMODES="`grep '^mode:' /tmp/ddcprobe.txt | grep -E 'x64k|x16m'| cut -f 2 -d ' ' | sed -e 's/x64k/x16/g ; s/x16m/x24/g' | sort -u -g`"
 echo "$XMODES" > /tmp/Xmodes.txt

 MONCHOICES="`grep '^timing:' /tmp/ddcprobe.txt | grep -E -v 'Apple|8514A|Mac II|\(XGA2\)' | grep -E ' 640x480@| 800x480@| 800x600@| 1024x600@| 1024x768@| 1280x768@| 1280x800@| 1280x1024@| 1366x768@| 1400x1050@| 1440x900@| 1600x900@| 1600x1200@| 1680x1050@| 1920x1080@| 1920x1200@' | cut -f 2 -d ' ' | sort -u -g`" #ex: 1280x800@59
 cMONCHOICES="`grep '^ctiming:' /tmp/ddcprobe.txt | grep -E -v 'Apple|8514A|Mac II|\(XGA2\)' | grep -E ' 640x480@| 800x480@| 800x600@| 1024x600@| 1024x768@| 1280x768@| 1280x800@| 1280x1024@| 1366x768@| 1400x1050@| 1440x900@| 1600x900@| 1600x1200@| 1680x1050@| 1920x1080@| 1920x1200@' | cut -f 2 -d ' ' | sort -u -g`" #ex: 1280x800@59
 dMONCHOICES="`grep '^dtiming:' /tmp/ddcprobe.txt | grep -E -v 'Apple|8514A|Mac II|\(XGA2\)' | grep -E ' 640x480@| 800x480@| 800x600@| 1024x600@| 1024x768@| 1280x768@| 1280x800@| 1280x1024@| 1366x768@| 1400x1050@| 1440x900@| 1600x900@| 1600x1200@| 1680x1050@| 1920x1080@| 1920x1200@' | cut -f 2 -d ' ' | sort -u -g`" #ex: 1280x800@59
 xMONCHOICES="$MONCHOICES"
 [ "$cMONCHOICES" ] && xMONCHOICES="$cMONCHOICES"
 [ "$dMONCHOICES" ] && xMONCHOICES="$dMONCHOICES"

 [ ! "$xMONCHOICES" ] && xMONCHOICES="`echo "$XMODES" | grep '1024x768'`" #fallback. 110912 syntax error.

 if [ "$xMONCHOICES" ];then
  CHOSENRES="`echo -n "$xMONCHOICES" | tail -n 1 | cut -f 1 -d '@'`"
  #patch video bios resolution for widescreen...
  if grep -F 'oem:' /tmp/ddcprobe.txt | grep -F -q 'Intel' ;then
   MYXYRES="`echo -n "$CHOSENRES" | cut -f 1-2 -d 'x'`" 
   if ! grep -q "$MYXYRES" /tmp/Xmodes.txt ;then
    MYXRES="`echo -n "$CHOSENRES" | cut -f 1 -d 'x'`" 
    MYYRES="`echo -n "$CHOSENRES" | cut -f 2 -d 'x'`" 
    #this is an app that patches video-bios, but in ram only...
    915resolution 38 $MYXRES $MYYRES #...replaces mode 38 (1024x768).
    #create something for xwin to execute... actually, executing this in /etc/profile...
    echo "915resolution 38 $MYXRES $MYYRES" > /etc/resolutionfix
   fi
  else
   #in case plug usb or multisession-cd into different pc...
   [ -f /etc/resolutionfix ] && rm -f /etc/resolutionfix
  fi
 fi
fi

#cp -f /etc/X11/Xorg.conf-generic-laptop /etc/X11/xorg.conf
cp -f /etc/X11/xorg.conf-auto-pc /etc/X11/xorg.conf

#120329
REGEX4="s%.*#xkeymap0%	Option      \"XkbLayout\" \"${XKBLAYOUT}\" #xkeymap0%"
REGEX5="s%.*#xkbmodel0%	Option      \"XkbModel\" \"${XKBMODEL}\" #xkbmodel0%"
sed -i -e "$REGEX4" -e "$REGEX5" /etc/X11/xorg.conf
if [ "$XKBVARIANT" ];then
 REGEX6="s%.*#xkbvariant0%	Option      \"XkbVariant\" \"${XKBVARIANT}\" #xkbvariant0%"
 sed -i -e "$REGEX6" /etc/X11/xorg.conf
fi

if [ "$CHOSENRES" ];then
 REGEX1="s%.*#screen0modes%		Modes       \"${CHOSENRES}\" #screen0modes%"
 REGEX2="s%.*#monitor0prefmode%	Option      \"PreferredMode\" \"${CHOSENRES}\" #monitor0prefmode%"
 sed -i -e "$REGEX1" -e "$REGEX2" /etc/X11/xorg.conf
fi

#BUSID="`lspci | grep 'VGA compatible controller:' | cut -f 1 -d ' ' | tr -s '0' | tr '.' ':' | tr ':' '\n' | sed -e 's%^0%%' -e 's%^$%0%' | tr '\n' ':'`" #ex: 0:2:0
#if [ "$BUSID" ];then
# REGEX3="s%.*#card0busid%	BusID       \"PCI:${BUSID}\" #card0busid%"
#else
# REGEX3="s%.*#card0busid%	#card0busid%" #delete line, hope that xorg-server can find it.
#fi
#sed -i -e "$REGEX3" /etc/X11/xorg.conf

#110627 remove...
##for intel video, mplayer needs this...
#zVIDEODRVR="`grep '#card0driver' /etc/X11/xorg.conf | cut -f 2 -d '"'`" #'geany
#case $zVIDEODRVR in
# intel|i810)
#  [ "`grep '"LinearAlloc" "6144"' /etc/X11/xorg.conf`" = "" ] && sed -i -e 's%#card0driver%#card0driver\n\tOption "LinearAlloc" "6144"%' /etc/X11/xorg.conf
# ;;
#esac

TOUCHPAD='';  OLD3='Section "Monitor"'
[ "`grep 'Elantech' /proc/bus/input/devices`" ] && TOUCHPAD='Elantech'
[ "`grep 'Alps' /proc/bus/input/devices`" ] && TOUCHPAD='Alps'
[ "`grep 'Synaptics' /proc/bus/input/devices`" ] && TOUCHPAD='Synaptics'
if [ "$TOUCHPAD" ];then
 if [ "`lsmod | grep '^evdev'`" = "" ];then
  REGEXt0="s%^ADDLIST=.*%ADDLIST='${ADDLIST} evdev '%"
  sed -i -e "$REGEXt0" /etc/rc.d/MODULESCONFIG #so evdev module will load next bootup.
  modprobe evdev
 fi
 
 if vercmp $XORGVER lt 1.9.0; then #110701
  REGEXt1='s%.*#loadsynaptics%	Load "synaptics" #loadsynaptics%'
  REGEXt2='s%.*#serverlayoutsynaptics%	InputDevice "Synaptics Mouse" "AlwaysCore" #serverlayoutsynaptics%'
  sed -i -e "$REGEXt1" -e "$REGEXt2" /etc/X11/xorg.conf
  case $TOUCHPAD in
   Elantech) #jemimah
    NEW3='Section "InputDevice"\
	Identifier "Synaptics Mouse"\
	Driver "synaptics"\
	Option "Device" "/dev/psaux"\
	Option "Protocol" "auto-dev"\
        Option "LeftEdge" "60"\
        Option "RightEdge" "1070"\
        Option "TopEdge" "90"\
        Option "BottomEdge" "680"\
        Option "Emulate3Buttons" "on"\
        Option "MaxTapTime" "180"\
        Option "MaxTapMove" "59"\
        Option "VertScrollDelta" "20"\
        Option "MinSpeed" "0.75"\
        Option "MaxSpeed" "1.00"\
        Option "AccelFactor" "0.16"\
        Option "SHMConfig" "on"\
        Option "VertEdgeScroll" "on"\
        Option "TapButton1" "1"\
EndSection\
\
Section "Monitor"'
   ;;
   Alps) #kirk
    NEW3='Section "InputDevice"\
	Identifier "Synaptics Mouse"\
	Driver "synaptics"\
	Option "Device" "/dev/psaux"\
	Option "Protocol" "auto-dev"\
	Option "LeftEdge" "120"\
	Option "RightEdge" "930"\
	Option "TopEdge" "120"\
	Option "BottomEdge" "650"\
	Option "FingerLow" "14"\
	Option "FingerHigh" "15"\
	Option "MaxTapTime" "0"\
	Option "MaxTapMove" "110"\
	Option "VertScrollDelta" "20"\
	Option "MinSpeed" "0.3"\
	Option "MaxSpeed" "0.75"\
	Option "AccelFactor" "0.030"\
	Option "EdgeMotionMinSpeed" "200"\
	Option "EdgeMotionMaxSpeed" "200"\
	Option "UpDownScrolling" "1"\
	Option "CircularScrolling" "1"\
	Option "CircScrollDelta" "0.1"\
	Option "CircScrollTrigger" "2"\
	Option "SHMConfig" "on"\
EndSection\
\
Section "Monitor"'
   ;;
   Synaptics)
    NEW3='Section "InputDevice"\
	Identifier "Synaptics Mouse"\
	Driver "synaptics"\
	Option "Device" "/dev/psaux"\
	Option "Protocol" "auto-dev"\
	Option "LeftEdge" "1700"\
	Option "RightEdge" "5300"\
	Option "TopEdge" "1700"\
	Option "BottomEdge" "4200"\
	Option "FingerLow" "25"\
	Option "FingerHigh" "30"\
	Option "MaxTapTime" "0"\
	Option "MaxTapMove" "220"\
	Option "VertScrollDelta" "100"\
	Option "MinSpeed" "0.10"\
	Option "MaxSpeed" "0.30"\
	Option "AccelFactor" "0.0030"\
	Option "SHMConfig" "on"\
	#Option "Repeater" "/dev/ps2mouse"\
EndSection\
\
Section "Monitor"'
   ;; 
  esac
  sed -i -e "s%${OLD3}%${NEW3}%" /etc/X11/xorg.conf
 else
  #110913 create basic section...
  #120129 jemimah: add Option "SHMConfig" "on"...
  REGEXt1='s%.*#loadsynaptics%	Load "synaptics" #loadsynaptics%'
  REGEXt2='s%.*#serverlayoutsynaptics%	InputDevice "Synaptics Mouse" "AlwaysCore" #serverlayoutsynaptics%'
  NEW3='Section "InputDevice"\
	Identifier "Synaptics Mouse"\
	Driver "synaptics"\
	Option "SHMConfig" "on"\
EndSection\
\
Section "Monitor"'
  REGEXt3="s%${OLD3}%${NEW3}%"
  sed -i -e "$REGEXt1" -e "$REGEXt2" -e "$REGEXt3" /etc/X11/xorg.conf

 fi #110701 endif
 
fi

#want a hardware profile so can bootup on different PCs...
PROFILECHIP="`grep -m1 '^oem: ' /tmp/ddcprobe.txt | cut -f 2-4 -d ' ' | tr ' ' '_' | sed -e 's/[^0-9a-zA-Z]/_/g'`"
#'eisa:' and 'id:' return fluctuating values on some PCs, so search in this order...
PROFILEMONITOR="`grep '^monitorid: ' /tmp/ddcprobe.txt | head -n 1 | cut -f 2 -d ':' | tr -d ' ' | sed -e 's/[^0-9a-zA-Z]/_/g'`"
[ "$PROFILEMONITOR" = "" ] && PROFILEMONITOR="`grep '^monitorname: ' /tmp/ddcprobe.txt | head -n 1 | cut -f 2 -d ':' | tr -d ' ' | sed -e 's/[^0-9a-zA-Z]/_/g'`"
[ "$PROFILEMONITOR" = "" ] && PROFILEMONITOR="`grep '^monitorrange: ' /tmp/ddcprobe.txt | head -n 1 | cut -f 2 -d ':' | tr -d ' ' | sed -e 's/[^0-9a-zA-Z]/_/g'`"
echo "#PuppyHardwareProfile=${PROFILECHIP}${PROFILEMONITOR}" >> /etc/X11/xorg.conf
#create a copy of xorg.conf with the profile in the filename...
cp -af /etc/X11/xorg.conf /etc/X11/xorg.conf.${PROFILECHIP}${PROFILEMONITOR}

echo 
echo "Xorg Wizard Automatic has just run."
echo "If you are reading this, then you are at the console without X running."
echo "The usual thing now is to type 'xwin' to start X..."

###END###
