#! /bin/bash
#(c) Copyright Barry Kauler, Oct 2010
#License: gpl v2 (/usr/share/doc/legal/gpl-2.0.txt)
#101024 fix bug with internet connection detection.
#101027 fixed little bugs for nvidia.
#101106 make generic so will work in other woof-built puppies, not just wary.
#101106 added support to install a commercial ATI driver.
#101107 ati driver installation (aticonfig --initial) messes up xorg.conf.
#101112 fix paths to which_ati_pkg, which_nvidia_pkg.
#101114 01micko: backslash was missing.
#101116 call 'download_file' instead of running wget directly.
#101117 fglrx fix.
#101118 insert warning to install DRI pkg before upgrade. 
#101129 WRONG for intel upgrade, upgrade pkg has upgraded dri/mesa in it.
#101205 fixes. nobody seems to have got my ati pet to work, remove for now.
#101220 add gettext rodin.s
# 4jan100 shinobar: local eval_gettext

export TEXTDOMAIN=usr_sbin
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
eval_gettext () {
  local myMESSAGE=$(gettext "$1")
  eval echo \"$myMESSAGE\"
}
#. gettext.sh
export LANGORG=$LANG
#export LANG=C # off for gettext
. /etc/DISTRO_SPECS
. /root/.packages/DISTRO_PET_REPOS #101106

#if [ "$DISTRO_COMPAT_VERSION" != "wary5" ];then
# xmessage -bg pink -center "Sorry, this Wizard currently only works in Wary Puppy"
# exit
#fi
#101106 determine where to look for PETs...
PET_DB_FILE="`echo -n "$PACKAGELISTS_PET_ORDER" | cut -f 1 -d ' '`" #exs: Packages-puppy-wary5-official, Packages-puppy-lucid-official, Packages-puppy-5-official
case $DISTRO_COMPAT_VERSION in
 wary*)
  PET_URL='http://distro.ibiblio.org/pub/linux/distributions/quirky/'
 ;;
 *)
  PET_URL='http://distro.ibiblio.org/pub/linux/distributions/puppylinux/'
 ;;
esac
[ "$DISTRO_BINARY_COMPAT" = "t2" ] && PET_URL='http://distro.ibiblio.org/pub/linux/distributions/quirky/'
case $PET_DB_FILE in
 Packages-puppy-wary5-official)
  PET_FOLDER='pet_packages-wary5/'
 ;;
 Packages-puppy-lucid-official)
  PET_FOLDER='pet_packages-lucid/'
 ;;
 *)
  PET_FOLDER='pet_packages-5/'
 ;;
esac

#101107 ati driver installation (aticonfig --initial) messes up xorg.conf... 101117
if [ "`grep '^ #card0driver' /etc/X11/xorg.conf`" != "" ];then
 sed -i -e '/#card0driver/d' /etc/X11/xorg.conf
 sed -i -e 's%^\tDriver *"fglrx".*%\tDriver      "fglrx" #card0driver%' /etc/X11/xorg.conf
 #101117 just in case it is still set to radeon driver...
 sed -i -e 's%^\tDriver *"radeon".*%\tDriver      "radeon" #card0driver%' /etc/X11/xorg.conf
fi

INTERNETCNT=0
if [ "`ifconfig | grep -o '^[pwe][a-z0-9]*'`" != "" ];then #101024
 for ONEIF in `ifconfig | grep -o '^[pwe][a-z0-9]*' | tr '\n' ' '`
 do
  [ -e /sys/class/net/${ONEIF}/statistics/rx_bytes ] && [ `cat /sys/class/net/${ONEIF}/statistics/rx_bytes` -gt 0 ] && INTERNETCNT=1
 done
fi
if [ $INTERNETCNT -eq 0 ];then
 #OFFMSG="<pixmap><input file>/usr/local/video_upgrade/notconnected.png</input></pixmap>"
 #gtkdialog span tag parsing is a bit odd...
 OFFMSG="<text use-markup=\"true\"><label>\"<b><big><span color='"'red'"' background='"'yellow'"'>$(gettext 'Currently not connected to Internet! Run this Wizard later (see Setup menu), when Internet working')</span></big></b>\"</label></text>"
 #this also works...
 #OFFMSG='<text use-markup="true"><label>"<b><big><span color='"'red'"' background='"'yellow'"'>Currently not connected to Internet! Run this Wizard later (see Setup menu), when Internet working</span></big></b>"</label></text>'
fi

#101118 put up a warning to install DRI pkg first...
#101129 take this out...
#WARNMSG="<text use-markup=\"true\"><label>\"<b><big><span color='"'red'"' background='"'yellow'"'>NOTICE: Install the DRI (mesa) package FIRST, followed by upgrade: we have found DRI pkg gives improved stability</span></big></b>\"</label></text>"


PET_UPGRADE_INTEL="` grep '^xorg_upgrade_intel\-' /root/.packages/${PET_DB_FILE} | cut -f 8 -d '|' | tail -n 1`"
PET_UPGRADE_DEV="` grep '^xorg_upgrade_DEV\-' /root/.packages/${PET_DB_FILE} | cut -f 8 -d '|' | tail -n 1`"
PET_UPGRADE_DRI_EXTRA="` grep '^xorg_upgrade_dri_extra-\-' /root/.packages/${PET_DB_FILE} | cut -f 8 -d '|' | tail -n 1`"
PET_MESA="` grep '^mesa\-[0-9]' /root/.packages/${PET_DB_FILE} | cut -f 8 -d '|' | tail -n 1`"

#101129 find if any already installed...
puiPATTERN='|'"${PET_UPGRADE_INTEL}"'|'
[ "`grep "$puiPATTERN" /root/.packages/user-installed-packages`" != "" ] && FLAG_UPGRADE_INTEL='done'

#101114 01micko: backslash was missing...
echo "#!/bin/sh
PET=\"\$1\" #ex: xorg_upgrade_intel-2.10.0-w5.pet
cd /root
download_file ${PET_URL}${PET_FOLDER}\$PET" > /tmp/petupgradevideo.sh
echo '
if [ $? -ne 0 ];then
 xmessage -bg pink -center "`gettext \"Sorry, download failed\"`"
 exit 1
fi
petget /root/$PET
rm -f /root/$PET
PKGBASE="`basename $PET .pet`"
if [ ! -f /root/.packages/$PKGBASE.files ];then
 xmessage -bg pink -center "`gettext \"Sorry, something went wrong, package downloaded but is not installed\"`"
fi
' >> /tmp/petupgradevideo.sh
chmod 755 /tmp/petupgradevideo.sh

OFFER_MESA="<frame $(gettext 'Mesa DRI')><hbox>
     <text><label>$(gettext 'DRI (Direct Rendering Infrastructure) provides Xorg drivers that will speed up some graphics/video applications that use openGL. This is optional, and will only give a noticeable improvement on a small selection of applications. If you have an Internet connection, click button to download (filesize 12MB):')</label></text>
     <vbox>
       <button><input file>/usr/local/video_upgrade/dri48x29.png</input>
       <action>/tmp/petupgradevideo.sh ${PET_MESA} & </action></button>
     </vbox>
   </hbox></frame>"

VIDEODETAILS="`lspci -nn -m | grep 'VGA compatible controller'`"
#ex: 00:02.0 "VGA compatible controller [0300]" "Intel Corporation [8086]" "Mobile 945GM/GMS, 943/940GML Express Integrated Graphics Controller [27a2]" -r03 "Acer Incorporated [ALI] [1025]" "Device [0110]"
#ex: 01:00.0 "VGA compatible controller [0300]" "nVidia Corporation [10de]" "G86 [GeForce 8400M GS] [0427]" -ra1 "Acer Incorporated [ALI] [1025]" "Device [011e]"

MANUFACTURER=""
#if currently running a generic driver, offer the upgrade...
if [ "`readlink /usr/bin/X`" == "Xvesa" ];then
 CURRENTDRIVER="Xvesa"
else
 CURRENTDRIVER="`grep '#card0driver' /etc/X11/xorg.conf | cut -f 2 -d '"'`" #'geany
 MANUFACTURER="`cat /etc/X11/xorg.conf | tr '\n' ' ' | tr '\t' ' ' | tr -s ' ' | grep -o '#card0driver .*' | grep -o 'VendorName "[^"]*' | cut -f 2 -d '"'`" #'geany
fi

#if multiple cards, find one most likely to be plugin...
[ "$MANUFACTURER" != "" ] && [ "`echo -n "$VIDEODETAILS" | grep "$MANUFACTURER"`" == "" ] && MANUFACTURER=""
[ "$MANUFACTURER" == "" ] && [ "`echo "$VIDEODETAILS" | grep 'nVidia Corporation'`" != "" ] && MANUFACTURER="nVidia Corporation"
[ "$MANUFACTURER" == "" ] && [ "`echo "$VIDEODETAILS" | grep 'Intel Corporation'`" != "" ] && MANUFACTURER="Intel Corporation"
[ "$MANUFACTURER" == "" ] && MANUFACTURER="`echo -n "$ONECARD" | cut -f 4 -d '"' | rev | cut -f 2-9 -d ' ' | rev`" #'geany
MANUFID="`echo -n "$VIDEODETAILS" | grep "$MANUFACTURER" | cut -f 4 -d '"' | rev | cut -f 2 -d ']' | cut -f 1 -d '[' | rev`" #'geany
DESCRIPTION="`echo -n "$VIDEODETAILS" | grep "$MANUFACTURER" | cut -f 6 -d '"' | rev | cut -f 2-12 -d ' ' | rev | sed -e 's%[^0-9a-zA-Z_ ,/]%%g'`" #'geany
DEVICEID="`echo -n "$VIDEODETAILS" | grep "$MANUFACTURER" | cut -f 6 -d '"' | rev | cut -f 2 -d ']' | cut -f 1 -d '[' | rev`" #'geany

HEADERMSG="<text><label>Your computer has an '${MANUFACTURER}' video chip:</label></text>
   <text use-markup=\"true\"><label>\"<b>${DESCRIPTION}</b>
PCI chip numbers: ${MANUFID}:${DEVICEID}\"</label></text>"

case "$MANUFACTURER" in
 "Intel Corporation") #8086:
  case $CURRENTDRIVER in
   vesa)
    GENERICMSG="<text><label>`eval_gettext \"X is currently using '\\\${CURRENTDRIVER}' which is a generic unaccelerated driver. This driver is slow and you may notice the sluggishness under certain circumstances. If you want snappier performance, you might want to try this upgrade:\"`</label></text>"
   ;;
   Xvesa)
    GENERICMSG="<text><label>`eval_gettext \"X is currently using '\\\${CURRENTDRIVER}' which is a generic unaccelerated driver. This driver is slow and you may notice the sluggishness under certain circumstances. Xvesa also has other limitations as it is not a true Xorg driver, for example the screen refresh frequency is not adjustable and you might notice flicker. You might also find video playing to be restricted. If you want snappier performance, you might want to try this upgrade:\"`</label></text>"
   ;;
   *)
    GENERICMSG="<text><label>`eval_gettext \"X is currently using the '\\\${CURRENTDRIVER}' driver which is the correct driver for this chip. You really don't need to upgrade. However, some applications will be snappier if you install the Mesa package:\"`</label></text>
    ${OFFER_MESA}
    <text use-markup=\"true\"><label>\"<span color='"'red'"' background='"'yellow'"'>$(gettext 'It is recommended that you do <b>NOT</b> click any of the buttons below!')</span>\"</label></text>"
    WARNMSG="" #101118
   ;;
  esac
  WARNMSG="" #101129
  INTEL_DEV_MSG="    <frame $(gettext 'Developer files')><hbox>
     <text><label>$(gettext 'If you are a developer, or have any need to compile packages, then also download these two packages. The first has the extra files needed for compiling with the upgraded X. Note that this package may enable you to compile other advanced Xorg drivers, other than Intel, that will not work with the original X server and/or require KMS. The second package has DRI drivers for non-Intel hardware. If you are a user and know nothing about compiling, do not download these packages.')</label></text>
     <vbox>
       <button><input file>/usr/local/lib/X11/pixmaps/x24.png</input>
       <action>/tmp/petupgradevideo.sh ${PET_UPGRADE_DEV} & </action></button>
       <button><input file>/usr/local/lib/X11/pixmaps/x24.png</input>
       <action>/tmp/petupgradevideo.sh ${PET_UPGRADE_DRI_EXTRA} & </action></button>
     </vbox>
   </hbox></frame>" #101205
  OFFER_MSG="<frame $(gettext 'Upgrade Intel driver')>${WARNMSG}<hbox>
     <text><label>$(gettext 'Clicking this button will download and install Xorg server 1.6.0 and Intel driver 2.10.0. Please note that this is a fairly radical upgrade, that will run the kernel with KMS (Kernel Mode Setting) turned on and upgrades many files. If you have an Internet connection, click button (filesize is 7.5MB):')</label></text>
     <vbox>
       <button><input file>/usr/local/video_upgrade/intel48x32.png</input>
       <action>/tmp/petupgradevideo.sh ${PET_UPGRADE_INTEL} & </action></button>
     </vbox>
   </hbox></frame>
   ${INTEL_DEV_MSG}
"
  if [ "$FLAG_UPGRADE_INTEL" = "done" ];then #101129
   GENERICMSG=""
   OFFER_MSG="<text use-markup=\"true\"><label>\"<span color='"'red'"' background='"'yellow'"'>`eval_gettext \"Upgrade package '\\\${PET_UPGRADE_INTEL}' already installed\"`</span>\"</label></text>
   ${INTEL_DEV_MSG}"
  fi
 ;;
 "nVidia Corporation") #10DE:
  /usr/local/video_upgrade/which_nvidia_pkg $DEVICEID $PET_DB_FILE #writes to /tmp/vuw_nvidia_pkg. 101106 10112
  PET_NVIDIA="`cat /tmp/vuw_nvidia_pkg`"
  case $CURRENTDRIVER in
   vesa)
    GENERICMSG="<text><label>`eval_gettext \"X is currently using '\\\${CURRENTDRIVER}' which is a generic unaccelerated driver. This driver is slow and you may notice the sluggishness under certain circumstances. If you want snappier performance, you might want to try this upgrade:\"`</label></text>"
   ;;
   Xvesa)
    GENERICMSG="<text><label>`eval_gettext \"X is currently using '\\\${CURRENTDRIVER}' which is a generic unaccelerated driver. This driver is slow and you may notice the sluggishness under certain circumstances. Xvesa also has other limitations as it is not a true Xorg driver, for example the screen refresh frequency is not adjustable and you might notice flicker. You might also find video playing to be restricted. If you want snappier performance, you might want to try this upgrade:\"`</label></text>"
   ;;
   nv)
    GENERICMSG="<text><label>`gettext \"X is currently using the 'nv' driver, which is the correct driver for this video hardware. This driver is satisfactory. However, some applications will be snappier if you install the Mesa package:\"`</label></text>
    ${OFFER_MESA}
    <text use-markup=\"true\"><label>\"`gettext \"Alternatively: the 'nv' driver may be noticeably slow with some graphics/video applications, and if you want faster rendering and more control over the display settings, you might want to <b>try the commercial nVidia driver</b>:\"`\"</label></text>"
   ;;
   nvidia)
    GENERICMSG="<text use-markup=\"true\"><label>\"<b>`gettext \"Currently using the 'nvidia' driver, so you have already upgraded!\"`</b>\"</label></text>"
   ;;
  esac
  if [ "$PET_NVIDIA" == "" ];then
   OFFER_MSG="<frame $(gettext 'Commercial nVidia driver')><text use-markup=\"true\"><label>\"<b>$(gettext 'Sorry, do not have a PET package that supports your video chip. Suggest that you go to the nVidia website and download a driver (look in Puppy Forum for other users experiences with doing this)')</b>\"</label></text></frame>"
  else
   PETSIZE=`grep "$PET_NVIDIA" /root/.packages/${PET_DB_FILE} | head -n 1 | cut -f 6 -d '|' | rev | cut -c 2-9 | rev`
   PETSIZE2=`expr $PETSIZE \/ 3`
   SIZEUNIT="`grep "$PET_NVIDIA" /root/.packages/${PET_DB_FILE} | head -n 1 | cut -f 6 -d '|' | rev | cut -c 1`"
   UNITS="KB"
   [ "$SIZEUNIT" = "M" ] && UNITS="MB"
   OFFER_MSG="<frame $(gettext 'Commercial nVidia driver')>${WARNMSG}<hbox>
     <text><label>`eval_gettext \"Clicking this button will download and install '\\\${PET_NVIDIA}'. This is a closed-source commercial product, provided by nVidia Corporation. It gives maximum performance and also has a GUI to finetune the settings. (when installed will occupy \\\${PETSIZE}\\\${UNITS}, the PET package to download is approx.  \\\${PETSIZE2}\\\${UNITS}):\"`</label></text>
     <vbox>
       <button><input file>/usr/local/video_upgrade/nvidia48x32.png</input>
       <action>/tmp/petupgradevideo.sh ${PET_NVIDIA} & </action></button>
     </vbox>
   </hbox></frame>"
  fi
 ;;
 "ATI Technologies Inc") #1002:
  /usr/local/video_upgrade/which_ati_pkg $DEVICEID $PET_DB_FILE #writes to /tmp/vuw_ati_pkg. 101106 101112
  PET_ATI="`cat /tmp/vuw_ati_pkg`"
  case $CURRENTDRIVER in
   vesa)
    GENERICMSG="<text><label>`gettext \"X is currently using 'vesa' which is a generic unaccelerated driver. This driver is slow and you may notice the sluggishness under certain circumstances. If you want snappier performance, you might want to try this upgrade:\"`</label></text>"
   ;;
   Xvesa)
    GENERICMSG="<text><label>`gettext \"X is currently using 'Xvesa' which is a generic unaccelerated driver. This driver is slow and you may notice the sluggishness under certain circumstances. Xvesa also has other limitations as it is not a true Xorg driver, for example the screen refresh frequency is not adjustable and you might notice flicker. You might also find video playing to be restricted. If you want snappier performance, you might want to try this upgrade:\"`</label></text>"
   ;;
   *)
    GENERICMSG="<text><label>`eval_gettext \"X is currently using the '\\\${CURRENTDRIVER}' driver, which is the correct driver for this video hardware. This driver is satisfactory. However, some applications will be snappier if you install the Mesa package:\"`</label></text>
    ${OFFER_MESA}
    <text use-markup=\"true\"><label>\"<b>`eval_gettext \"Alternatively:</b> the '\\\${CURRENTDRIVER}' driver is fine, but for even faster graphics/video rendering and more control over the display settings, you might want to <b>try the commercial ATI driver\"`</b>:\"</label></text>"
   ;;
  esac
  #101205 nobody seems to have got my ati pet to work, remove for now...
  #if [ "$PET_ATI" == "" ];then #101106
   OFFER_MSG="<frame $(gettext 'Commercial ATI driver')><pixmap><input file>/usr/local/video_upgrade/ati48x32.png</input></pixmap><text use-markup=\"true\"><label>\"<b>$(gettext 'Sorry, do (yet) not have a PET package that supports your video chip. Suggest that you go to the ATI website and download a driver (look in Puppy Forum for other users experiences with doing this)')</b>\"</label></text></frame>"
  #else
  # PETSIZE=`grep "$PET_ATI" /root/.packages/${PET_DB_FILE} | head -n 1 | cut -f 6 -d '|' | rev | cut -c 2-9 | rev`
  # PETSIZE2=`expr $PETSIZE \/ 3`
  # SIZEUNIT="`grep "$PET_ATI" /root/.packages/${PET_DB_FILE} | head -n 1 | cut -f 6 -d '|' | rev | cut -c 1`"
  # UNITS="KB"
  # [ "$SIZEUNIT" = "M" ] && UNITS="MB"
  # OFFER_MSG="<frame Commercial ATI driver>${WARNMSG}<hbox>
  #   <text><label>Clicking this button will download and install '${PET_ATI}'. This is a closed-source commercial product, provided by ATI Corporation. It gives maximum performance and also has a GUI to finetune the settings. (when installed will occupy ${PETSIZE}${UNITS}, the PET package to download is approx.  ${PETSIZE2}${UNITS}):</label></text>
  #   <vbox>
  #     <button><input file>/usr/local/video_upgrade/ati48x32.png</input>
  #     <action>/tmp/petupgradevideo.sh ${PET_ATI} & </action></button>
  #   </vbox>
  # </hbox></frame>"
  #fi
 ;;
 *)
  case $CURRENTDRIVER in
   vesa)
    GENERICMSG="<text><label>`gettext \"X is currently using 'vesa' which is a generic unaccelerated driver. This driver is slow and you may notice the sluggishness under certain circumstances, however Wary does not have a hardware-specific driver for your video chip.\"`</label></text>"
   ;;
   Xvesa)
    GENERICMSG="<text><label>`gettext \"X is currently using 'Xvesa' which is a generic unaccelerated driver. This driver is slow and you may notice the sluggishness under certain circumstances. Xvesa also has other limitations as it is not a true Xorg driver, for example the screen refresh frequency is not adjustable and you might notice flicker. You might also find video playing to be restricted. If possible, you should use the 'vesa' Xorg driver, that is another generic driver but is a true Xorg driver and is in msome ways superior. Sorry, but Wary does not have a hardware-specific Xorg driver for your video chip.\"`</label></text>"
   ;;
   *)
    GENERICMSG="<text><label>`eval_gettext \"X is currently using the '\\\${CURRENTDRIVER}' driver which is the correct driver for this chip. So, you have nothing more to do, however, some applications will be snappier if you install the Mesa package:\"`</label></text>
    ${OFFER_MESA}"
   ;;
  esac
 ;;
esac

#   <hbox>
#     <vbox>
#       <button><input file>/usr/local/video_upgrade/nvidia48x32.png</input></button>
#     </vbox>
#   </hbox>
#   <hbox>
#     <vbox>
#       <button><input file>/usr/local/video_upgrade/ati48x32.png</input></button>
#     </vbox>
#   </hbox>


export VIDEOUPGRADE_DLG="<window title=\"$(gettext 'Video Driver Upgrade Wizard')\" icon-name=\"gtk-select-color\">
 <vbox>
   ${OFFMSG}
   ${HEADERMSG}
   ${GENERICMSG}
   ${OFFER_MSG}
   
   <hbox>
     <button><input file>/usr/local/lib/X11/mini-icons/mini.exit.xpm</input><action type=\"exit\">$(gettext 'EXIT')</action></button>
   </hbox>
 </vbox>
</window>
"

gtkdialog3 --program=VIDEOUPGRADE_DLG


