#!/bin/sh
#(c) Copyright Barry Kauler 2006,2007 www.puppylinux.com
#2007 Lesser GPL licence v2 (/usr/share/doc/legal/lgpl-2.1.txt)
## 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.
#v405 BK 22jul08: added support for 1024x600.
#v408 BK k2.6.25.16 serial mouse driver now a module 'sermouse'.
#v411 rerwin: mouse improvements.
#v411 moved choosemousefunc() to /etc/rc.d/functions4puppy4, want call from rc.sysinit.
#w000 path /usr/X11R7 no longer used.
#w002 no longer offering xvesa.
#w005 restored EXIT button in first dlg.
#w007 fix X if it is not a symlink.
#w019 april2009: restored Xvesa.
#w460 fixed X test screen, proper exit.
#w468 modify mouse detection message.
#w478 fix prevent 2 instances of xwin, do not 'exec xwin'.
#w480 bugfix, serial mouse choice lost if choose to tweak refresh.
#w482 allow switch between intel_drv.so and i810OLD_drv.so.
#091119 pizzasgood: patch for multiple monitors.
#091125 if no Xvesa, do not display dlg that offers to choose it.
#100109 fix yes/no labels in dlg, fix bug when switch to 'vesa' driver.
#100113 choose alternate video drivers.
#100215 now have xgamma-gui.
#100328 different xorg.conf0 for xorg 7.3.
#100329,100330 first window offer vesa.
#100401 offer alternate drivers in first window.
#100430 handle commercial nvidia driver. improve Choose button.
#100501 monitor probe returns random 'manufacture' data from one of my monitors.
#100510 fix column alignment.
#100516 TazOC: nvidia module loading fix.
#100603 Jemimah: support Elantech touchpads.
#100708 fix for when all three nv, nouveau, nvidia drivers present, choose just one.
#100820 Xvesa present, improve dialogs if choose Xorg.
#100820 for intel video, mplayer needs 'Option "LinearAlloc" "6144"' in xorg.conf.
#100902 fix switching between choosing 'intel' and 'i810' drivers.
#101224 added LANG=C in call to xinitrc_test.
#110505 support sudo for non-root user.
#110701 synaptics stuff seems to clash with xorg-server 1.10.1.
#110701 bugfix for '%' char in /root/xorg.conf.new.
#110806 correction, changed 1360x768 to 1366x768.
#110814 01micko: convert script from gtkdialog2 to gtkdialog3+.
#110910 BK: gtkdialog3 (patriot-e version) syntax error, bump to gtkdialog4.
#110911 'drivers-alternate' moved from /usr/lib/xorg/modules/ to /usr/lib/x/.
#110927 fix touchpad setup.
#111108 Karl Godt: fix for desktop drive icons getting drawn in wrong place after change resolution.
#120129 jemimah: add Option "SHMConfig" "on".
#120130 rodin.s: i18n internationalise script. BK: note, edited all changes manually, as script from rodin.s had some differences.
#120131 rodin.s: added a couple more translations.
#120213 /var/local/pup_event_icon_change_flag path changed from /tmp (see /sbin/clean_desk_icons)
#120213 rodin.s: removed LANG=C prefix when run xinitrc_test.
#120226 01micko: bugfix, call new script 'nouveau_unload'.
#120301 it seems have to reload console font after exit from X.
#120318 correctly set keyboard layout, model and variant.
#120323 partial replace 'xmessage' with 'pupmessage'.
#120723 BOOT_DISABLEXORGWIZARD variable in /etc/rc.d/BOOTCONSTRAINED. refer 3builddistro, also xwin.
#130201 01micko: support "modesetting_drv.so" xorg driver installed to /usr/lib/x/drivers-alternate/
#130427 new script xorgwizard-cli.
#130504 no longer using nouveau_unload.
#150929 do not assume xorg is in /usr/lib
#151105 change gtkdialog4 to gtkdialog.
#160914 no longer using mouse and keyboard xorg drivers, now evdev, see /etc/X11/xorg.conf.d/10-evdev-puppy.conf
#181223 new zarfy multiple monitor manager.
#200113 add PuppyHardwareProfile for 'xorg.conf.d' folder.
#200114 improve PuppyHardwareProfile. see also: /usr/bin/xwin /usr/sbin/xorgwizard-cli, xorgwizard-automatic. 200120
#200121 old wizard code removed. see xorgwizard-cli.
#200131 document brightness tray applet.

[ "`whoami`" != "root" ] && exec sudo -A ${0} ${@} #110505

#130427 new script replaces this one...
if [ ! $DISPLAY ];then
 XORGVER="`Xorg -version 2>&1 | grep '^X Window System Version' | rev | cut -f 1 -d ' ' | rev`" #ex: 1.3.0
 [ ! "$XORGVER" ] && XORGVER="`Xorg -version 2>&1 | grep '^X\.Org X Server' | rev | cut -f 1 -d ' ' | rev`"
 if vercmp $XORGVER ge 1.11.0; then
  exec xorgwizard-cli
 fi
fi

export TEXTDOMAIN=xorgwizard
export OUTPUT_CHARSET=UTF-8
. gettext.sh

mkdir -p /usr/lib/x/drivers-backup
mkdir -p /var/local/xorg #100902
CR='
'

#130504 no longer using nouveau_unload...
##120226 unload nouveau
#[ $DISPLAY ] && CURRENTVIDEODRIVER="`grep -E -i " connected|card detect|primary dev" /var/log/Xorg.0.log|cut -d ':' -f1|rev|cut -d ' ' -f1|rev|cut -d '(' -f1|tr '[:upper:]' '[:lower:]'|head -n1`"
#[ "$CURRENTVIDEODRIVER" = "nouveau" ] && nouveau_unload
#[ -f /tmp/abort_xorgwizard ] && exit

. /etc/DISTRO_SPECS #120723
. /etc/rc.d/BOOTCONSTRAINED #120723

#150929 find where is xorg... ex: /usr/lib64/xorg
XORGPATH="$(find /usr -maxdepth 5 -type d -name drivers | grep '/xorg/modules/drivers' | head -n 1 | rev | cut -f 3- -d '/' | rev)"
if [ ! "$XORGPATH" ];then
 echo "Sorry, cannot find where Xorg is installed, quiting."
 exit
fi

if [ ! $DISPLAY ];then #120723
 if [ "$BOOT_DISABLEXORGWIZARD" = "yes" ];then #120723 refer /etc/rc.d/BOOTCONSTRAINED and 3builddistro.
  echo
  echo "Sorry, the text-mode Xorg Wizard is disabled for this build of Puppy."
  exit
 fi
fi

alternate_driver_func() { #100401 100708
 REPLACEDDRV=""
 mv -f /usr/lib/x/drivers-backup/* ${XORGPATH}/modules/drivers/ 2>/dev/null
 SUBSTDRV="$1" #alternative driver in /usr/lib/x/drivers-alternate
 case $SUBSTDRV in
  intel)
   REPLACEDDRV="i810"
   [ ! -h ${XORGPATH}/modules/drivers/i810_drv.so ] && mv -f ${XORGPATH}/modules/drivers/i810_drv.so /usr/lib/x/drivers-alternate/
   mv -f /usr/lib/x/drivers-alternate/intel_drv.so ${XORGPATH}/modules/drivers/
   [ ! -e ${XORGPATH}/modules/drivers/i810_drv.so ] && [ -h /var/local/xorg/i810_drv.so ] && mv -f /var/local/xorg/i810_drv.so ${XORGPATH}/modules/drivers/ #100902
   #100330 restore, see i810 case below...
   if [ -d /usr/lib/x/drivers-alternate/intel ];then #100330 see my xf86-video-i810_xorg73 pet pkg.
    mkdir -p /usr/lib/x/drivers-alternate/i810
    for ONEXFILE in `find /usr/lib/x/drivers-alternate/intel -type f | sed -e 's%/usr/lib/x/drivers-alternate/intel/%%' | tr '\n' ' '`
    do
     ONEXDIR="`dirname $ONEXFILE`"
     [ "$ONEXDIR" = "." ] && ONEXDIR=""
     mkdir -p /usr/lib/x/drivers-alternate/i810/$ONEXDIR
     cp -a -f /$ONEXFILE /usr/lib/x/drivers-alternate/i810/$ONEXDIR #swap them
     cp -a -f /usr/lib/x/drivers-alternate/intel/$ONEXFILE /$ONEXDIR  # "
    done
   fi
  ;;
  i810)
   REPLACEDDRV="intel"
   [ -h ${XORGPATH}/modules/drivers/i810_drv.so ] && mv -f ${XORGPATH}/modules/drivers/i810_drv.so /var/local/xorg/ #move aside old symlink. 100902
   mv -f ${XORGPATH}/modules/drivers/intel_drv.so /usr/lib/x/drivers-alternate/
   mv -f /usr/lib/x/drivers-alternate/i810_drv.so ${XORGPATH}/modules/drivers/
   if [ -d /usr/lib/x/drivers-alternate/i810 ];then #100330 see my xf86-video-i810_xorg73 pet pkg.
    mkdir -p /usr/lib/x/drivers-alternate/intel #100330
    for ONEXFILE in `find /usr/lib/x/drivers-alternate/i810 -type f | sed -e 's%/usr/lib/x/drivers-alternate/i810/%%' | tr '\n' ' '`
    do
     ONEXDIR="`dirname $ONEXFILE`"
     [ "$ONEXDIR" = "." ] && ONEXDIR=""
     mkdir -p /usr/lib/x/drivers-alternate/intel/$ONEXDIR
     cp -a -f /$ONEXFILE /usr/lib/x/drivers-alternate/intel/$ONEXDIR #swap them
     cp -a -f /usr/lib/x/drivers-alternate/i810/$ONEXFILE /$ONEXDIR  # "
    done
   fi
  ;;
  nv)
   mv -f /usr/lib/x/drivers-alternate/nv_drv.so ${XORGPATH}/modules/drivers/
   if [ -f ${XORGPATH}/modules/drivers/nouveau_drv.so ];then
    REPLACEDDRV="nouvea"
    mv -f ${XORGPATH}/modules/drivers/nouveau_drv.so /usr/lib/x/drivers-alternate/
   fi
   if [ -f ${XORGPATH}/modules/drivers/nvidia_drv.so ];then #100430
    REPLACEDDRV2="nvidia"
    mv -f ${XORGPATH}/modules/drivers/nvidia_drv.so /usr/lib/x/drivers-alternate/
    rmmod nvidia #100516
   fi
  ;;
  nouveau)
   mv -f /usr/lib/x/drivers-alternate/nouveau_drv.so ${XORGPATH}/modules/drivers/
   if [ -f ${XORGPATH}/modules/drivers/nv_drv.so ];then
    REPLACEDDRV="nv"
    mv -f ${XORGPATH}/modules/drivers/nv_drv.so /usr/lib/x/drivers-alternate/
   fi
   if [ -f ${XORGPATH}/modules/drivers/nvidia_drv.so ];then
    REPLACEDDRV2="nvidia"
    mv -f ${XORGPATH}/modules/drivers/nvidia_drv.so /usr/lib/x/drivers-alternate/
    rmmod nvidia
   fi
  ;;
  nvidia)
   mv -f /usr/lib/x/drivers-alternate/nvidia_drv.so ${XORGPATH}/modules/drivers/
   if [ -f ${XORGPATH}/modules/drivers/nv_drv.so ];then
    REPLACEDDRV="nv"
    mv -f ${XORGPATH}/modules/drivers/nv_drv.so /usr/lib/x/drivers-alternate/
   fi
   if [ -f ${XORGPATH}/modules/drivers/nouveau_drv.so ];then
    REPLACEDDRV2="nouvea"
    mv -f ${XORGPATH}/modules/drivers/nouveau_drv.so /usr/lib/x/drivers-alternate/
   fi
   modprobe nvidia
  ;;
  modesetting) #130201 01micko.
   REPLACEDDRV=""
   [ -f /usr/lib/x/drivers-alternate/modesetting_drv.so ] && mv -f /usr/lib/x/drivers-alternate/modesetting_drv.so ${XORGPATH}/modules/drivers/
  ;;
  *) #100430 get rid of all drivers except chosen one.
   REPLACEDDRV=""
   mv -f ${XORGPATH}/modules/drivers/* /usr/lib/x/drivers-backup/
   [ -h /usr/lib/x/drivers-backup/i810_drv.so ] && mv -f /usr/lib/x/drivers-backup/i810_drv.so /var/local/xorg/ #100902
   [ -f /usr/lib/x/drivers-backup/$SUBSTDRV ] && cp -f /usr/lib/x/drivers-backup/$SUBSTDRV ${XORGPATH}/modules/drivers/
   [ -f /usr/lib/x/drivers-alternate/$SUBSTDRV ] && cp -f /usr/lib/x/drivers-alternate/$SUBSTDRV ${XORGPATH}/modules/drivers/
  ;;
 esac
 if [ "$REPLACEDDRV" -o "$REPLACEDDRV2" ];then
  REPLACEDDRV0="${REPLACEDDRV}${REPLACEDDRV2}"
  XMSG1="`eval_gettext \"NOTICE: The '\\\${REPLACEDDRV0}' driver has been removed, so when Xorg probes the video hardware, it should instead use '\\\${SUBSTDRV}'. 

TECHNICAL NOTE: '\\\${REPLACEDDRV0}' has been moved to /usr/lib/x/drivers-alternate and if you ever want to manually get it back so that Xorg can use it, move it to ${XORGPATH}/modules/drivers. Note that there may also be one or more directories inside /usr/lib/x/drivers-alternate with library files needed for the alternate drivers.

Press ENTER key to continue...\"`"
  if [ "$REPLACEDDRV" -a "$REPLACEDDRV2" ];then
   XMSG1="`eval_gettext \"NOTICE: The '\\\${REPLACEDDRV}' and '\\\${REPLACEDDRV2}' drivers have been removed, so when Xorg probes the video hardware, it should instead use '\\\${SUBSTDRV}'. 

TECHNICAL NOTE: '\\\${REPLACEDDRV}' and '\\\${REPLACEDDRV2}' have been moved to /usr/lib/x/drivers-alternate and if you ever want to manually get them back so that Xorg can use them, move them to ${XORGPATH}/modules/drivers. Note that there may also be one or more directories inside /usr/lib/x/drivers-alternate with library files needed for the alternate drivers.

Press ENTER key to continue...\"`"
  fi
  dialog  --title "$(gettext 'Xorg Video Wizard')" --msgbox "${XMSG1}" 0 0
 fi
 #restart wizard after return.
} #end alternate_driver_func

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

PSFND="`ps`"
CURRENTX="`readlink /usr/bin/X`"
if [ "$CURRENTX" = "" ];then #w007 fix if not a symlink.
 ln -snf Xorg /usr/bin/X
 CURRENTX='Xorg'
fi

#110701 improve logic...
XORGVER="`Xorg -version 2>&1 | grep '^X Window System Version' | rev | cut -f 1 -d ' ' | rev`" #ex: 1.3.0
[ ! "$XORGVER" ] && XORGVER="`Xorg -version 2>&1 | grep '^X\.Org X Server' | rev | cut -f 1 -d ' ' | rev`"

#100328 determine version of Xorg...
if [ -f /etc/X11/xorg.conf0XORG73 ];then
 #110701 remove test...
 #XORGBIN="`which Xorg`"
 #if [ "$XORGBIN" ];then
  #XVERSION=`Xorg -version 2>&1 | grep '^X Window System Version' | rev | cut -f 1 -d ' ' | rev | cut -c 3` #ex: 3 (as in 1.3.0)
  #[ ! $XVERSION ] && XVERSION=`Xorg -version 2>&1 | grep '^X.Org X Server' | rev | cut -f 1 -d ' ' | rev | cut -c 3`
  #if [ $XVERSION -lt 5 ];then
  if vercmp $XORGVER lt 1.5.0; then #110701
   if [ -f /etc/X11/xorg.conf0XORG73 ];then
    #xorg 7.3 needs a different file...
    mv -f /etc/X11/xorg.conf0XORG73 /etc/X11/xorg.conf0
   fi
  fi
 #fi
fi

. /etc/rc.d/functions4puppy4 #v411 has choosemousefunc().

#120318 broken, use shinobar's keymap-set...
##v2.0.0 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
#if [ -d /etc/X11/xkb/symbols/pc ];then #100127 fallback...
# if [ ! -f /etc/X11/xkb/symbols/pc/${XKEYMAP} ];then
#  if [ ! -f /etc/X11/xkb/symbols/${XKEYMAP} ];then
#   echo "ERROR: Console keyboard `cat /etc/keymap` no matching Xorg layout. PLEASE REPORT TO BK"
#   XKEYMAP='us'
#  fi
# fi
#fi
XKEYMAP='us'
RETVALS="`keymap-set --xopt`"
eval "$RETVALS" #ex: XKBMODEL="pc102", XKBLAYOUT="us", XKBVARIANT=""
[ "$XKBLAYOUT" = "" ] && XKBLAYOUT='us'
[ "$XKBMODEL" = "" ] && XKBMODEL='pc102'

if [ "$BOOT_DISABLEXORGWIZARD" = "yes" ];then #120723 refer /etc/rc.d/BOOTCONSTRAINED and 3builddistro.
 XORGPRELIM_XML="
  <text><label>\"$(gettext 'The behaviour of Xorg is controlled
by a configuration file, /etc/X11/
xorg.conf. This was generated auto-
matically at the first boot, but
you may now edit it manually.')\"</label></text>"
 XORGWIZ_XML=""
else
 XORGPRELIM_XML="
  <text><label>\"$(gettext 'The behaviour of Xorg is controlled
by a configuration file, /etc/X11/
xorg.conf. You have a choice here,
either to completely reconstruct
the /etc/X11/xorg.conf file, or
to modify the existing file.')\"</label></text>
  <text><label>\"$(gettext 'Also, specific components
are controlled by files in 
/etc/X11/xorg.conf.d')\"</label></text>"
 XORGWIZ_XML="
 <frame $(gettext 'XorgWizard')>
  <hbox>
   <text><label>$(gettext 'XorgWizard completely reconstructs the /etc/X11/xorg.conf file, and X must not be running to do this. Click the button to exit from X, and you will then be able to run xorgwizard.')</label></text>
   <vbox>
    <button>
     <input file>/usr/local/lib/X11/mini-icons/wizard16.xpm</input>
     <action>EXIT:11</action>
    </button>
   </vbox>
  </hbox>
  <text><label>$(gettext 'NOTE: You can also run XorgWizard by exiting from X via the menu at bottom-left of the screen, Shutdown --> Exit to commandline.')</label></text>
 </frame>"
fi

#181223 zarfy manage multiple monitors
if which zarfy >/dev/null; then
 ZARFY_XML="
  <frame $(gettext 'Multiple monitors')>
   <hbox>
    <text><label>$(gettext 'Use Zarfy to manage multiple monitors:')</label></text>
    <vbox>
     <button>
      <width>22</width>
      <height>22</height>
      <input file>/usr/local/lib/X11/mini-icons/mini-doc1.xpm</input>
      <action>basichtmlviewer /usr/share/doc/zarfy.htm & </action>
     </button>
    </vbox>
    <vbox>
     <button>
      <width>22</width>
      <height>22</height>
      <input file>/usr/share/zarfy/zarfy.png</input>
      <action>EXIT:19</action>
     </button>
    </vbox>
   </hbox>
  </frame>
"
fi

MAIN1="
<window title=\"$(gettext 'Xorg Video Wizard')\">
<hbox>
 <vbox>
  <text><label>\"$(gettext 'Welcome to the Puppy Linux
Xorg Video Wizard!')\"</label></text>
  ${XORGPRELIM_XML}
 </vbox>
 <vbox>
  ${XORGWIZ_XML}
  <frame $(gettext 'Edit xorg.conf')>
   <hbox>
    <text><label>$(gettext 'You can manually edit /etc/X11/xorg.conf, but note that you will need to exit from X afterward then restart X (see Shutdown menu). Click button to edit:')</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>$(gettext 'If the screen is displaced or the width/height are wrong, xvidtune can get it right. This will modify the existing xorg.conf file. Use with caution:')</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 $(gettext 'Mouse/keyboard Wizard')>
   <hbox>
    <text><label>$(gettext '/etc/X11/xorg.conf.d/10-evdev-puppy.conf has generic settings for mouse and keyboard and in most cases it is recommended to leave it as-is. Instead, use the MouseKeyboardWizard:')</label></text>
    <vbox>
     <button>
      <input file>/usr/local/lib/X11/mini-icons/wizard16.xpm</input>
      <action>EXIT:16</action>
     </button>
    </vbox>
   </hbox>
  </frame>

  <frame $(gettext 'Monitor gamma calibration')>
   <hbox>
    <text><label>\"$(gettext 'This will adjust the monitor colors:')
$(gettext '(also see below, there is a brightness and tint tray applet)')\"</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 $(gettext 'Resolution changer')>
   <hbox>
    <text><label>$(gettext 'It is possible to change screen resolutions without exiting from 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>
  
  ${ZARFY_XML}

 </vbox>
</hbox>
</window>
"


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

 RETSTR="`echo "$MAIN1" | gtkdialog --stdin`"
 RETVAL="`echo "$RETSTR" | grep '^EXIT=' | cut -f 2 -d '"'`" #'geany

 case $RETVAL in
  11) #XorgWizard. 200121 fix
   #rm /etc/mousedevice # Dougal: any reason for this?
   . /etc/DISTRO_SPECS #120226 01micko fix for boot to desktop
   [ "$DISTRO_XORG_AUTO" = "yes" ] && sed -i "s/^DISTRO_XORG_AUTO='yes'/DISTRO_XORG_AUTO='no'/" /etc/DISTRO_SPECS #120226 temporary, fixes xorgwizard gui bug
   mv -f /etc/X11/xorg.conf /etc/X11/xorg.conf.prev 2>/dev/null
   [ -e /usr/bin/Xvesa ] && ln -sf Xvesa /usr/bin/X
   #NEXTWM="`cat /etc/windowmanager`"
   #echo -n "$NEXTWM" > /etc/windowmanager #this makes change permanent.
   #echo -n "$NEXTWM" > /tmp/wmexitmode.txt
   echo -n "ICONWIPE" >/var/local/pup_event_icon_change_flag #111108 Karl Godt: in case Xvesa selects different resolution.  120213 path changed from /tmp (see /sbin/clean_desk_icons)
   sync
   #exec killall X
   exec /usr/bin/wmexit #200121 just killing X is a bit harsh.
  ;;
  12) #xorgconfig
   xorgcfg
   pupmessage -bg "#ff8080" -center -title "xorgcfg" "$(gettext 'Changes to /etc/X11/xorg.conf and /etc/X11/xorg.conf.d/ will only take effect after X is restarted.')

$(gettext 'Click OK button to restart X...')"
   exec restartwm
  ;;
  13) #xvidtune
   pupmessage -bg violet -title "$(gettext 'Xvidtune IMPORTANT HELP')" "`gettext \"If you click the 'Show' button, the adjusted settings will be what
you want permanently. After hitting the 'Quit' button, you will be
given one last chance not to make the change permanent.\"`
$(gettext 'Thus:')
`gettext \"'Show' to record the settings (for later inclusion into xorg.conf)\"`
`gettext \"'Quit' to exit Xvidtune.\"`

$(gettext 'Please note that Xvidtune does not work with all video hardware, meaning
that changing the settings will cause no change on the screen.')" &
   XMSGPID=$!
   MODELINE0="`xvidtune | grep '^"[0-9]'`" #'geanyfix.
   pupkill $XMSGPID
   if [ ! "$MODELINE0" = "" ];then
    xmessage -bg orange -title "$(gettext 'Xvidtune: Modeline')" -buttons Write:10,Quit:11 "$(gettext 'The new modeline is:')
$MODELINE0

$(gettext 'Note, you will have to restart X for it to take effect. If it messes')
`gettext \"up X, edit from commandline 'mp /etc/X11/xorg.conf' and comment-out\"`
`gettext \"the 'UseModes' line (do not delete it) in the Monitor section.\"`

`gettext \"To insert this into /etc/X11/xorg.conf, click 'Write' button...\"`
`gettext \"To exit without changing xorg.conf, click 'Quit' button...\"`"
    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
  ;;
  19) #181223 zarfy
   M_z1="$(gettext 'Zarfy is a GUI for managing multiple monitors. Settings are saved in folder /root/.zarfy, which will be automatically loaded everytime X starts.')"
   M_z2="$(gettext 'Note: To remove, delete /root/.zarfy, or change /root/Startup/zarfy_load to non-executable.')"
   M_z3="$(gettext 'Click OK button to run Zarfy:')"
   popup "terminate=ok background=#ffc080 process=wait level=top|<big>${M_z1}${CR}${M_z2}${CR}${M_z3}</big>"
   exec zarfy
  ;;
 esac
 exit
fi

#200121 the old cli xorgwizard was here (replaced by /usr/sbin/xorgwizard-cli).

###end###
