#!/bin/sh
#(c) Copyright Barry Kauler 2006,2007 www.puppylinux.com
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
## Abused by Dougal, Feb 2007
## Update: March 16th: fixed "OK" button when testing X
## Update: April 15th: fixed problem with MONTYPES format, added touchpad code
## Update: April 16th: removed MONTYPES, using a case-structure to set params
#v2.21 BK 9sept2007 bug fix: serial mouse setting was wrong.
#v3.93 BK 1dec2007: updated for dingo.
#v3.93 BK 2dec2007: bugfixes from Dougal and rerwin.
#v3.94 BK 24dec2007: recognise Classmate PC laptop.
#v3.95 BK 4jan2008: recognise eeepc laptop.
#v3.95 BK 8jan2008: bug that caused xorgwizard to run at every boot.
#v3.96 BK 22jan2008: special case submitted by rerwin.
#v3.97 BK 2feb2008: improved detection of Classmate laptop.
#v3.98 RE 7mar2008: added preferred mode parameter; corrected & added special cases submitted by rerwin.
#v3.99 BK 15apr2008: have restored the xrandrshell utility to the main GUI menu.
#v3.99 BK 15apr2008: workaround for xrandr and dual monitors.
#v3.99 RE 7apr2008: improve user interface for any Xvesa-hanging video adapters.

# redirect errors to file (to help debug):
exec 2>/tmp/xorgwizard-errors.log

PSFND="`ps`"
CURRENTX="`readlink /usr/X11R7/bin/X`"

#v2.0.0
choosemousefunc() {
 #assume kernel drivers already loaded. rc.local0 reliably detects usb mouse and loads driver,
 #ps/2 and serial drivers builtin to kernel.
 dialog --no-cancel --menu "マウスタイプを選択して下さい...\n(タイプは差込みプラグの形状で確認できます)\n上/下 矢印キーを使い、 ENTERキーで終了" 0 0 0 "ps/2" "(小さい直径8mmの緑色プラグ)" "usb" "(小さい11x4mm平らなプラグ)" "serial" "(大きい15x9mm DB9かDB25プラグ)" 2> /tmp/testmsx
 MOUSETYPE="`cat /tmp/testmsx`"
 MOUSEDEV="psaux"
 if [ "$MOUSETYPE" = "usb" ];then
  MOUSEDEV="input/mice"
  [ "`lsmod | grep '^usbhid'`" = "" ] && modprobe usbhid #v2.17
 fi
 if [ "$MOUSETYPE" = "serial" ];then
  dialog --no-cancel --menu "シリアルポートマウスが接続されています...\n上/下 矢印キーを使い、 ENTERキーで終了" 0 0 0 ttyS0 "COM1 (たいがい)" ttyS1 COM2 ttyS2 COM3 ttyS3 COM4 2> /tmp/testmsx
  MOUSEDEV="`cat /tmp/testmsx`"
 fi
 ln -sf /dev/$MOUSEDEV /dev/mouse
 echo -n "$MOUSEDEV" > /etc/mousedevice #note, rc.local0 will read this on next boot.
 #fix mouse protocol for Xorg...
 if [ -f /etc/X11/xorg.conf ];then
    case "$MOUSEDEV" in
     inpu*) #"input/mice", usb
      cat /etc/X11/xorg.conf | sed -e 's/\W\+Option\W\+"Protocol"\W\+"\w\+\W\+#mouse0protocol/	Option	    "Protocol" "IMPS\/2" #mouse0protocol/g' > /tmp/xorg.conf.tmp2
      ;;
     ttyS*) #ttyS0,1,2,3, serial
      cat /etc/X11/xorg.conf | sed -e 's/\W\+Option\W\+"Protocol"\W\+"\w\+\W\+#mouse0protocol/	Option	    "Protocol" "Microsoft" #mouse0protocol/g' > /tmp/xorg.conf.tmp2
      ;;
     *)
      cat /etc/X11/xorg.conf | sed -e 's/\W\+Option\W\+"Protocol"\W\+"\w\+\W\+#mouse0protocol/	Option	    "Protocol" "auto" #mouse0protocol/g' > /tmp/xorg.conf.tmp2
      ;;
    esac
    sync
    cp -f /tmp/xorg.conf.tmp2 /etc/X11/xorg.conf
    sync
    rm -f /tmp/xorg.conf.tmp2
 fi
}

#v2.0.0
#read the console keymap (country layout), apply to xkb...
CONSKEYMAP="`cat /etc/keymap | cut -b 1-2`"
[ "$CONSKEYMAP" = "" ] && CONSKEYMAP="us"
case $CONSKEYMAP in
 az) #azerty
  XKEYMAP="fr"
  ;;
 wa) #wangbe
  XKEYMAP="fr" #v2.13
  ;;
 dv) #dvorak
  XKEYMAP="dvorak"
  ;;
 cr) #croat
  XKEYMAP="cz"
  ;;
 sl) #slovene v2.12 bugfix, changed from 'sk' to 'si' (sk is slovakia)
  XKEYMAP="si"
  ;;
 uk) #united kingdom
  XKEYMAP="gb"
  ;;
 *)
  XKEYMAP="$CONSKEYMAP"
  ;;
esac



MAIN1="
<wtitle>Xorgビデオウィザード</wtitle>
<hbox>
 <vbox>
  <text><label>\"パピーリナックスXorgビデオウィザードに
ようこそ！\"</label></text>
  <text><label>\"Xorgの動作は環境設定ファイル
（/etc/X11/xorg.conf）で制御されています。
ここで2つの選択肢があります。
/etc/X11/xorg.conf ファイルを完全に
再設定するか、あるいは、
現在のファイルを変更するかどうかです。\"</label></text>
 </vbox>
 <vbox>
  <frame Xorgウィザード>
  <hbox>
   <text><label>Xorgウィザードは、 /etc/X11/xorg.confファイルを完全に再設定しますがXはこれだけでは実行されません。再起動が必要でXが立ち上がる前にテキストモードでウィザードが実行されます。</label></text>
   <vbox>
    <button>
     <input file>/usr/local/lib/X11/mini-icons/wizard16.xpm</input>
     <action>EXIT:11</action>
    </button>
   </vbox>
  </hbox>
  <text><label>注意：Xorgウィザードを手動で実行する事もできます。再起動しないでXからコマンドラインに戻り xorgwizardコマンドを実行します（シャットダウンメニューをご覧下さい）。</label></text>
  </frame>

  <frame xorg.confの編集>
   <hbox>
    <text><label>/etc/X11/xorg.confファイルを手動で編集できます。しかしXを終了させその後Xをリスタートさせる必要があります（シャットダウンメニューをご覧下さい）。編集するにはボタンをクリックして下さい:</label></text>
    <vbox>
     <button>
      <input file>/usr/local/lib/X11/mini-icons/mini-x.xpm</input>
      <action>EXIT:15</action>
     </button>
    </vbox>
   </hbox>
  </frame>

  <frame xvidtune>
   <hbox>
    <text><label>画面がずれたり幅や高さが違っている時は、xvidtune で修正できます。これは現在のxorg.confファイルを変更します。慎重にお使い下さい:</label></text>
    <vbox>
     <button>
      <input file>/usr/local/lib/X11/mini-icons/mini-x.xpm</input>
      <action>EXIT:13</action>
     </button>
    </vbox>
   </hbox>
  </frame>

  <frame マウス/キーボードウィザード>
   <hbox>
    <text><label>/etc/X11/xorg.confファイルには、マウスとキーボードの一般設定項目があります。ほとんどの場合そのままにしておくよう推奨されています。そのかわりマウス/キーボードウィザードをお使い下さい:</label></text>
    <vbox>
     <button>
      <input file>/usr/local/lib/X11/mini-icons/wizard16.xpm</input>
      <action>EXIT:16</action>
     </button>
    </vbox>
   </hbox>
  </frame>

  <frame モニタ・ガンマ補正>
   <hbox>
    <text><label>これは明るさも含めてモニタの色の調整をします。注意：設定が保存できない -- そういう方のためのプロジェクトがあります --  /usr/share/doc/tkgamma.txt をご覧下さい。</label></text>
    <vbox>
     <button>
      <input file>/usr/local/lib/X11/mini-icons/mini-x.xpm</input>
      <action>EXIT:17</action>
     </button>
    </vbox>
   </hbox>
  </frame>

  <frame 解像度チェンジャ>
   <hbox>
    <text><label>Xを終了しなくても画面の解像度を変更できます:</label></text>
    <vbox>
     <button>
      <input file>/usr/local/lib/X11/mini-icons/mini-x.xpm</input>
      <action>EXIT:18</action>
     </button>
    </vbox>
   </hbox>
  </frame>

 </vbox>
</hbox>
"


#do this code block if X is running...
if [ ! "`echo -n "$PSFND" | grep "\\.xinitrc"`" = "" ];then

 RETSTR="`echo "$MAIN1" | gtkdialog2 --stdin`"
 RETVAL="`echo "$RETSTR" | grep 'EXIT:' | cut -f 2 -d ':'`"

 case $RETVAL in
  11) #XorgWizard.
   #rm /etc/mousedevice # Dougal: any reason for this?
   mv -f /etc/X11/xorg.conf /etc/X11/xorg.conf.prev 2>/dev/null
   [ -f /usr/X11R7/bin/Xvesa ] && ln -sf Xvesa /usr/X11R7/bin/X
   NEXTWM="`cat /etc/windowmanager`"
   echo -n "$NEXTWM" > /etc/windowmanager #this makes change permanent.
   echo -n "$NEXTWM" > /tmp/wmexitmode.txt
   sync
   exec killall X
   ;;
  12) #xorgconfig
   xorgcfg
   xmessage -bg "#ff8080" -center -title "xorgcfg" " /etc/X11/xorg.conf の変更はXのリスタート後に有効になります。

XをリスタートするにはOKボタンをクリックして下さい..."
   exec restartwm
   ;;
  13) #xvidtune
   gxmessage -bg violet -title "Xvidtune 重要なヘルプ" "「Show」ボタンをクリックすると調整にした
設定は永久的に使いたいものになります。「Quit」ボタンを押すと変更を永久的にしない最後のチャンスが与えられます。
以下:
「Show」は設定を記録（xorg.confに最新包括するため）
「Quit」は Xvidtune を終了。

注意。Xvidtuneはすべてのビデオハードウェアで動きません。
すなわち設定を変更しても画面に変更がない事も起こります。" &
   XMSGPID=$!
   MODELINE0="`xvidtune | grep '^"[0-9]'`"
   kill $XMSGPID
   if [ ! "$MODELINE0" = "" ];then
    gxmessage -bg orange -title "Xvidtune: Modeline" -buttons Write:10,Quit:11 "新しいモードラインは:
$MODELINE0

注意。設定を有効にするにはXをリスタートしなければなりません。Xがおかしく
なったらコマンドラインで「mp /etc/X11/xorg.conf」として、「Monitor」
セクションの「UseModes」行を（削除しないで）コメントアウトします。

これを /etc/X11/xorg.conf に挿入するには「Write」ボタンをクリック...
xorg.conf を変更しないで終了するには「Quit」ボタンをクリック..."
    if [ $? -eq 10 ];then
     PATTERNA="s/.*#modes0modeline0/ ModeLine $MODELINE0 #modes0modeline0/g"
     cat /etc/X11/xorg.conf | sed -e "$PATTERNA" > /tmp/xorg.conf.new
     sync
     cat /tmp/xorg.conf.new | sed -e 's/#.*UseModes/UseModes/g' > /etc/X11/xorg.conf
     sync
    fi
   fi
   exit
   ;;
  15) #edit xorg.conf
   exec defaulttexteditor /etc/X11/xorg.conf
   ;;
  16) #mouse/keyboard wizard
   exec input-wizard
   ;;
  17) #tkgamma
   exec tkgamma
   ;;
  18) #resolution changer
   exec xrandrshell
   ;;
 esac
 exit
fi

#come here when X is not running, completely rebuild xorg.conf...
if [ -f /etc/X11/xorg.conf ];then
# echo
# echo "/etc/X11/xorg.conf exists. XorgWizard rebuilds it from scratch."
# echo -e "Hit ENTER key to quit, or type \"y\" then ENTER keys to rename"
# echo -n "xorg.conf to xorg.conf.bak and continue XorgWizard: "
# read yeahman
# if [ "$yeahman" = "" ];then
#  exit
# else
  mv -f /etc/X11/xorg.conf /etc/X11/xorg.conf.bak
# fi
fi

#comes here first time boot puppy, or whenever bootup
#and xorg.conf has been removed.

#need to create xorg.conf so that at subsequent boots, xwin will not run xorgwizard...
cp /etc/X11/xorg.conf0 /etc/X11/xorg.conf

#v3.99 begin
#If video adapter would hang Xvesa, tell user and allow only Xorg.
if [ -s /tmp/ddcprobeoem.txt ] || grep -q -f /etc/xorgoverrides /tmp/ddcprobe.txt 2>/dev/null;then   #Xvesa hanger encountered
 dialog --colors --title "Puppy Video Wizard" --yes-label "Xorg" --no-label "Commandline" --yesno "Welcome to the Puppy Video Wizard!

Puppy has two X servers (to run Puppy in graphics mode):
Xvesa: A very small and simple \"Kdrive\" X server.
Xorg:  A very large and sophisticated X server.

\Z1Your video hardware would cause Xvesa to freeze Puppy, so your options are to use Xorg or exit to the command prompt.\Z0

It is recommended that you choose the <Xorg> button now, but some
quirky video hardware does not work with Xorg, in which case you must operate from the command line.  Make sure you have at least 96 MB of RAM+swap, for Xorg to function.

Press ENTER for Xorg...
Press TAB (or right-arrow) key then ENTER key to use the command line...\Zn" 0 0
 [ ! $? -eq 0 ] && exit

else   #normal video adapter
#v3.99 end
 #offer to drop out to Xvesa...
 dialog --colors --title "Puppy Video Wizard" --yes-label "Xorg" --no-label "Xvesa" --yesno "Welcome to the Puppy Video Wizard!

Puppy has two X servers (to run Puppy in graphics mode):
Xvesa: A very small and simple \"Kdrive\" X server.
Xorg:  A very large and sophisticated X server.

Xvesa works flawlessly on most video hardware, simple to configure,
but one major disadvantage is that screen refresh rate is fixed.
Also, Xvesa has limited support for input devices, and lacks features
like hardware-acceleration (video may be jerky on slow PCs).

It is recommended that you choose the <Xorg> button now, but some
quirky video hardware does not work with Xorg, in which case you can
run this Wizard again and choose the <Xvesa> button.

Press ENTER for Xorg \Z1probing will take several seconds\Z0...
Press TAB (or right-arrow) key then ENTER key for Xvesa...\Zn" 0 0
 STATUS=$?  #v3.99
 [ $STATUS -eq -1 ] && exit   #v3.99 ESC to exit, same as above dialogs
 if [ $STATUS -eq 1 ];then   #v3.99 Xvesa selected
  ln -sf Xvesa /usr/X11R7/bin/X #Xvesa is now the default X server.
  #when start xorgwizard from commandline, it messes up xorg.conf...
  if [ -f /etc/X11/xorg.conf ];then
   if [ -f /etc/X11/xorg.conf.bak ];then
    #basic test to see that file has right stuff in it...
    if ! grep -F -q 'VertRefresh' /etc/X11/xorg.conf ;then
     cp -f /etc/X11/xorg.conf.bak /etc/X11/xorg.conf
    fi
   fi
  fi
  #choosemousefunc #v2.0.0
  #clear
  #echo "Type 'xwin' to start Xvesa graphics server."
  #exit
  exec xwin #v2.16beta
  #....Xvesa Wizard will run automatically if no /etc/videomode.
 fi
fi   #v3.99

#resolution changer uses this. it must be removed here...
rm -f /etc/xrandrindex 2> /dev/null

ln -sf Xorg /usr/X11R7/bin/X

# Dougal: moved echo to below xorg probing, since loop isn't slow anymore...
clear
echo -en "\\033[1;35m" #34=blue, 33=yellow, 32=green, 31=red, 35=purple, 36=aquablue, 38=black.
echo -n "Probing video hardware, please wait..."
echo -e "\\033[0;39m"
#autoprobe...
Xorg -configure > /tmp/xorgprobe.txt 2>&1
#... config in /root/xorg.conf.new


##### Below commented out by Dougal:

#echo '.' #progress indicator completion.
#CARD0DRIVER="`cat /tmp/CARD0DRIVER`"
#CARD0VENDOR="`cat /tmp/CARD0VENDOR`"
#CARD0BOARD="`cat /tmp/CARD0BOARD`"
#HORIZSYNC0="`cat /tmp/HORIZSYNC0`"
#VERTREFRESH0="`cat /tmp/VERTREFRESH0`"
##now have an almost-working xorg.conf, missing Screen section.
###############################END BIG ITERATION###########################


## My (Dougal) alternate code:
######################################################################
#  Rather than pipe data to "while read", I use "here document" (at bottom),
#+ so that the loop doesn't run in a subshell -- hence variables 
#+ (like HORIZSYNC0) are retained after the loop has ended...
while read ASECTION
do
 # this (outer) loop recieves a line containing **an entire section**
 # filter out entire sections we don't want
 case $ASECTION in
  *Section?\"Screen\"*)
   break #Screen section is created further down.
  ;;
  *Section?\"Files\"*|*Section?\"Module\"*|*Section?\"ServerFlags\"*)
   continue #these sections pre-created in /etc/X11/xorg.conf0
  ;;
 esac
 #  Params are retained for the entire ASECTION loop.
 #  I use TEMPSECTION since BASH is slow with file i/o -- so echo
 #+ only once for each section.
 #  Tabs at beginning of lines are lost when reading, so I add them
 #+ where appropriate (i.e. before every line except for Section borders).
 #+ ("\n"=newline ; "\t"=tab)
 IDENTIFIER="" ; XSECTION="" ; TEMPSECTION=""
 while read ALINE
 do
  case "$ALINE" in 
   EndSection*) 
    if [ "$XSECTION" = "InputDevice" -a "$IDENTIFIER" = "Keyboard0" ] ; then
     TEMPSECTION="$TEMPSECTION
	Option      \"XkbRules\" \"xorg\"
	Option      \"XkbModel\" \"pc102\"
	Option      \"XkbLayout\" \"$XKEYMAP\" #xkeymap0"
    elif [ "$XSECTION" = "Monitor" -a "$IDENTIFIER" = "Monitor0" ] ; then
     TEMPSECTION="$TEMPSECTION
	HorizSync    HORIZSYNC0
	VertRefresh  VERTREFRESH0
	#UseModes     \"Modes0\" #monitor0usemodes
	Option      \"PreferredMode\" \"PREFERREDMODE0\"
	$ALINE
	
Section \"Modes\"
	Identifier \"Modes0\"
	#modes0modeline0
EndSection"
     #v3.98 rerwin - added preferredmode line, after usemodes line.
     continue # we added the line before the "Modes" section, so continue
    fi
    TEMPSECTION="$TEMPSECTION\n$ALINE"
    continue # just added line -- no need to get to bottom
    ;;
   Section?\"*) XSECTION=${ALINE%\"*} ; XSECTION=${XSECTION##*\"}
    TEMPSECTION="\n$ALINE" 
    continue # just added line -- no need to get to bottom
    ;;
   *Identifier*) IDENTIFIER=${ALINE%\"*} ; IDENTIFIER=${IDENTIFIER##*\"} 
    ;; 
   *\"ZAxisMapping\"*) continue 
    ;;
   *HorizSync*[0-9]*) set -- $ALINE ; HORIZSYNC0="$2$3$4" ; continue
    ;; 
   *VertRefresh*[0-9]*) set -- $ALINE ; VERTREFRESH0="$2$3$4" ; continue
    ;;
   *Option*\"Protocol\"?\"auto\"*) 
    case "`cat /etc/mousedevice 2>/dev/null`" in 
     input/mice) TEMPSECTION="$TEMPSECTION
	Option	    \"Protocol\" \"IMPS/2\" #mouse0protocol" 
	  continue
	  ;;
     *ttyS*) TEMPSECTION="$TEMPSECTION
	Option	    \"Protocol\" \"Microsoft\" #mouse0protocol"
	  continue
	  ;;
     *) TEMPSECTION="$TEMPSECTION
	Option	    \"Protocol\" \"auto\" #mouse0protocol"
	  continue
	  ;;
    esac
    ;;
   */dev/mouse*|*/dev/input/mice*) #v2.21
    TEMPSECTION="$TEMPSECTION\n\t$ALINE"
    case "`cat /etc/xextraoptions`" in 
     *2button*) BLA="" ;;
	 *) BLA="#" ;;
	esac
	TEMPSECTION="$TEMPSECTION
	${BLA}Option      \"Emulate3Buttons\"
	${BLA}Option      \"Emulate3Timeout\" \"50\""
	if [ "`cat /etc/mousebuttons`" = "5" ] ; then #scrollwheel
	 BLA=""
	else
	 BLA="#"
	fi
    TEMPSECTION="$TEMPSECTION
	${BLA}Option      \"ZAxisMapping\" \"4 5\" #scrollwheel"
	continue # we added the line at the beginning
    ;;	 
  esac
  
  if [ "$XSECTION" = "Device" -a "$IDENTIFIER" = "Card0" ] ; then
   FBODY=${ALINE%\"*} ; FBODY=${FBODY##*\"}
   case "$ALINE" in 
    *Driver*) CARD0DRIVER="$FBODY" ; ALINE="$ALINE #card0driver" ;;
    *VendorName*) CARD0VENDOR="$FBODY" ;;
    *BoardName*) CARD0BOARD="$FBODY" ;;
    *Screen1*Right?Of*Screen0*) ALINE="#$ALINE" ;;
   esac
  fi
  # add line to section (if we've come this far...)
  TEMPSECTION="$TEMPSECTION\n\t$ALINE"
 done <<_END2
$(echo "$ASECTION" | tr '%' '\n')
_END2
echo -e "$TEMPSECTION" >> /etc/X11/xorg.conf
sync
done <<_END1
$(cat /root/xorg.conf.new | tr '\n' '%' | sed 's/EndSection/EndSection\n/g' )
_END1
###################################################################

#v2.21 quick hack...
sed -e 's%/dev/input/mice%/dev/mouse%'  /etc/X11/xorg.conf >/tmp/xorgconfmousefix
mv -f /tmp/xorgconfmousefix /etc/X11/xorg.conf

#echo "Probing the video card and monitor..."
#code below is not distinguishing multiple cards/monitors...
HORIZSYNC="$HORIZSYNC0"
VERTREFRESH="$VERTREFRESH0"

#want to choose a resolution and color-depth...
##sometimes get bogus '1x' or '0x' pixel horiz resolution...
#DDCPROBE="`ddcprobe | grep -v 'timing: [0123456789]x'`"
#DDCPROBE="`ddcprobe`"
ddcprobe >/tmp/ddcprobe.txt
#weird (ddcprobe is very weird), on some hardware, edid monitor probe fails
#every alternate run of ddcprobe...
case "`cat /tmp/ddcprobe.txt`" in *edidfail*)
 sleep 1
 ddcprobe >/tmp/ddcprobe.txt
 ;;
esac

#v3.93 rerwin: For some Tecra and Portege laptops, avoid an Xvesa hang by skipping "listmodes"
#v3.96 added trident line...
#v3.98 added more CYBER lines & corrected case
XVESALISTOK="yes"
TIMINGOK="yes"
if [ -s /tmp/ddcprobeoem.txt ];then   #v3.99 begin
 if [ ! -f /etc/xorgoverrides ] || ! grep -q -f /tmp/ddcprobeoem.txt /etc/xorgoverrides;then
  echo "`cat /tmp/ddcprobeoem.txt`" >> /etc/xorgoverrides  #append new signature if user canceled out from hung Xvesa
  sync
 fi
fi
if [ -f /etc/xorgoverrides ] && grep -q -f /etc/xorgoverrides /tmp/ddcprobe.txt;then   #v3.99 end
 #HORIZSYNC=""
 #VERTREFRESH=""
 XVESALISTOK="no" #'Xvesa -listmodes' hangs.
 TIMINGOK="no"    #ddcprobe returns incomplete 'timing:' info (res's missing).
 [ "`grep 'mode: 640x480x64k' /tmp/ddcprobe.txt`" = "" ] && echo 'mode: 640x480x64k' >> /tmp/ddcprobe.txt
 [ "`grep 'mode: 640x480x16m' /tmp/ddcprobe.txt`" = "" ] && echo 'mode: 640x480x16m' >> /tmp/ddcprobe.txt
 [ "`grep 'mode: 800x600x64k' /tmp/ddcprobe.txt`" = "" ] && echo 'mode: 800x600x64k' >> /tmp/ddcprobe.txt
 [ "`grep 'mode: 800x600x16m' /tmp/ddcprobe.txt`" = "" ] && echo 'mode: 800x600x16m' >> /tmp/ddcprobe.txt
 [ "`grep 'mode: 1024x768x64k' /tmp/ddcprobe.txt`" = "" ] && echo 'mode: 1024x768x64k' >> /tmp/ddcprobe.txt
 [ "`grep 'mode: 1024x768x16m' /tmp/ddcprobe.txt`" = "" ] && echo 'mode: 1024x768x16m' >> /tmp/ddcprobe.txt
fi   #v3.93 end

#v3.94 detect if a Classmate PC laptop with 800x480 screen...
#if [ "`grep 'edidfail' /tmp/ddcprobe.txt`" != "" ];then
# if [ "`cat /tmp/ddcprobe.txt | tr -s ' ' | grep 'oem: Intel(r)915GM/910ML/915MS Graphics'`" != "" ];then
#  if [ "`dmesg | grep 'CPU0: Intel(R) Celeron(R) M processor          900MHz'`" != "" ];then
#   if [ "`dmesg | grep 'uDiskOnChip'`" != "" ];then
#    #yep, looks like this is a Classmate model with 800x480...
#    echo 'timing: 800x480@60' >> /tmp/ddcprobe.txt
#   fi
#  fi
# fi
#fi
#v3.97 improved method: CM=Classmate, 915=chip 10=generation#...
[ "`dmidecode -s bios-version | grep '^CM91510'`" != "" ] && echo 'timing: 800x480@60' >> /tmp/ddcprobe.txt
#v3.95 recognise more special screens...
DMIDECODE="`dmidecode`"
#recognise EeePC Asus laptop...
[ "`echo "$DMIDECODE" | grep 'Product Name: 701'`" != "" ] && [ "`echo "$DMIDECODE" | grep 'Serial Number: EeePC'`" != "" ] && echo 'timing: 800x480@60' >> /tmp/ddcprobe.txt


#modes supported by card...
CARDCHOICES=""
if [ -f /usr/X11R7/bin/Xvesa -a "$XVESALISTOK" = "yes" ];then #format of each line: 1024x768x16
 echo -e '\nIf initialization freezes here, press ctrl-c and type "xorgwizard",\nselect "Xorg", choose monitor type and choose video mode.'   #v3.99
 grep '^oem:' /tmp/ddcprobe.txt > /tmp/ddcprobeoem.txt   #v3.99 current adapter signature
 Xvesa -listmodes 2>&1 | cut -f 2 -d ' ' | grep --extended-regexp 'x16$|x24$' | sort --field-separator=x --numeric-sort > /tmp/Xmodes.txt
 rm /tmp/ddcprobeoem.txt  #v3.99 didn't hang in Xvesa, so delete signature
else
 #the vesa 'mode:' lines returned by ddcprobe are very limited...
 grep '^mode:' /tmp/ddcprobe.txt | grep -E 'x64k|x16m'| cut -f 2 -d ' ' | sed -e 's/x64k/x16/g ; s/x16m/x24/g' > /tmp/Xmodes.txt
fi
sync
CARDCHOICES="`cat /tmp/Xmodes.txt`"

#frequencies supported by monitor...
#Xorg probe may already have got this information...
if [ "$HORIZSYNC" = "" -o "$VERTREFRESH" = "" ];then
 #one or both no good, so set both no good...
 HORIZSYNC=""
 VERTREFRESH=""
else
 #both have values, but are they 'valid'? Xorg probe sometimes returns bogus values...
 BOGUSVAL="no"
 for ONEVAL in `echo -n "$HORIZSYNC" | tr '\-' ' ' | tr ',' ' ' | tr -s ' '`
 do
  [ ${ONEVAL%.*} -lt 10 ] && BOGUSVAL="yes"
  [ ${ONEVAL%.*} -gt 200 ] && BOGUSVAL="yes"
 done
 for ONEVAL in `echo -n "$VERTREFRESH" | tr '\-' ' ' | tr ',' ' ' | tr -s ' '`
 do
  [ ${ONEVAL%.*} -lt 10 ] && BOGUSVAL="yes"
  [ ${ONEVAL%.*} -gt 200 ] && BOGUSVAL="yes"
 done
 if [ "$BOGUSVAL" = "yes" ];then
  #one or both no good, so set both no good...
  HORIZSYNC=""
  VERTREFRESH=""
 fi
fi

MONCHOICES=""
MONCUTXY=""
if [ "$TIMINGOK" = "yes" ];then #v3.93
 #modes supported by monitor... format of each line: 1024x768@75
 #MONCHOICES="`echo "$DDCPROBE" | grep 'timing:' | cut -f 2 -d ' ' | grep -v 'Apple' | grep -v '8514A' | grep -v '(XGA2)' | sort -u`"
 #get too many weird resolutions, maybe better just only allow these...
 MONCHOICES="`grep -F 'timing:' /tmp/ddcprobe.txt | grep -E -v 'Apple|8514A|Mac II|\(XGA2\)' | grep -E ' 640x480@| 800x480@| 800x600@| 1024x768@| 1280x768@| 1280x800@| 1280x1024@| 1360x768@| 1400x1050@| 1440x900@| 1600x1200@| 1680x1050@| 1920x1080@| 1920x1200@' | cut -f 2 -d ' ' | sort -u`"
 #no, better if reject known weird resolutions...
 #MONCHOICES="`echo "$DDCPROBE" | grep 'timing:' | cut -f 2 -d ' ' | grep -v --extended-regexp 'Apple|8514A|XGA2|720x400|832x624|256x256|248x248|632x632|280x280' | sort -u`"
 #...no, ddcprobe is buggy, returns random resolutions, cannot screen all out!
 MONCUTXY="`echo "$MONCHOICES" | cut -f 1 -d '@' | sort -u`" #each line: 1024x768
fi

#if Xorg probe failed to determine monitor freq, try ddcprobe...
if [ "$HORIZSYNC" = "" -o "$VERTREFRESH" = "" ];then
 MONFREQ="`grep -F 'monitorrange:' /tmp/ddcprobe.txt | cut -f 2-10 -d ' '`"
 HORIZSYNC="`echo -n "$MONFREQ" | cut -f 1 -d ','`"
 VERTREFRESH="`echo -n "$MONFREQ" | cut -f 2 -d ',' | tr -d ' '`"
fi

#ddcprobe on my laptop returns one 'timing:' line but no 'monitorrange:', so
#can fall back to calculating freqs...
if [ ! "$MONCHOICES" = "" ];then
 ##hmmm, maybe always use the freqs of the 'timing:' lines rather than the
 ##'monitorrange:' as it always seems to be too high...
 if [ "$HORIZSYNC" = "" -o "$VERTREFRESH" = "" ];then
  UHORIZSYNC=32    #start with mediocre values.
  UVERTREFRESH=60  # /
  LHORIZSYNC=30    # /
  LVERTREFRESH=56  # /
  for ONECH in $MONCHOICES #format: 1024x768@75
  do
   ONEVERT=${ONECH#*@}
   ONEX=${ONECH%x*}
   ONEY=${ONECH#*x} ; ONEY=${ONEY%@*}
   ONEHORIZ=`gtf $ONEX $ONEY $ONEVERT | grep -o 'hsync:.*' | cut -f 2 -d ' ' | cut -f 1 -d '.'`
   [ $ONEVERT -lt $LVERTREFRESH ] && LVERTREFRESH=$ONEVERT
   [ $ONEHORIZ -gt $UHORIZSYNC ] && UHORIZSYNC=$ONEHORIZ
   #it seems ddcprobe truncates any fractional part off vertrefresh...
   ONEVERT=`expr $ONEVERT + 1`
   ONEHORIZ=`gtf $ONEX $ONEY $ONEVERT | grep -o 'hsync:.*' | cut -f 2 -d ' ' | cut -f 1 -d '.'`
   [ $ONEVERT -gt $UVERTREFRESH ] && UVERTREFRESH=$ONEVERT
   [ $ONEHORIZ -lt $LHORIZSYNC ] && LHORIZSYNC=$ONEHORIZ
  done
  #v3.93 hmmm, above still can round down too much, add 2...
  UHORIZSYNC=`expr $UHORIZSYNC + 2`
  HORIZSYNC="${LHORIZSYNC}-${UHORIZSYNC}"
  VERTREFRESH="${LVERTREFRESH}-${UVERTREFRESH}"
 fi
fi


RECMODE="800x600x16" #default recommended mode.

#if ddcprobe also failed, have to ask monitor type...
if [ "$HORIZSYNC" = "" -o "$VERTREFRESH" = "" ];then #"" if monitor not plug-n-play.
 ## Dougal: (v2.16??) removed MONTYPES param, added case-structure below
 ##  Dougal: modified formatting of list: old tag format used to give problems, 
 ##+ since we had the same tag for different options! (eg. "h31.5-90v60")
 MONCHOICES=""
 MONCUTXY=""
 while [ 1 ];do
  #need a dialog, ask generic monitor type...
  dialog --no-collapse --no-cancel --title "Puppy Video Wizard" --menu "Automatic probing of your monitor was unsuccessful, so you now need\nto choose from a list of generic monitor types.\nChoose the highest specification that describes your monitor.\nLCD: Liquid Crystal Display.\nCRT: Cathode Ray Tube (normal monitor).\nChoose \"Z\" if you have the monitor user manual, and it has the\nhorizontal and vertical frequency specifications.\nDOWN-ARROW to highlight choice, ENTER key to finish..."  0 0 0 \
 "A" "h31.5v40-70		LCD Panel 640x480" \
 "B" "h31.5-37v40-70		LCD Panel 800x600" \
 "C" "h31.5-48.5v40-70	LCD Panel 1024x768" \
 "D" "h31.5-90v60		LCD Panel 1280x800" \
 "E" "h31.5-67v50-75		LCD Panel 1280x1024" \
 "F" "h31.5-90v60		LCD Panel 1360x768" \
 "G" "h31.5-90v59-75		LCD Panel 1400x1050" \
 "H" "h31.5-100v59-75	LCD Panel 1440x900" \
 "I" "h31.5-90v60		LCD Panel 1600x1200" \
 "J" "h31.5-90v60		LCD Panel 1680x1050" \
 "K" "h30-68v67		LCD HDTV  1920x1080" \
 "L" "h31.5-90v60		LCD Panel 1920x1200" \
 "M" "h31.5v50-61		CRT 640x480" \
 "N" "h31.5-35v50-61		CRT 800x600 (old 14inch monitor)" \
 "O" "h31.5-48.5v56-72	CRT 1024x768 (basic 15inch monitor)" \
 "P" "h31.5-79v50-90		CRT 1280x1024 (basic 17inch monitor)" \
 "Q" "h31.5-90v50-90		CRT 1400x1050" \
 "R" "h31.5-94v50-90		CRT 1600x1200" \
 "Z" "SELECT THIS IF YOU KNOW MONITOR FREQUENCY SPECS" \
  2>/tmp/taggen.txt
  CHOICE="`cat /tmp/taggen.txt 2>/dev/null`"
  [ "$CHOICE" = "" ] && continue
  ## Dougal: use case-structure for setting params -- safer than the old way
  ## (not sure why Barry only uses 16-bit, maybe worth improving?)
  case "$CHOICE" in
   A) # "h31.5v40-70		LCD Panel 640x480"
     RECMODE="640x480x16"
     HORIZSYNC="31.5"
     VERTREFRESH="40-70"
     ;;
   B) # "h31.5-37v40-70		LCD Panel 800x600"
     RECMODE="800x600x16"
     HORIZSYNC="31.5-37"
     VERTREFRESH="40-70"
     ;;
   C) # "h31.5-48.5v40-70	LCD Panel 1024x768"
     RECMODE="1024x768x16"
     HORIZSYNC="31.5-48.5"
     VERTREFRESH="40-70"
     ;;
   D) # "h31.5-90v60		LCD Panel 1280x800"
     RECMODE="1280x800x16"
     HORIZSYNC="31.5-90"
     VERTREFRESH="60"
     ;;
   E) # "h31.5-67v50-75		LCD Panel 1280x1024"
     RECMODE="1280x1024x16"
     HORIZSYNC="31.5-67"
     VERTREFRESH="50-75"
     ;;
   F) # "h31.5-90v60		LCD Panel 1360x768"
     RECMODE="1360x768x16"
     HORIZSYNC="31.5-90"
     VERTREFRESH="60"
     ;;
   G) # "h31.5-90v59-75		LCD Panel 1400x1050"
     RECMODE="1400x1050x16"
     HORIZSYNC="31.5-90"
     VERTREFRESH="59-75"
     ;;
   H) # "h31.5-100v59-75	LCD Panel 1440x900"
     RECMODE="1440x900x16"
     HORIZSYNC="31.5-100"
     VERTREFRESH="59-75"
     ;;
   I) # "h31.5-90v60		LCD Panel 1600x1200"
     RECMODE="1600x1200x16"
     HORIZSYNC="31.5-90"
     VERTREFRESH="60"
     ;;
   J) # "h31.5-90v60		LCD Panel 1680x1050"
     RECMODE="1680x1050x16"
     HORIZSYNC="31.5-90"
     VERTREFRESH="60"
     ;;
   K) # "h30-68v67		LCD HDTV  1920x1080"
     RECMODE="1920x1080x16"
     HORIZSYNC="30-68"
     VERTREFRESH="67"
     ;;
   L) # "h31.5-90v60		LCD Panel 1920x1200"
     RECMODE="1920x1200x16"
     HORIZSYNC="31.5-90"
     VERTREFRESH="60"
     ;;
   M) # "h31.5v50-61		CRT 640x480"
     RECMODE="640x480x16"
     HORIZSYNC="31.5"
     VERTREFRESH="50-61"
     ;;
   N) # "h31.5-35v50-61		CRT 800x600 (old 14inch monitor)"
     RECMODE="800x600x16"
     HORIZSYNC="31.5-35"
     VERTREFRESH="50-61"
     ;;
   O) # "h31.5-48.5v56-72	CRT 1024x768 (basic 15inch monitor)"
     RECMODE="1024x768x16"
     HORIZSYNC="31.5-48.5"
     VERTREFRESH="56-72"
     ;;
   P) # "h31.5-79v50-90		CRT 1280x1024 (basic 17inch monitor)"
     RECMODE="1280x1024x16"
     HORIZSYNC="31.5-79"
     VERTREFRESH="50-90"
     ;;
   Q) # "h31.5-90v50-90		CRT 1400x1050"
     RECMODE="1400x1050x16"
     HORIZSYNC="31.5-90"
     VERTREFRESH="50-90"
     ;;
   R) # "h31.5-94v50-90		CRT 1600x1200"
     RECMODE="1600x1200x16"
     HORIZSYNC="31.5-94"
     VERTREFRESH="50-90"
     ;;
   Z) # "SELECT THIS IF YOU KNOW MONITOR FREQUENCY SPECS"
     dialog --title "Xorg Video Wizard" --inputbox "Please enter the horizontal frequency range of the monitor, in KHz. An example is given:" 0 0 "31.5-48.5" 2>/tmp/taghoriz.txt
     dialog --title "Xorg Video Wizard" --inputbox "Please enter the vertical frequency range of the monitor, in Hz. An example is given:" 0 0 "56-72" 2>/tmp/tagvert.txt
     HORIZSYNC="`cat /tmp/taghoriz.txt 2>/dev/null`"
     VERTREFRESH="`cat /tmp/tagvert.txt 2>/dev/null`"
     [ "$HORIZSYNC" = "" ] && continue
     [ "$VERTREFRESH" = "" ] && continue
     # comment out -- it's the default from above
     #RECMODE="800x600x16"
     ;;
  esac
  break
 done
fi


#if have MONCHOICES, that's all i want to display (with warning if not in card list).
#if no MONCHOICES, then display card list only...
echo -n "" > /tmp/finalchoices.txt
if [ "$MONCHOICES" = "" ];then
 for ONECHOICE in $CARDCHOICES
 do
  if [ "$RECMODE" = "$ONECHOICE" ];then
   echo "$ONECHOICE \"Unconfirmed MAXIMUM for monitor, OK for card\" \\" >> /tmp/finalchoices.txt
  else
   echo "$ONECHOICE \"Unconfirmed for monitor, OK for video card\" \\" >> /tmp/finalchoices.txt
  fi
 done
 for ONECHOICE in 640x480x16 800x600x16 1024x768x16 1280x800x16 1280x1024x16 1360x768x16 1400x1050x16 1440x900x16 1600x1200x16 1680x1050x16 1920x1080x16 1920x1200x16 640x480x24 800x600x24 1024x768x24 1280x800x24 1280x1024x24 1360x768x24 1400x1050x24 1440x900x24 1600x1200x24 1680x1050x24 1920x1080x24 1920x1200x24
 do
  case "$CARDCHOICES" in *$ONECHOICE*) ;; *) # we've done CARDCHOICES above
   echo "$ONECHOICE \"Unconfirmed for monitor, maybe NOT OK for card\" \\" >> /tmp/finalchoices.txt ;;
  esac
 done
else
 #decide on a default... (already 800x600x16 earlier) no, reset...
 RECMODE=""
 case "$MONCUTXY" in
  *1024x768*) RECMODE="1024x768x16" ;;
  *800x600*) RECMODE="800x600x16" ;;
 esac
 for ONECUTXY in $MONCUTXY #1024x768
 do
  for ONECHOICE in ${ONECUTXY}x16 ${ONECUTXY}x24
  do
   case "$CARDCHOICES" in
    *$ONECHOICE*) echo "$ONECHOICE \"OK for monitor, OK for video card\" \\" >> /tmp/finalchoices.txt ;;
    *) echo "$ONECHOICE \"OK for monitor, maybe NOT OK for card\" \\" >> /tmp/finalchoices.txt ;;
   esac 
  done
 done
fi

#FINALCHOICES="`cat /tmp/finalchoices.txt`"
FINALCHOICES="`cat /tmp/finalchoices.txt | sort --field-separator=x --numeric-sort`" #v3.93 rerwin 

echo '#!/bin/sh' > /tmp/choosexmodedlg
echo -n "dialog --colors --extra-button --aspect 11 --default-item $RECMODE" >> /tmp/choosexmodedlg
echo ' --no-collapse --title "Puppy Video Wizard" --ok-label "OK" --extra-label "TEST" --cancel-label "EXIT" --menu "Please use the UP/DOWN ARROW keys to select a video mode. Choices are
shown in the format WidthxHeightxBits, where Width and Height are
screen resolution in pixels, 16/24 bits = 65,536/16,777,216 colors.
' >> /tmp/choosexmodedlg
[ ! "$RECMODE" = "" ] && echo "CANNOT DECIDE? ...${RECMODE} is suggested as a good choice."  >> /tmp/choosexmodedlg
echo '\Z1RECOMMENDATION: Choose <TEST> button to verify it works!\Z0' >> /tmp/choosexmodedlg
echo '
OK:     Just press ENTER key to immediately use the selected mode...
TEST: TAB key then ENTER to test and debug the mode before using it...
EXIT: Exit to text-mode console, do not start X...\Zn" 0 0 0 \' >> /tmp/choosexmodedlg #'geanyfix.
echo "$FINALCHOICES" >> /tmp/choosexmodedlg
echo ' 2>/tmp/tag.txt' >> /tmp/choosexmodedlg
echo 'exit $?' >> /tmp/choosexmodedlg

chmod 755 /tmp/choosexmodedlg
/tmp/choosexmodedlg
XAUTO=$? #=0 for automatic.
#[ $XAUTO -ne 0 ] && [ $XAUTO -ne 3 ] && exit #drop out to console.
#if [ $XAUTO -ne 0 ];then
# if [ $XAUTO -ne 3 ];then
#  #drop out to console...
#  #the script has created /etc/X11/xorg.conf without Screen section...
#  [ -f /etc/X11/xorg.conf.bak ] && cp -f /etc/X11/xorg.conf.bak /etc/X11/xorg.conf
#  exit
# fi
#fi
CHOSENRES="`cat /tmp/tag.txt 2>/dev/null`"
[ "$CHOSENRES" = "" ] && CHOSENRES="800x600x16"
FINALDEPTH=${CHOSENRES##*x}
#CHOSENX=${CHOSENRES%%x*}
#CHOSENY=${CHOSENRES#*x} ; CHOSENY=${CHOSENY%x*}
#FINALXY="${CHOSENX}x${CHOSENY}"
FINALXY=${CHOSENRES%x*}

##lcd panels usually work at one freq only...
#if [ ! "$MONCHOICES" = "" ];then #format of each line: 1024x768@75
# FIXEDFREQS="`echo -n "$MONCHOICES" | grep "$FINALXY" | cut -f 2 -d '@' | tr '\n' ','' | sed -e 's/,$//g'`"
# [ ! "$FIXEDFREQS" = "" ] && VERTREFRESH="$FIXEDFREQS"
#fi

#some monitors have a very high vertrefresh, and Xorg uses it, but it is too high...
#v2.13b2 was 95, make it 85...
##  Dougal: note that old code (line below) didn't work in my case: 50.0-160.0
##+ seems like the ".0" gave it problems, so ended up with MAXVERT=""
#MAXVERT=`echo -n "$VERTREFRESH" | grep -o '[ -,][0-9]*$' | sed -e 's/^[ -,]//g'`
MAXVERT=${VERTREFRESH#*-} ; MAXVERT=${MAXVERT%.*}
if [ $MAXVERT -gt 85 ];then
 #VPATTERN="s/${MAXVERT}$/85/g"
 #VERTREFRESH="`echo -n "$VERTREFRESH" | sed -e "$VPATTERN"`"
 VERTREFRESH=${VERTREFRESH/$MAXVERT/85}
else
 #want to cut down rating on modern LCD panels also...
 if [ $MAXVERT -gt 74 ] && [ $MAXVERT -lt 86 ];then
   #and if manufactured in 2004 or later...
   #(so older CRTs in this range maxvert 75-85 will get rejected)
   if grep '^manufacture:' /tmp/ddcprobe.txt | grep -E -q ' 20[123][0-9]| 200[4-9]' ;then
    let LCDMAXVERT=MAXVERT-4
    #VPATTERN="s/${MAXVERT}$/${LCDMAXVERT}/g"
    #VERTREFRESH="`echo -n "$VERTREFRESH" | sed -e "$VPATTERN"`"
    VERTREFRESH=${VERTREFRESH/$MAXVERT/$LCDMAXVERT}
   fi
 fi
fi
#script also needs these max freqs later on...
#MAXHORIZ=`echo -n "$HORIZSYNC" | grep -o '[ -,][0-9]*$' | sed -e 's/^[ -,]//g'`
#MINVERT=`echo -n "$VERTREFRESH" | grep -o '^[0-9]*'`
#MINHORIZ=`echo -n "$HORIZSYNC" | grep -o '^[0-9]*'`
MAXHORIZ=${HORIZSYNC#*-} #; MAXHORIZ=${MAXHORIZ%.*}
MINVERT=${VERTREFRESH%-*} #; MINVERT=${MINVERT%.*}
MINHORIZ=${HORIZSYNC%-*} #; MINHORIZ=${MINHORIZ%.*}

#now write the Screen section...
SCRNSECTION="
Section \"Screen\"
	Identifier \"Screen0\"
	Device     \"Card0\"
	Monitor    \"Monitor0\"
    DefaultDepth $FINALDEPTH
    Subsection \"Display\"
        Depth       $FINALDEPTH
        Modes       \"${FINALXY}\"
    EndSubsection
EndSection
"
echo "$SCRNSECTION" >> /etc/X11/xorg.conf

PREFERREDMODE=$FINALXY   #v3.98 rerwin - set monitor preferred mode same as screen mode.
#horiz and vert sync ranges and preferred mode were found earlier but not written... v3.98
cp -f /etc/X11/xorg.conf /tmp/xorg.conf.tmp
#APATTERN="s/HORIZSYNC0/$HORIZSYNC/g"
sed -e "s/HORIZSYNC0/$HORIZSYNC/g ; s/VERTREFRESH0/$VERTREFRESH/g ; s/PREFERREDMODE0/$PREFERREDMODE/g" /tmp/xorg.conf.tmp >/etc/X11/xorg.conf   #v3.98 rerwin - added update of preferredmode.
#BPATTERN="s/VERTREFRESH0/$VERTREFRESH/g"
sync
#cat /tmp/xorg.conf.tmp2 | sed -e "$BPATTERN" > /etc/X11/xorg.conf
#sync
#rm -f /tmp/xorg.conf.tmp2

## Dougal (v2.16??):
## Kirk's code for adding synaptics touchpads (or my version of it...)
if grep -F -q 'Alps' /proc/bus/input/devices ; then
 cp /etc/X11/xorg.conf /etc/X11/xorg.conf-without-synaptic
 modprobe evdev
 if [ "`grep '^modprobe evdev' /etc/rc.d/rc.local`" = "" ];then #v2.17
  echo "modprobe evdev" >> /etc/rc.d/rc.local
 fi
 # Where we want to insert new lines...
 OLD1='Section "Module"'
 OLD2='Section "ServerLayout"'
 OLD3='Section "Monitor"'

 # ...and the replacements
 NEW1='Section "Module"\
	Load "synaptics"'

 NEW2='Section "ServerLayout"\
	InputDevice "Synaptics Mouse" "AlwaysCore"'

 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"\
EndSection\
\
Section "Monitor"'

 # now replace:
 sed "s%$OLD1%$NEW1% ; s%$OLD2%$NEW2% ;s%$OLD3%$NEW3%" /etc/X11/xorg.conf-without-synaptic >/etc/X11/xorg.conf
fi

if grep -F -q 'Synaptics' /proc/bus/input/devices ; then
 cp /etc/X11/xorg.conf /etc/X11/xorg.conf-without-synaptic
 modprobe evdev
 echo "modprobe evdev" >> /etc/rc.d/rc.local
 # Where we want to insert new lines...
 OLD1='Section "Module"'
 OLD2='Section "ServerLayout"'
 OLD3='Section "Monitor"'

 # ...and the replacements
 NEW1='Section "Module"\
	Load "synaptics"'

 NEW2='Section "ServerLayout"\
	InputDevice "Synaptics Mouse" "AlwaysCore"'

 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"'

 # now replace:
 sed "s%$OLD1%$NEW1% ; s%$OLD2%$NEW2% ;s%$OLD3%$NEW3%" /etc/X11/xorg.conf-without-synaptic >/etc/X11/xorg.conf
fi
## end Kirk's code

#v2.13 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'`"
## Dougal: maybe add "^monitorname: "??
#PROFILEMONITOR="`grep -E -m1 '^monitorrange: |^monitorid: |^eisa: ' /tmp/ddcprobe.txt | cut -f 2 -d ':' | tr -d ' ' | sed -e 's/[^0-9a-zA-Z]/_/g'`"
#v3.95 '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'`"
#[ "$PROFILEMONITOR" = "" ] && PROFILEMONITOR="`grep '^dtiming: ' /tmp/ddcprobe.txt | tail -n 1 | cut -f 2 -d ':' | tr -d ' ' | sed -e 's/[^0-9a-zA-Z]/_/g'`"
[ "$PROFILEMONITOR" = "" ] && PROFILEMONITOR="`grep '^manufacture: ' /tmp/ddcprobe.txt | tail -n 1 | cut -f 2 -d ':' | tr -d ' ' | sed -e 's/[^0-9a-zA-Z]/_/g'`"
#[ "$PROFILEMONITOR" = "" ] && PROFILEMONITOR="`grep '^eisa: ' /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}
#also write profile to the backup (used for tweaking vertrefresh)...
echo "#PuppyHardwareProfile=${PROFILECHIP}${PROFILEMONITOR}" >> /tmp/xorg.conf.tmp

#v2.12
#note, this code will have to go into 'xwin' also.
#video bios may not offer the chosen resolution if it is widescreen.
#for intel video there is a way to patch the video bios, but this
#has to be run at every boot...
if grep -F 'oem:' /tmp/ddcprobe.txt | grep -F -q 'Intel' ;then
 #MYXYRES="`echo -n "$CHOSENRES" | cut -f 1-2 -d 'x'`" 
 MYXYRES=${CHOSENRES%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'`" 
  MYXRES=${CHOSENRES%%x*}
  MYYRES=${CHOSENRES%x*} ; MYYRES=${MYYRES#*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 eecute...
  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

[ $XAUTO -ne 0 ] && [ $XAUTO -ne 3 ] && exit #drop out to console.

#have moved this back up to original position.
##offer to revert to Xvesa...
#if [ $XAUTO -ne 0 ];then #FIX
# if [ -f /usr/X11R7/bin/Xvesa ];then
#  dialog --colors --title "Puppy Video Wizard" --no-label "Xvesa" --yes-label "Xorg" --yesno "Puppy has two X servers (to run Puppy in graphics mode):
#Xvesa: A very small and simple \"Kdrive\" X server.
#Xorg:  A very large and sophisticated X server.
#
#Up until v1.0.6, Puppy only had Xvesa. This works flawlessly on most
#video hardware, very simple to configure, but one major disadvantage
#is that screen refresh rate is fixed. Also, Xvesa has limited support
#for input devices, and lacks features like acceleration and Xinerama.
#
#Normally, you would choose to tweak the Xorg settings to get it to
#work properly, however if it does not, you can fall back to Xvesa.
#
#Press ENTER to test (and debug/fix) Xorg...
#Press TAB then ENTER key for Xvesa..." 0 0
#  if [ $? -ne 0 ];then #yes
#   ln -sf Xvesa /usr/X11R7/bin/X #Xvesa is now the default X server.
#   #fall back to any earlier xorg.conf...
#   if [ -f /etc/X11/xorg.conf ];then
#    if [ -f /etc/X11/xorg.conf.bak ];then
#     #basic test to see that file has right stuff in it...
#     if [ "`cat /etc/X11/xorg.conf | grep "VertRefresh"`" = "" ];then
#      mv -f /etc/X11/xorg.conf.bak /etc/X11/xorg.conf
#     fi
#    fi
#   fi
#   #choosemousefunc #v2.0.0
#   #the previous dlg chose a resolution, so apply that to Xvesa...
#   VPATTERN=" ${FINALXY}x${FINALDEPTH} "
#   VMODE="`cat /tmp/Xmodes.txt | grep "$VPATTERN" | head -n 1 | cut -f 1 -d ':'`"
#   echo -n "${VMODE} ${FINALXY}x${FINALDEPTH}" > /etc/videomode
#   #prevent Xvesa video wizard from running after X has started...
#   rm -f /tmp/videomode   
#   echo "Xorg Video Wizard completed. Type 'xwin' to start X..."
#   exit
#  fi
# fi
#fi

#code here to test the video...
CHOSENVERT=""
while [ $XAUTO -ne 0 ];do #testloop
 case "`cat /etc/mousedevice 2>/dev/null`" in
  input/mice) MOUSETYPE="usb" ;;
  psaux) MOUSETYPE="ps/2" ;;
  tty*) MOUSETYPE="serial" ;;
 esac
 dialog --title "Xorg Video Wizard" --yes-label "TEST_X_NOW" --no-label "Change_mouse" --yesno "Okay, just about to take the plunge.

Puppy thinks that you have a $MOUSETYPE mouse.
If this is wrong, you must choose the 'Change_mouse' button now.

After choosing 'TEST_X_NOW' button, if the next screen shows a nice
dialog box, then X started okay. If the screen is blank or garbage,
then failure (In such a case, just press CTRL-ALT-BACKSPACE to
terminate the test, preferably within the 60 second test timeout).

IMPORTANT: Some old PCs require press CTRL-ALT-BACKSPACE twice to
terminate X -- do this if one press doesn't get back to the Wizard!

Press ENTER key now to start X graphics mode (may take a few secs)...
TAB then ENTER to change the mouse (afterward will return here)..." 0 0

 if [ ! $? -eq 0 ];then
  #change the mouse... (then go back up to above dialog)
  choosemousefunc
  continue
 fi

 Xorg > /dev/null 2>&1 &
 sleep 3
 set -- `xvidtune -display :0 -show`
 #TESTMODELINE="`xvidtune -display :0 -show | tr -s " "`"
 #CLOCKMHZ=`echo -n "$TESTMODELINE" | cut -f 2 -d " "`
 #CLOCKHZ=`dc $CLOCKMHZ 1000000 \* p`
 CLOCKHZ=`dc $2 1000000 \* p`
 if [ ! "$CLOCKHZ" = "0" ];then
  #XTOTAL=`echo -n "$TESTMODELINE" | cut -f 6 -d " "`
  #YTOTAL=`echo -n "$TESTMODELINE" | cut -f 10 -d " "`
  #XTEST=`echo -n "$TESTMODELINE" | cut -f 3 -d " "`
  #YTEST=`echo -n "$TESTMODELINE" | cut -f 7 -d " "`
  XTEST=$3
  YTEST=$7
  #VRTEST=`dc $CLOCKHZ $XTOTAL \/ $YTOTAL \/ p`
  #HSTEST=`dc $CLOCKHZ $XTOTAL \/ 1000 \/ p`
  VRTEST=`dc $CLOCKHZ $6 \/ ${10} \/ p`
  HSTEST=`dc $CLOCKHZ $6 \/ 1000 \/ p`
 else
  #some video chips do not work with xvidtune.
  
  #set -- `xrandr -display :0 -q | grep '^*'`
  ##XTEST=`xrandr -display :0 -q | grep '^*' | tr -s " " | cut -f 2 -d " "`
  ##YTEST=`xrandr -display :0 -q | grep '^*' | tr -s " " | cut -f 4 -d " "`
  ##VRTEST=`xrandr -display :0 -q | grep '^*' | tr -s " " | cut -f 3 -d '*' | cut -f 1 -d " "`
  ##wrong for new xrandr...
  #XTEST=$2
  #YTEST=$4
  #VRTEST=${10#\*} 
  ##  if it's not always $10, could do: 
  ##+ VRTEST=${@} ; VRTEST=${VRTEST##*\*} ; VRTEST=${VRTEST%% *}
  
  #V2.21 BK replace above with this... v3.99...
  #XRANDRINFO="`xrandr -display :0 -q | grep -E '^\*|\*$|\* $' | tr -s ' '`"
  #v3.99 also, a workaround for now for multiple monitors, just have 'head -n 1'...
  XRANDRINFO="`xrandr -display :0 -q | grep '\*' | tr -s ' ' | grep '^ [0-9]' | head -n 1`"
  XTEST=`echo -n "$XRANDRINFO" | cut -f 2 -d ' ' | cut -f 1 -d 'x'`
  YTEST=`echo -n "$XRANDRINFO" | cut -f 2 -d ' ' | cut -f 2 -d 'x'`
  VRTEST=`echo -n "$XRANDRINFO" | cut -f 3 -d ' ' | cut -f 1 -d '*'`
  
  #xrandr does not always return the vertical frequency...
  if [ ! "$VRTEST" ];then
   if [ "$MONCHOICES" = "" ];then
    VRTEST=60 #last resort, but not correct.
   else
    VRTEST=`echo "$MONCHOICES" | grep -m1 "${XTEST}x${YTEST}" | cut -f 2 -d '@'`
    [ ! "$VRTEST" ] && VRTEST=60 #desparate last resort.
   fi
  fi
  # so we calc horiz freq theoretically... v3.93 modified...
  #HSTEST=`gtf $XTEST $YTEST $VRTEST | head -n 2 | tail -n 1 | cut -f 1 -d ';' | cut -f 2 -d ':' | cut -f 2 -d ' ' | cut -f 1 -d '.'`
  HSTEST=`gtf $XTEST $YTEST $VRTEST | head -n 2 | tail -n 1 | cut -f 1 -d ';' | cut -f 2 -d ':' | cut -f 2 -d ' '`
 fi

#v3.93 round off to two decimal places...
HSTEST=`printf "%.2f" $HSTEST`
VRTEST=`printf "%.2f" $VRTEST`

 #xsetroot -display :0 -grey
 #xli -display :0 -onroot -quiet /usr/share/images/linuz.gif
 #v2.10... v3.93 remove...
 #qiv --display :0 --root_t /usr/share/images/linuz.gif &

#v2.16beta BK ok button still hangs my laptop, think best not have ok...
## xmessage -center -timeout 40 -display :0 -default "okay" "Puppy Xorg Video
# gxmessage -center -timeout 40 -display :0 -default "okay" " 
# Puppy Xorg Video Wizard: testing X
#
#
# If you can see this, then X is working!
#
# Current resolution:         ${XTEST}x${YTEST} pixels
# Horizontal sync frequency:  $HSTEST KHz
# Vertical refresh frequency: $VRTEST Hz (times per second
#                                              screen image is refreshed)
#
# However, if the image is wrong, say displaced on the screen
# or distorted, then you will need to tweak /etc/X11/xorg.conf.
#
# Press \"Okay\" (or just hit ENTER) to continue.
#
# NOTE:	You can also exit this message by hitting
#		CTRL-ALT-BACKSPACE - the standard keyboard 
#		shortcut for terminating the X graphics server.)"

# yaf-splash -display :0 -font "8x16" -outline 0 -margin 5 -bg "pale green" -timeout 60 -text "
# xmessage -center -timeout 40 -display :0 -buttons "" " 
 gxmessage -font "8x16" -center -timeout 60 -display :0 -buttons "OK:10" " 
Puppy Xorg Video Wizard: testing X


これが読めるなら、Xは動いています！

現在の解像度:         ${XTEST}x${YTEST} pixels
水平同期周波数:  $HSTEST KHz
垂直リフレッシュレート: $VRTEST Hz (times per second)

６０秒以内に「CTRL-ALT-BACKSPACE」と打って下さい（CTRL と ALT を
押さえてBACKSPACEキーを打ちます）。-- ６０秒以内です！ --

あるいは「OK」ボタンをクリックします -- 警告。これをするとハングアップ
するPCもあります。そうなったら「CTRL-ALT-BACKSPACE」が推奨方法です。"


 ##  Dougal: some machines freeze when we try and kill Xorg with xrandr 
 ##+ running (see comment in xorg.conf0) -- so turn it off first...
 echo '##### the following error message can be ignored #####' >&2
 Xorg -extension RANDR
 echo '######### end ignored error message (Dougal) #########' >&2
 #if [ $? -eq 0 ];then
 # XTRY="success"
 #else
 # XTRY="fail"
 #fi
 killall qiv > /dev/null 2>&1
 pidof Xorg && killall Xorg > /dev/null 2>&1 #SIGTERM
 sleep 1
 #pidof qiv && killall -9 qiv
 pidof Xorg && killall -9 Xorg #SIGKILL
 sleep 1
 #pidof qiv && killall -3 qiv
 pidof Xorg && killall -3 Xorg

 if [ -f /usr/X11R7/bin/Xvesa ];then
  TWKMSG="Note: If you have tweaked away, to no avail, choose \"FINISHED\" and you
      will get one final chance to fall back to using Xvesa.
Note: If display has only minor displacement or proportions error, run
      (with care) Xvidtune module in Xorg Wizard within X (Setup menu). 

Press ENTER if test was okay (or to fall back to Xvesa)... "
 else
  TWKMSG="Press ENTER key if test was okay..."
 fi

 dialog --no-collapse --title "Xorg Video Wizard" --yes-label "FINISHED" --no-label "Tweak" --yesno "Report on X test:
                  Resolution           ${XTEST}x${YTEST} pixels
                  Horizontal frequency $HSTEST KHz
                  Refresh frequency    $VRTEST Hz

If you are reporting success, great, but if the display was not quite
right, say displaced or distorted, you now have the option of tweaking
the HorizSync and VertRefresh parameters in /etc/X11/xorg.conf.
Ditto if testing X was an abysmal failure.

${TWKMSG}
TAB then ENTER to tweak parameters in xorg.conf & retest X..." 0 0

 if [ $? -eq 0 ];then
  break
 fi

 if [ ! "$CARD0DRIVER" = "vesa" ];then #v2.11
  dialog --no-collapse --title "Xorg Video Wizard" --yes-label "Tweak_xorg.conf" --no-label "Generic_driver" --yesno "Before continuing to tweaking xorg.conf, one other thing to consider.
Xorg has a dozen or so drivers for particular video hardware, which
optimises performance for the matching hardware, but sometimes a
driver does not work right or there is no exact driver to match the
hardware. The Intel 'i810' driver is an example that is troublesome.
The workaround for this is that Xorg has a generic driver called
'vesa' (note, this is not the Xvesa X server!), that works with most
video hardware, but some performance may be lost.

The current driver being used by Xorg is '${CARD0DRIVER}', and your video
vendor is:  '${CARD0VENDOR}'
board info: '${CARD0BOARD}'

So, if you have already tried 'tweaking' or you know that the Xorg
'${CARD0DRIVER}' driver does not work on your hardware, consider the
generic driver. Click 'Generic_driver' to change Xorg to using the
generic driver, otherwise press ahead and try tweaking..." 0 0
  if [ ! $? -eq 0 ];then
   APATTERN="s/.*#card0driver/	Driver      \"vesa\" #card0driver/g"
   sed -e "$APATTERN" /etc/X11/xorg.conf > /tmp/xorg.conf.tmp2
   sync
   cp -f /tmp/xorg.conf.tmp2 /etc/X11/xorg.conf
   continue
  fi
 else #using xorg 'vesa' driver...
  SPECVESA="`find /usr/X11R7/lib -noleaf -mount -type f -name vesa_drv.so`"
  DRVRSPATH="`dirname $SPECVESA`"
  DRVRSNUM="`ls -1 $DRVRSPATH | wc -l | sed -e 's/ //g'`"
  if [ "$DRVRSNUM" = "1" ];then
  dialog --no-collapse --title "Xorg Video Wizard" --msgbox "IMPORTANT INFORMATION
Xorg currently only has the 'Generic driver' installed, called 'vesa'
(remember: this is different from the simple Xvesa X server!).
The Generic driver will work with most video hardware, however it lacks
some features, in particular has a limited selection of screen refresh
frequencies and is slow -- a factor if you wish to play DVD videos and
graphical games. However, you can easily install a Xorg driver specific
to your video hardware -- after Puppy is connected to the Internet, run
the PupGet package manager and install the 'xorg_VIDEO_DRVRS' package.
After that, run the Xorg Video Wizard again and it will automatically
choose the best driver for your PC's video hardware
(PupGet and Xorg Video Wizard are to be found in the 'Setup' menu).

A further note: for extra video speed, Xorg has what is called the
'Direct Rendering Interface' (DRI), which requires a special driver.
Some games can benefit from this. Also, some applications and games
need 'GLI', which is a special library for 3-dimensional rendering.
Again, the PupGet package manager has 'xorg_DRI_MODULES' and 'xorg_
OPENGL' packages if you require these extra capabilities.

For now, keep going with using the Generic driver
(for general PC use, this is proably all that you need anyway)..."
  fi
 fi

 #tweak...
 dialog --no-collapse --title "Xorg Video Wizard" --yes-label "Tweak_refresh" --no-label "Edit_xorg.conf" --yesno "If the graphics mode display was distorted, displaced, or a complete
mess, the simplest option now is to adjust the vertical screen refresh
frequency up or down slightly. 
However, if you are more experienced with Linux and X, you may prefer
to open xorg.conf in a text editor and change whatever you wish.

Press ENTER key for the easy option, tweak refresh frequency...
Press TAB then ENTER to open xorg.conf in a text editor..." 0 0

 if [ $? -eq 0 ];then
  if [ "$MONCHOICES" = "" ];then
   #not plug-n-play.
   MSGPNP="WARNING: Your monitor is not plug-and-play so this Wizard cannot
         determine its max allowable refresh frequency. A very cheap old
         CRT monitor may not handle more than 70Hz, better quality, 85Hz."
  else
   MSGPNP="WARNING: the maximum screen refresh frequency is specified
         as ${MAXVERT}Hz (times per second). Do not choose any entry
         in the list below that exceeds this."
  fi
  MSGGEN="Note1: It is unwise to push a monitor too hard. Just go high enough so
       that there is no flicker. 72Hz-85Hz for CRT, 60Hz for LCD.
Note2: Display displaced to right is often due to frequency too high."
  #just offer standard freqs...
  VERT0=56; MSGV0="Very noticeable flicker on CRT screens"
  VERT1=60; MSGV1="Okay for most LCD screens, flicker on CRT"
  VERT2=72; MSGV2="Sensitive eyes may notice flicker on CRT"
  VERT3=75; MSGV3="Sensitive eyes may notice flicker on CRT"
  VERT4=80; MSGV4="Usually no flicker on CRT"
  VERT5=85; MSGV5="No flicker on CRT"
  
  dialog --no-cancel --title "Xorg Video Wizard" --menu "Xorg is currently set to refresh the screen $VRTEST times per second. Taking this up or down may remove distortion etc., but note that the choices here are only suggested upper limits and Xorg may not use exactly the frequency chosen here.

$MSGGEN

$MSGPNP" 0 0 0 \
 "$VERT0" "$MSGV0" \
 "$VERT1" "$MSGV1" \
 "$VERT2" "$MSGV2" \
 "$VERT3" "$MSGV3" \
 "$VERT4" "$MSGV4" \
 "$VERT5" "$MSGV5" \
 2>/tmp/tagvert.txt

  #FUTURE: use xrandr above, to determine alternative lower frequencies.

  CHOSENVERT="`cat /tmp/tagvert.txt 2>/dev/null`"
  NEEDHORIZ=`gtf $XTEST $YTEST $CHOSENVERT | grep -o 'hsync: .*' | cut -f 2 -d ' ' | cut -f 1 -d '.'`
  let NEEDHORIZ=NEEDHORIZ+1 #compensate for rounding off error.
  APATTERN="s/HORIZSYNC0/${HORIZSYNC}/g"
  if [ $NEEDHORIZ -gt ${MAXHORIZ%.*} ];then
   if [ "$MONCHOICES" = "" ];then
    #monitor not plug and play.
    APATTERN="s/HORIZSYNC0/${MINHORIZ}\\-${NEEDHORIZ}/g"
   fi
  fi
  sed -e "$APATTERN" /tmp/xorg.conf.tmp > /tmp/xorg.conf.tmp2
  BPATTERN="s/VERTREFRESH0/${MINVERT}\\-${CHOSENVERT}/g"
  sync
  sed -e "$BPATTERN" /tmp/xorg.conf.tmp2 > /etc/X11/xorg.conf
  sync
  rm -f /tmp/xorg.conf.tmp2
 else
  dialog --no-collapse --title "Xorg Video Wizard" --msgbox "Before editing xorg.conf, consider this...

/etx/X11/xorg.conf has these lines in the Monitor section:
  HorizSync    $HORIZSYNC
  VertRefresh  $VERTREFRESH

For the resolution that you want to run at, you many want to narrow
-down the specs in xorg.conf. For example, say you want 1024x768@85,
but xorg.conf has VertRefresh 50-160. -- maybe narrow that to 50-85.

Press ENTER key to edit xorg.conf..." 0 0
  #edit xorg.conf...
  mp /etc/X11/xorg.conf
 fi
done #testloop

if [ $XAUTO -ne 0 ];then #<FIX> (XAUTO=0 is <OK> button)
 if [ -f /usr/X11R7/bin/Xvesa ];then
  dialog --title "Xorg Video Wizard" --yes-label "DONE" --no-label "Xvesa" --yesno "Just press ENTER key if all is fine and you are ready to run
Puppy with Xorg.

If you cannot get Xorg to work, press TAB then ENTER to choose
the \"Xvesa\" button. This will start Puppy with the simple Xvesa
graphics server that runs on most video hardware." 0 0

  if [ ! $? -eq 0 ];then
   ln -sf Xvesa /usr/X11R7/bin/X
  else
   #prevent Xvesa video wizard from running after X has started...
   rm -f /tmp/videomode
  fi
 fi
else
 #prevent Xvesa video wizard from running after X has started...
 rm -f /tmp/videomode
fi

#move it so people ain't confused seeing it in /root...
mv -f /root/xorg.conf.new /tmp/xorg.conf.new

if [ $XAUTO -ne 0 ];then #<FIX> (XAUTO=0 is <OK> button)
 #need to do this here again in case of tweaking made some changes...
 #create a copy of xorg.conf with the profile in the filename...
 cp -af /etc/X11/xorg.conf /etc/X11/xorg.conf.${PROFILECHIP}${PROFILEMONITOR}
 clear
 echo "Xorg Video Wizard completed. Type 'xwin' to start X..."
else
 clear
 #user chose the <OK> button, so going directly into X without testing.
 echo -en "\\033[1;31m" #34=blue, 33=yellow, 32=green, 31=red, 35=purple, 36=aquablue, 38=black.
 echo "If X did not work, type 'xorgwizard' to run the Video Wizard again."
 echo "After that, type 'xwin' to run X graphics mode."
 echo "Note, if X works but needs minor adjustment to the display, run the"
 echo "Video Wizard from within X (in the Setup menu) -- module 'Xvidtune'"
 echo -n "may be used, with caution, to finetune the display dimensions."
 echo -e "\\033[0;39m"
fi

#v3.99 begin
[ -z /tmp/ddcprobeoem.txt ] && rm /tmp/ddcprobeoem.txt  #empty
#If started from command line after hang, delete new Xvesa hanger indication and go to xwin - no need to have user type it in.
[ -f /tmp/ddcprobeoem.txt ] && rm /tmp/ddcprobeoem.txt && exec xwin 
#3.99 end

###END###
