#!/bin/sh
#120204 rodin.s: internationalized.
#120323 replace 'xmessage' with 'pupmessage'.
#120331 ping -4 for ipv4, refer: http://murga-linux.com/puppy/viewtopic.php?p=616090#616090
#120525 start fixing this to work with all puppies (not just wary).
#131209 Packages-puppy- files renamed to Packages-pet-

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

. /etc/DISTRO_SPECS

#[ "$DISTRO_COMPAT_VERSION" != "wary5" ] && exec /usr/local/video_upgrade/video_upgrade_wizard-OLD
#if vercmp $DISTRO_VERSION lt 5.1.96;then
# exec /usr/local/video_upgrade/video_upgrade_wizard-OLD
#fi

#new upgrade script, just getting started...
INSERT0=""

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

INSERT0=""
INSERT1=""
INSERT1b=""
INSERT2=""
case $DISTRO_COMPAT_VERSION in
 wary5)
  if vercmp $XORGVER gt 1.5;then
   #my xorg 7.6 mega pkg has server 1.11.0
   INSERT1="<text><label>`eval_gettext \"Xorg server version '\\\${XORGVER}' is currently running, so it seems that you have already upgraded Xorg.\"`</label></text>"
  else
   #offer to upgrade...
   ping -4 -c 1 www.google.com
   if [ $? -ne 0 ];then
    INSERT0="<text use-markup=\"true\"><label>\"<b>`gettext \"Currently not connected to the Internet. If you wish to connect later, you can run this Video Upgrade Wizard again via the 'setup' icon on the desktop or the 'Setup' category in the menu. Note, to connect to the Internet, click 'connect' icon on left of desktop.\"`</b>\"</label></text>"
   fi
   INSERT1="<frame $(gettext 'Xorg 7.6 Mega Package')>
   <hbox>
    <text><label>$(gettext 'Wary can be upgraded from Xorg 7.3 to Xorg 7.6. The PET is large, 21MB. Click button to download and install it:')</label></text>
    <vbox>
     <button>
      <input file>/usr/local/lib/X11/pixmaps/pet48x.png</input>
      <action>EXIT:xorg76pet</action>
     </button>
    </vbox>
   </hbox>
   </frame>"
   INSERT2="<text use-markup=\"true\"><label>\"<b>`gettext \"To decline upgrade, click 'OK' button:\"`</b>\"</label></text>"
  fi
 ;;
 *)
  INSERT1="<text><label>`eval_gettext \"Xorg server version '\\\${XORGVER}' is currently running.\"`</label></text>"
  INSERT1b="<text><label>$(gettext 'This Wizard is not offering any upgrades.')</label></text>"
 ;;
esac

export VIDEOUPGRADE_DLG="<window title=\"$(gettext 'Video Driver Upgrade Wizard')\" icon-name=\"gtk-select-color\">
 <vbox>
  ${INSERT0}
  ${INSERT1}
  ${INSERT1b}
  <hbox>
   ${INSERT2}
   <button ok></button>
  </hbox>

 </vbox>
</window>
"

RETVALS="`gtkdialog3 --program=VIDEOUPGRADE_DLG`"

if [ "`echo "$RETVALS" | grep 'xorg76pet'`" != "" ];then
 #x_xorg76_mega_pkg-7.6-w5c|x_xorg76_mega_pkg|7.6-w5c||Setup|17388K||x_xorg76_mega_pkg-7.6-w5c.pet||upgrade Wary from Xorg 7.3 to 7.6|puppy|wary5||
 PETPKG="`grep '|x_xorg76_mega_pkg|' /root/.packages/Packages-pet-wary5-official | cut -f 8 -d '|'`"
 [ "$PETPKG" = "" ] && PETPKG="x_xorg76_mega_pkg-7.6-w5c.pet" #precaution.
 cd /root
 download_file http://distro.ibiblio.org/quirky/pet_packages-wary5/${PETPKG}
 if [ $? -ne 0 ];then
  pupmessage -bg pink -center "$(gettext 'Sorry, download failed')"
 else
  petget /root/$PETPKG
  rm -f /root/$PETPKG
  PKGBASE="`basename $PETPKG .pet`"
  if [ ! -f /root/.packages/$PKGBASE.files ];then
   pupmessage -bg pink -center "$(gettext 'Sorry, something went wrong, package downloaded but is not installed')"
  fi
 fi
fi
