#!/bin/bash -a
# getflash 0.5 original code by technosaurus and by Barry K. http://bkhome.org/blog/?viewDetailed=01897
# 13 Nov 2011 01micko http://www.murga-linux.com/puppy/viewtopic.php?t=72805&start=201
# v0.6 - 21 Dec 2011 shinobar: puppy version independent (may work 4.0 and later), flashblock checkbox, registration, i18n
# v0.8 - 11 Jan 2012 shinobar: flashplayer version info (stu90), fix was not able to uninstall
# v0.9 - 25 Mar 2012 shinobar: fix was the ownership under /usr messing up
# v0.9.9 -  3 May 2012 shinobar: version choice, jwm -reload for no flicker(01mico)
# v1.0 - 5 May 2012 shinobar: flashplayer11-11.2.202.228, insert sudo line
# v1.1 - 9 Jun 2012 shinobar: URL changed, download size
# v1.2 - 21 Jul 2012 shinobar: fix download pets,flashplayer10.3.183.23
# v1.3 - 8 Oct 2013: change adobe url (ETP), forum URL by OscarTalks
# v1.4 - 23 Nov 2013: fix was always offer flashblock
# v1.5 - 4 Feb 2014: flashplayer10-10.3.183.90 from smokey01.com
#150305 BK adobe site now https:. fix pet repo. fix register with ppm.
#150310 BK: weird, some testers report that http: only works. test for either.
#150311 BK: Argolance: bug fixes. refer: http://murga-linux.com/puppy/viewtopic.php?p=833174#833174
#150311 yaf-splash (gtkdialog-splash) is deprecated in Quirky, in favour of 'popup'
#150315 ASRI: try to improve main GUI, improve gettext, add conf file. And more...
#150325 ASRI: substitute "strings" with "busybox strings" (Barry proposal)
#151028 v1.5-7 ASRI: add function autoupdate at startup, improve messages (SEE_*) of main GUI, stop to propose flashplayer9, improve test of download size, improve support of x64 arch, add options for boot (getflash -a -h), add prefs file, and other small changes...
#151108 1.5-8 - move some configs, fix pinstall.sh, set dafault autocheck to "false"

AppVersion=1.5-8


## ANL = Application Name writing Lowercase => to be use in different parts of the script
ANL01="getflash"


## Get language settings (gettext method)
export TEXTDOMAIN=getflash
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
LANG_USER=$LANG
. gettext.sh


## Application : name
AppTitle01="$(gettext 'GetFlashPlayer')"


## Get Application dir (zigbert method)
PREFDIR=$HOME/.getflash


## Check data and variables/settings for script 
. /etc/DISTRO_SPECS
APP_CONF="$PREFDIR/$ANL01-conf"
APP_PREFS="$PREFDIR/$ANL01-prefs"
. $APP_CONF
. $APP_PREFS


## Test processor arch
ARCH=$(uname -m)
lsuffix=
case "$ARCH" in #01micko, support 64 bit. 150305 BK now https: 150310 maybe either:
 i?86)CHECKURL=$CHECKURL_32
      FORUMURL=$FORUMURL_FLASKPKGS_32
      ARCHSEE=i386
 ;;
 *64)CHECKURL=$CHECKURL_64
     FORUMURL=$FORUMURL_FLASKPKGS_64
     ARCHSEE=x86_64
     lsuffix=64
 ;;
 *)exit 1
 ;;
esac


## Check version of flash player installed
check_flash_version_installed() {
if [ ! -f /usr/lib${lsuffix}/mozilla/plugins/libflashplayer.so ];then
  FLASHVERSION="0"
  TEXT=$(gettext "Currently, Flash player plugin is not installed.")
  TEXT2=$(gettext "Note: currently, Flash player plugin is not installed.")
else
  FLASHVERSION=$(busybox strings -n13 /usr/lib${lsuffix}/mozilla/plugins/libflashplayer.so  | grep 'FlashPlayer_' | cut -d '_' -f2-5| tr '_' '.')
  #echo "INSTALLED=$FLASHVERSION" >&2
  TEXT=$(gettext "Currently, Flash player plugin v%s is installed.")
  TEXT=$(printf "$TEXT" "$FLASHVERSION")
  TEXT2=$(gettext "Note: currently, Flash player plugin v%s is installed.")
  TEXT2=$(printf "$TEXT2" "$FLASHVERSION")
fi
}
check_flash_version_installed


## Define messages for script
T_title="$AppTitle01 v$AppVersion"
WAIT_PLS="$(eval_gettext '$AppTitle01 - Please wait, updating Flash player plugin...')"
_WHICH=$(gettext "Select the version to install")
_UNABLE_INTERNET="$(gettext 'No internet connection founded, please check your configuration, then restart') $AppTitle01.

$TEXT2"
_UNABLE_SERVER="$(gettext 'Unable to contact the server, please check your internet connection, then restart') $AppTitle01.

$TEXT2"


## Various tests/configs
[ "$UID" != "0" ] && which sudo &>/dev/null exec sudo $0

[ "$HOME" ] || HOME='/root'


## Check gtkdialog version
GTKDIALOG=gtkdialog3
which gtkdialog4 &>/dev/null && GTKDIALOG=gtkdialog4


## Various tests to define applications used
MYBROWSER=defaultbrowser # defaulthtmlviewer is possible

RXVT=urxvt
which $RXVT &>/dev/null || RXVT=rxvt
#TERMCONF="$RXVT -bg orange -geometry 80x4 -e"
TERMCONF="$RXVT -bg black -fg yellow -geometry 110x12+0+200 -e"









#### Function part

export X1PID=""

#150311 yaf-splash (gtkdialog-splash) is deprecated in Quirky, in favour of 'popup'
POPUPFLAG=0
[ `which popup` ] && POPUPFLAG=1
#[ `which popadd` ] || POPUPFLAG=0


mysplash() {
  if [ $POPUPFLAG -eq 1 ];then #150311
   [ `pidof popup` ] && killall popup #popadd 'name=gfpop terminate=now|'
  else
	[ "$X1PID" ] && kill $X1PID
	killall yaf-splash &>/dev/null
	X1PID=""
	BGCOLOR="orange"
	MYICON="gtk-info"
	TIMEOUT=50
  fi
  while [ "$1" ]; do
	case "$1" in
	-*stop) return;;
	-*bg) shift; BGCOLOR=$1; shift;;
	-*timeout) shift; TIMEOUT=$1; shift;;
	-*icon) shift; MYICON=$1; shift;;
	-*text) shift;;
	-*) shift; shift;;
	*) break;;
	esac
  done
  if [ $POPUPFLAG -eq 1 ];then #150311
   case $BGCOLOR in #hack
    yellow)    BACKCOL='#FFFF00' ;;
    LightPink|hotpink) BACKCOL='#FF30FF' ;;
    orange)    BACKCOL='#FFB030' ;;
    *)         BACKCOL='#FFFF00' ;;
   esac
   LANG=$LANG_USER popup "name=gfpop placement=center fontname='Sans 14' background=${BACKCOL} terminate=${TIMEOUT} level=top|${@}" #daemonizes
  else
	if which gtkdialog-splash &>/dev/null ; then
	  LANG=$LANG_USER gtkdialog-splash -fontsize "large" -icon $MYICON -placement center -bg $BGCOLOR -timeout $TIMEOUT -close never -text "$@" & #150311
	else
	  LANG=$LANG_USER gxmessage -center -wrap -bg $BGCOLOR -title "$T_title" -buttons ''  -timeout $TIMEOUT -nofocus "$@" &
	fi
	X1PID=$!
  fi
}
#mysplash -bg yellow "$WAIT_PLS"


internet_status() {
  LANG=C route | grep -q 'default[ ].*[ ]0\.0\.0\.0[ ]' && grep -wq 'nameserver' /etc/resolv.conf #&& ping -c1 google.com &>/dev/null
}


help_gui() {
Xdialog --title "$T_title" --left --wrap --msgbox "$(eval_gettext 'About $AppTitle01 utility:
- This utility facilitates the installation/update of the Flash player plugin.
- Expert users can edit manualy the configuration file of $AppTitle01 (/usr/sbin/getflash-conf).

About Flash player plugin:
- The plugin is used by web browsers installed in your system.
- The version of the plugin must be compatible with the architecture of your operating system (32 or 64 bits).
- The plugin is saved in folder /usr/lib/mozilla/plugins.
- Latest versions of the plugin may be incompatible with some old CPU, in this case choose an older version of the plugin.
- The latest version of the plugin is downloaded directly from the Adobe server, older versions are downloaded from servers of Puppy users.
- To manualy download the latest version of the plugin, consult $DLALLVERSIONS
- Flash player is a proprietary software proposed by the Adobe company. You can consult the general terms at $ADOBE_GENERAL_TERMS')" 0 120
}


## Check Internet connection, define messages, then start main GUI
   #mysplash -bg yellow "$WAIT_PLS"
    if ! internet_status; then
      #mysplash -bg hotpink -timeout 5 -icon gtk-dialog-error "$_UNABLE_INTERNET"
      #mysplash --stop
      Xdialog --title "$T_title" --left --wrap --msgbox "$_UNABLE_INTERNET" 0 80
      exit 1
    fi

     #111113 changed url, scrapped ping
     #DOWNLOADURL="`curl -s 'http://get.adobe.com/flashplayer/completion/?installer=Flash_Player_11_for_other_Linux_(.tar.gz)_32-bit'|tr ' ' '\n'|grep fpdownload|tr -d ';'`"
     #v1.1: CHECKURL, grep -o
     #DOWNLOADURL=$(curl -s "$CHECKURL" | grep -o 'http://fpdownload.*inux.i386.tar.gz')
     
     case "$ARCH" in #150305 now https
      i?86)DOWNLOADURL=$(curl -s "https${CHECKURL}" | grep -o 'http.://fpdownload.*inux.i386\.tar\.gz') ;;
      *64)DOWNLOADURL=$(curl -s "https${CHECKURL}" | grep -o 'http.://fpdownload.*inux.x86_64\.tar\.gz') ;;
     esac
     if [ ! "$DOWNLOADURL" ];then #150310
      case "$ARCH" in
       i?86)DOWNLOADURL=$(curl -s "http${CHECKURL}" | grep -o 'http.://fpdownload.*inux.i386\.tar\.gz') ;;
       *64)DOWNLOADURL=$(curl -s "http${CHECKURL}" | grep -o 'http.://fpdownload.*inux.x86_64\.tar\.gz') ;;
      esac
     fi
     if [ ! "$DOWNLOADURL" ];then 
      #mysplash -bg hotpink -timeout 5 -icon gtk-dialog-error "$_UNABLE_SERVER"
      #mysplash --stop
      Xdialog --title "$T_title" --left --wrap --msgbox "$_UNABLE_SERVER" 0 80
      exit 1
     fi
    #echo "$DOWNLOADURL" >&2
     NEWVERSION=$(dirname "$DOWNLOADURL")
     NEWVERSION=$(basename "$NEWVERSION")
     #echo "NEWVERSION=$NEWVERSION"
     #v1.1 download size
     LENGTH=$(curl -s -k -I "$DOWNLOADURL" | grep -i 'Content-Length:' | cut -s -f2 -d':'| tr -dc '0-9') || LENGTH=0 #20151023 Test curl: add -k to allow connections to SSL sites without certs (Adobe is a example)
     NEWMB=$((($LENGTH + 52429) / 104858))
     if [ $NEWMB -gt 0 ] ; then
       L=$(echo -n $NEWMB | wc -c)
       M=$(($L -1))
       NEWMB="$(echo $NEWMB -n | cut -c-$M).$(echo -n $NEWMB | cut -c$L) $(gettext 'MB')"
     else
      NEWMB="$(gettext "unknown size")" #150311
     fi
#DOWNTEXT=$(gettext "Click the 'Yes' button to download and install FlashPlayer-%s (6MB).")
#DOWNTEXT=$(printf "$DOWNTEXT" "$NEWVERSION")
  if [ "$FLASHVERSION" = "$NEWVERSION" ]; then
    TEXT="$TEXT $(gettext "This is the latest version available, so you do not need to update it.")"
  else
    TEXT="$TEXT $(gettext "It is recommended to install the latest version available") (v$NEWVERSION)."
  fi


update_conf_file() {
cd /tmp
$TERMCONF wget -L "$CONFFILE_URL"
sleep 2 # may be need for slower computers

if [ ! -f /tmp/getflash-conf ]; then
Xdialog --title "$T_title" --left --wrap --msgbox "$(eval_gettext 'No configuration file was found on the server. Please try again later.
If the problem persists permanently, thank you to report on the $AppTitle01 forum.')" 0 100
	else
	mv -f "/tmp/getflash-conf" "$PREFDIR/getflash-conf"
fi
}











#### GUI part

## Define messages, icons and size of GUI
MAIN_ICON="flash-player-properties"
FLASH_ICON="/usr/share/doc/flashlogo.gif"
MAIN_ICON_WIDTH1="60"
TEXT_WIDTH_CHARS1="10"
TEXT_WIDTH1="500"
SEE_FRAME_AUTOUPDATE=$(gettext "Automatic search for an update at startup")
SEE_FRAME_FORUM=$(gettext "Forums")
SEE_AUTOUPDATE_MAINGUI_TOOLTIP=$(eval_gettext "Activate main interface of $AppTitle01.")
SEE_INSTALL_LASTVERSION_TEXT=$(gettext "Latest version available")
SEE_INSTALL_LASTVERSION_TOOLTIP=$(gettext "Install now the last version of the Flash Player plugin.")
SEE_INSTALL_LASTVERSION_LABEL=$(gettext "Install")
SEE_AUTOUPDATE_CHECK_TOOLTIP=$(gettext "Activate automatic search for an update at startup of the session (check if a new version of Flash player plugin is available, then suggest to install it).")
SEE_AUTOUPDATE_CHECK_LABEL=$(gettext "Activate automatic search for an update")
SEE_AUTOUPDATE_SILENT_TOOLTIP=$(gettext "The installation will be silent: no confirmation will be asked to the user before installation (which will happen automatically if an update is detected at the startup of the session).")
SEE_AUTOUPDATE_SILENT_LABEL=$(gettext "Silent installation")
SEE_AUTOUPDATE_DELAY_TOOLTIP=$(gettext "Define the delay (in seconds) before startup of automatic search for an update (at startup of the session). This delay is necessary to wait until the Internet connection is active (the required duration depends on your hardware).")
SEE_AUTOUPDATE_DELAY_LABEL=$(gettext "Delay before automatic search: ")
SEE_FORUM_LABEL_PKGS32=$(gettext "Flash player packages")
SEE_FORUM_LABEL_PKGS64=$(gettext "Flash player packages")
SEE_FORUM_LABEL_GETFLASH=$(eval_gettext "$AppTitle01 utility")
SEE_FORUM_LABEL=$(gettext "Forum")
SEE_FORUM_TOOLTIP=$(gettext "Use the web browser to consult the forum.")
SEE_CANCEL_LABEL=$(gettext "Cancel")
SEE_CANCEL_TOOLTIP=$(gettext "Cancel the installation.")
SEE_INSTALL_LABEL=$(gettext "Install")
SEE_INSTALL_TOOLTIP=$(gettext "Install selected version.")
SEE_HELP_TOOLTIP=$(eval_gettext "Consult help for $AppTitle01.")
SEE_EDITCONF_TOOLTIP=$(eval_gettext "For expert users! Use default text editor to edit configuration file of $AppTitle01. Note: after edit, you will need to click on Refresh button to update informations (available versions of Flash player).")
SEE_REFRESH_TOOLTIP=$(eval_gettext "Restart $AppTitle01 to refresh informations (available versions of Flash player).")
SEE_UPDATECONF_TOOLTIP=$(eval_gettext "Update configuration file of $AppTitle01. It is need if last version of Flash player plugin is not detected. If this does not work, thank you to report on the $AppTitle01 forum.")
SEE_QUITAPP_TOOLTIP=$(eval_gettext "Quit $AppTitle01.")
SEE_LICENSE_TOOLTIP=$(gettext "Use the web browser to consult general terms about Adobe Flash player.")
SEE_FORUM=


## Check if libstardust (zigbert prod) is installed
CHECK_LIBSTARDUST="/usr/lib/gtkdialog/xml_scalegrip"
if [ ! -f "$CHECK_LIBSTARDUST" ]; then
SCALEGRIP=""
	else
	SCALEGRIP="`/usr/lib/gtkdialog/xml_scalegrip`"
fi


## Define forum buttons
if [ "$FORUMURL_FLASKPKGS_32" ]; then
 if [ "$DISTRO_TARGETARCH" == "x86" ];then #150305
  SEE_FORUM='<frame '$SEE_FRAME_FORUM'>
  <hbox><text><label>"'$SEE_FORUM_LABEL_GETFLASH'"</label></text>
  <button tooltip-text="'$SEE_FORUM_TOOLTIP'"><input file stock="gtk-network"></input><label>"'$SEE_FORUM_LABEL'"</label>
  <action>'$MYBROWSER' '$FORUMURL_GETFLASHAPP' &</action>
  </button></hbox>
  <hbox><text><label>"'$SEE_FORUM_LABEL_PKGS32'"</label></text>
  <button tooltip-text="'$SEE_FORUM_TOOLTIP'"><input file stock="gtk-network"></input><label>"'$SEE_FORUM_LABEL'"</label>
  <action>'$MYBROWSER' '$FORUMURL_FLASKPKGS_32' &</action>
  </button></hbox>
  </frame>'
 else
  SEE_FORUM='<frame '$SEE_FRAME_FORUM'>
  <hbox><text><label>"'$SEE_FORUM_LABEL_GETFLASH'"</label></text>
  <button tooltip-text="'$SEE_FORUM_TOOLTIP'"><input file stock="gtk-network"></input><label>"'$SEE_FORUM_LABEL'"</label>
  <action>'$MYBROWSER' '$FORUMURL_GETFLASHAPP' &</action>
  </button></hbox>
  <hbox><text><label>"'$SEE_FORUM_LABEL_PKGS64'"</label></text>
  <button tooltip-text="'$SEE_FORUM_TOOLTIP'"><input file stock="gtk-network"></input><label>"'$SEE_FORUM_LABEL'"</label>
  <action>'$MYBROWSER' '$FORUMURL_FLASKPKGS_64' &</action>
  </button></hbox>
  </frame>'
 fi
fi


FRAMEAUTOUPDATE='<frame '$SEE_FRAME_AUTOUPDATE'>
	<hbox>
	<vbox>
	<checkbox tooltip-text="'$SEE_AUTOUPDATE_CHECK_TOOLTIP'">
	<label>"'$SEE_AUTOUPDATE_CHECK_LABEL'"</label>
	<default>'"$AUTOUPDATECHECK"'</default>
	<variable>AUTOUPDATECHECK</variable>
	<action>if true enable:AUTOUPDATESILENTINSTALL</action>
	<action>if false disable:AUTOUPDATESILENTINSTALL</action>
	<action>if true enable:AUTOUPDATESTARTUPDELAY</action>
	<action>if false disable:AUTOUPDATESTARTUPDELAY</action>
	<action>if true sed -i "s%$AUTOUPDATEOLDPREF%$AUTOUPDATEPREFTRUE%" $APP_PREFS</action>
	<action>if false sed -i "s%$AUTOUPDATEOLDPREF%$AUTOUPDATEPREFFALSE%" $APP_PREFS</action>
	</checkbox>
	</vbox>
	</hbox>
	<hbox>
	<vbox>
	<checkbox tooltip-text="'$SEE_AUTOUPDATE_SILENT_TOOLTIP'">
	<label>"'$SEE_AUTOUPDATE_SILENT_LABEL'"</label>
	<default>'"$AUTOUPDATESILENTINSTALL"'</default>
	<variable>AUTOUPDATESILENTINSTALL</variable>
	<sensitive>'$AUTOUPDATECHECK'</sensitive>
	<action>if true sed -i "s%$AUTOUPDATESILENTOLDPREF%$AUTOUPDATESILENTPREFTRUE%" $APP_PREFS</action>
	<action>if false sed -i "s%$AUTOUPDATESILENTOLDPREF%$AUTOUPDATESILENTPREFFALSE%" $APP_PREFS</action>
	</checkbox>
	</vbox>
	</hbox>
	<hbox>
	<vbox>
	<text><label>"'$SEE_AUTOUPDATE_DELAY_LABEL'"</label></text>
	</vbox>		
	<vbox>
	<spinbutton tooltip-text="'$SEE_AUTOUPDATE_DELAY_TOOLTIP'" range-min="20" range-max="240" range-step="10" range-value="'$AUTOUPDATESTARTUPDELAY'" editable="false">
	<variable>AUTOUPDATESTARTUPDELAY</variable>
	<sensitive>'$AUTOUPDATECHECK'</sensitive>
	<action>sed -i "s%$AUTOUPDATESTARTUPDELAYOLDPREF%$AUTOUPDATESTARTUPDELAYNEWPREF_a$AUTOUPDATESTARTUPDELAY$AUTOUPDATESTARTUPDELAYNEWPREF_b"% $APP_PREFS</action>
	</spinbutton>
	</vbox>
	</hbox>
  </frame>'



## Funtion update
function UpdateFlashNow (){
      if [ "$CHOICE" = "V11X" ]; then
       cd /usr/lib${lsuffix}/mozilla/plugins
       #echo -e "#!/bin/sh\nwget -O - http://fpdownload.macromedia.com/get/flashplayer/current/install_flash_player_11_linux.i386.tar.gz | tar -xz" > /tmp/delayedrun_flashdl
       # v0.9: fix the ownership
       if [ "$(echo -n "$DOWNLOADURL" | grep 'https')" == "" ];then #150305
        echo -e "#!/bin/sh\nwget -O - $DOWNLOADURL | tar --no-same-owner -xz" > /tmp/delayedrun_flashdl
       else
        echo -e "#!/bin/sh\nwget --no-check-certificate -O - $DOWNLOADURL | tar --no-same-owner -xz" > /tmp/delayedrun_flashdl
       fi
       chmod 777 /tmp/delayedrun_flashdl
			if [ "$SILENT" = "true" ]; then #20151023 for silent install at startup
			/tmp/delayedrun_flashdl
			else
			$TERMCONF /tmp/delayedrun_flashdl
			mysplash -bg yellow "$WAIT_PLS"
			fi
       ldd /usr/lib${lsuffix}/mozilla/plugins/libflashplayer.so > /dev/null 2>&1
       [ $? -ne 0 ] && rm -f /usr/lib${lsuffix}/mozilla/plugins/libflashplayer.so 2>/dev/null
       if [ -f /usr/lib${lsuffix}/mozilla/plugins/libflashplayer.so ];then
        FLASHSIZE=$(du /usr/lib${lsuffix}/mozilla/plugins/libflashplayer.so| cut -f1)
        echo /usr/lib${lsuffix}/mozilla/plugins/libflashplayer.so > /tmp/flashplayer.files #register
        if [ -d /usr/lib${lsuffix}/mozilla/plugins/usr ];then #150305
         rm -rf /usr/lib${lsuffix}/mozilla/plugins/usr/lib 2>/dev/null
         rm -rf /usr/lib${lsuffix}/mozilla/plugins/usr/lib64 2>/dev/null
         rm -rf /usr/lib${lsuffix}/mozilla/plugins/usr/share/kde4 2>/dev/null
         cp -r -f /usr/lib${lsuffix}/mozilla/plugins/usr /
         #find /usr/lib${lsuffix}/mozilla/plugins/usr -type d -printf '/usr/%P/\n'  >> /tmp/flashplayer.files
         #find /usr/lib${lsuffix}/mozilla/plugins/usr -type l -printf '/usr/%P\n'  >> /tmp/flashplayer.files
         find /usr/lib${lsuffix}/mozilla/plugins/usr -type f -printf '/usr/%P\n'  >> /tmp/flashplayer.files
         cd $HOME
         rm -rf /usr/lib${lsuffix}/mozilla/plugins/usr
         grep -vw '^flashplayer-$NEWVERSION-$ARCHSEE' $HOME/.packages/user-installed-packages > /tmp/user-installed-packages
         NEWENTRY="flashplayer-$NEWVERSION-$ARCHSEE|flashplayer|$NEWVERSION-$ARCHSEE||Internet|$LENGTH K||flashplayer-$NEWVERSION-$ARCHSEE.pet||Preferences for Adobe Flash Player||||"
         echo "$NEWENTRY" >> /tmp/user-installed-packages
         PACKAGE_ROOT=flashplayer-$NEWVERSION-$ARCHSEE	#v1.0
         mv -f /tmp/flashplayer.files /tmp/$PACKAGE_ROOT.files
         cp -f /tmp/$PACKAGE_ROOT.files /tmp/user-installed-packages $HOME/.packages/
         if [ -f /usr/share/applications/flash-player-properties.desktop ];then #fix .desktop for menu
          OLDNAME="`grep "^Name=" /usr/share/applications/flash-player-properties.desktop`"
          OLDCATEGORY="`grep "^Categories" /usr/share/applications/flash-player-properties.desktop`"
          OLDICON="`grep "^Icon" /usr/share/applications/flash-player-properties.desktop`"
          OLDEXEC="`grep "^Exec" /usr/share/applications/flash-player-properties.desktop`"
          sed -i -e "s%$OLDNAME%$NEWNAME%" \
			  -e "s%$OLDEXEC%$NEWEXEC%" \
			  -e "s%$OLDCATEGORY%$NEWCATEGORY%" \
			  -e "s%$OLDICON%$NEWICON%" /usr/share/applications/flash-player-properties.desktop
         fi
         fixmenus
         if [ "`pidof jwm`" != "" ] ; then
           jwm -reload || jwm -restart	#v1.0
         fi
         SUCCESS="y"
        fi
       fi
      else
         DOWNLOADURL="$_V11_86_url" #150305
         case "$CHOICE" in
         V11_86) DOWNLOADURL="$_V11_86_url"
			  PACKAGE_ROOT="$_V11_86_name";;
         V11_64) DOWNLOADURL="$_V11_64_url"
			  PACKAGE_ROOT="$_V11_64_name";;
         V10_86) DOWNLOADURL="$_V10_86_url"
              PACKAGE_ROOT="$_V10_86_name";;
         *) exit 0;;
         esac
         DOWNLOADURL="$DOWNLOADURL/$PACKAGE_ROOT.pet"
         cd /tmp
         DOWNLOAD_FILE=$(which download_file)
         if [ "$DOWNLOAD_FILE" ]; then
           $DOWNLOAD_FILE "$DOWNLOADURL"
         else
          if [ "$(echo -n "$DOWNLOADURL" | grep 'https')" == "" ];then #150305
           echo -e "#!/bin/sh\nwget -O - $DOWNLOADURL" > /tmp/delayedrun_flashdl
          else
           echo -e "#!/bin/sh\nwget --no-check-certificate -O - $DOWNLOADURL" > /tmp/delayedrun_flashdl
          fi
           chmod 777 /tmp/delayedrun_flashdl
           $TERMCONF /tmp/delayedrun_flashdl
         fi
         #cd $HOME #V1.2 fix download pets
         if [ -s "$PACKAGE_ROOT.pet" ]; then
           petget +$PACKAGE_ROOT.pet
           [ -s $HOME/.packages/$PACKAGE_ROOT.files ] && SUCCESS="y"
           rm -f $PACKAGE_ROOT.pet
         fi
      fi
      if [ "$SUCCESS" ]; then
        #http://flashblock.mozdev.org/
        #LANG=C Xdialog --center --title "Adobe Flash Player" --icon "/usr/share/doc/flashlogo.gif" --msgbox "The Adobe Flash Player has been downloaded to:\\n /usr/lib/mozilla/plugins/libflashplayer.so\\nYou will need to exit from your web browser and restart it for the player to load" 0 0
        NOFB="`grep -i '^flashblock' $HOME/.packages/*-installed-packages`"
        if [ "$OFFERFB" -a "$NOFB" = "" ];then
         FLASHBLOCK='<hbox>
            <pixmap><input file>/usr/share/icons/flashblock.png</input></pixmap>
         <checkbox>
           <label>'$(gettext "Open your browser at the mozilla Flashblock page.")'</label>
           <variable>OPENFB</variable>
           <default>true</default>
          </checkbox></hbox>'
          else FLASHBLOCK='';
        fi
        check_flash_version_installed #to update flash version installed
        TEXT="$(eval_gettext '$AppTitle01 information: the Adobe Flash player plugin has been updated (v$FLASHVERSION). You need to restart your web browser to load this new version.')" #150311
        export FDLG='<window title="'$T_title'">
        <vbox>
         <frame><hbox>
          <pixmap><width>60</width><input file>/usr/share/doc/flashlogo.gif</input></pixmap>
          <text usemarkup="true"><input>echo -en "'$TEXT'"</input></text>
         </hbox></frame>
         '"$FLASHBLOCK"'
         <hbox>
          <button ok></button>
         </hbox>
        </vbox>
       </window>'
       mysplash --stop
       #I=$IFS; IFS="" 
		STATEMENTS=$($GTKDIALOG -p FDLG -c) || echo "$FLDG" >&2 
		STATEMENTS=$(echo "$STATEMENTS"|grep '^[A-Z][A-Z]*=')
		eval "$STATEMENTS"
		#IFS=$I
		[ "$EXIT" = "OK" -a "$OPENFB" = "true" ] && \
		exec $MYBROWSER $FLASHBLOCK_URL
		exit 0
      else
        #LANG=C Xdialog --center --title "Adobe Flash Player" --icon "/usr/share/doc/flashlogo.gif" --msgbox "Sorry, the Player did not download. Try the Puppy Package Manager." 0 0
        TEXT=$(eval_gettext "Sorry, the Adobe Flash player plugin has not been successfully downloaded/installed (please report this bug in the $AppTitle01 forum).")
        export FDLG='<window title="'$T_title'">
        <vbox>
        <hbox>
        <pixmap><width>60</width><input file>/usr/share/doc/flashlogo.gif</input></pixmap>
        <text><input>echo -en "'$TEXT'"</input></text>
        </hbox>
        <hbox><button ok></button></hbox>
        </vbox></window>'
         mysplash --stop
         $GTKDIALOG -p FDLG -c
      fi
}


function HelpNoGui(){
echo "-----------------------------------
Help for $T_title
Boot options:
-h    to see this help
-a    to check plugin update at startup of the session
-----------------------------------"
}


function AutoUpdateGui(){
if [ "$FLASHVERSION" != "$NEWVERSION" ]; then
	if [ "$AUTOUPDATESILENTINSTALL" = "false" ]; then
	export AutoGUI='<window title="'$T_title'" icon-name="'$MAIN_ICON'">
      <vbox>
      <hbox>
      <pixmap><width>"'$MAIN_ICON_WIDTH1'"</width><input file>"'$FLASH_ICON'"</input></pixmap>
      <text width-chars="'$TEXT_WIDTH_CHARS1'" width-request="'$TEXT_WIDTH1'"><label>'"$TEXT"'</label></text>
      </hbox>
		<hbox>
		<button tooltip-text="'$SEE_INSTALL_LASTVERSION_TOOLTIP'">
		<label>"'$SEE_INSTALL_LASTVERSION_LABEL'"</label>
		<input file stock="gtk-apply"></input>
		<action>UpdateFlashNow &</action>
		<action>exit:ok</action>
		</button>
		<button tooltip-text="'$SEE_AUTOUPDATE_MAINGUI_TOOLTIP'">
		<input file stock="gtk-jump-to"></input>
		<action>$ANL01 &</action>
		<action>exit:ok</action>
		</button>
		<button tooltip-text="'$SEE_HELP_TOOLTIP'">
		<input file stock="gtk-help"></input>
		<action>help_gui &</action>
		</button>
		<button tooltip-text="'$SEE_QUITAPP_TOOLTIP'">
		<input file stock="gtk-quit"></input>
		<action type="exit">quit_now</action>
		</button>
		</hbox>
      </vbox></window>'
		#mysplash --stop
		REPLY=$($GTKDIALOG -p AutoGUI -c |grep '=') || echo "$AutoGUI" >&2
		eval "$REPLY"
	else #silent install
		SILENT="$AUTOUPDATESILENTINSTALL"
		UpdateFlashNow &
		exit 0
	fi
else
	echo "GetFlash - $TEXT" 
	exit 0
fi
}




## Startup options
case "$1" in
-a|-autoupdate|--autoupdate) CHOICE="V11X"; AutoUpdateGui; exit 0;;
-h|-help|--help) HelpNoGui; exit 0;;
esac





#### Main GUI
AUTOUPDATEOLDPREF='AUTOUPDATECHECK="[a-z]*[a-z]"'
AUTOUPDATEPREFTRUE='AUTOUPDATECHECK="true"'
AUTOUPDATEPREFFALSE='AUTOUPDATECHECK="false"'
AUTOUPDATESILENTOLDPREF='AUTOUPDATESILENTINSTALL="[a-z]*[a-z]"'
AUTOUPDATESILENTPREFTRUE='AUTOUPDATESILENTINSTALL="true"'
AUTOUPDATESILENTPREFFALSE='AUTOUPDATESILENTINSTALL="false"'
AUTOUPDATESTARTUPDELAYOLDPREF='AUTOUPDATESTARTUPDELAY="[0-9]*[0-9]"'
#AUTOUPDATESTARTUPDELAYNEWPREF='AUTOUPDATESTARTUPDELAY="'$AUTOUPDATESTARTUPDELAY'"'
AUTOUPDATESTARTUPDELAYNEWPREF_a='AUTOUPDATESTARTUPDELAY="'
AUTOUPDATESTARTUPDELAYNEWPREF_b='"'


      case "$ARCH" in # Define arch to select available packages
        i?86)RADIOBTNS='<radiobutton><label>'$_V11_86_name' ('$_V11_86_size' '$(gettext "MB")')</label><variable>V11_86</variable></radiobutton>
        <radiobutton><label>'$_V10_86_name' ('$_V10_86_size' '$(gettext "MB")')</label><variable>V10_86</variable></radiobutton>' ;;
        *64)RADIOBTNS='<radiobutton><label>'$_V11_64_name' ('$_V11_64_size' '$(gettext "MB")')</label><variable>V11_64</variable></radiobutton>' ;;
      esac
      _V11X="flashplayer-$NEWVERSION-$ARCHSEE ($NEWMB) - $SEE_INSTALL_LASTVERSION_TEXT"
      #LANG=C Xdialog --center --title "Adobe Flash Player" --icon "/usr/share/doc/flashlogo.gif" --yesno "$TEXT" 0 0
      export FDLG='<window title="'$T_title'" icon-name="'$MAIN_ICON'">
      <vbox>
      <hbox>
      <pixmap><width>"'$MAIN_ICON_WIDTH1'"</width><input file>"'$FLASH_ICON'"</input></pixmap>
      <text width-chars="'$TEXT_WIDTH_CHARS1'" width-request="'$TEXT_WIDTH1'"><label>'"$TEXT"'</label></text>
      </hbox>
      <frame '$_WHICH'>
        <radiobutton><label>'$_V11X'</label><variable>V11X</variable></radiobutton>
        '$RADIOBTNS'
		<hbox>
		<button tooltip-text="'$SEE_INSTALL_TOOLTIP'">
		<label>'"$SEE_INSTALL_LABEL"'</label>
		<input file stock="gtk-apply"></input>
		<action type="exit">OK</action>
		</button>
		</hbox>
      </frame>
      
        '$FRAMEAUTOUPDATE'
        
        '$SEE_FORUM'
        
		<hbox>
		<button tooltip-text="'$SEE_HELP_TOOLTIP'">
		<input file stock="gtk-help"></input>
		<action>help_gui &</action>
		</button>
		<button tooltip-text="'$SEE_LICENSE_TOOLTIP'">
		<input file stock="gtk-info"></input>
		<action>$MYBROWSER $ADOBE_GENERAL_TERMS &</action>
		</button>
		<button tooltip-text="'$SEE_REFRESH_TOOLTIP'">
		<input file stock="gtk-refresh"></input>
		<action>$ANL01 &</action>
		<action>exit:ok</action>
		</button>
		<button tooltip-text="'$SEE_UPDATECONF_TOOLTIP'">
		<input file stock="gtk-network"></input>
		<action>update_conf_file</action>
		<action>$ANL01 &</action>
		<action>exit:ok</action>
		</button>
		<button tooltip-text="'$SEE_EDITCONF_TOOLTIP'">
		<input file stock="gtk-edit"></input>
		<action>defaulttexteditor $APP_CONF</action>
		</button>
		<button tooltip-text="'$SEE_QUITAPP_TOOLTIP'">
		<input file stock="gtk-quit"></input>
		<action type="exit">quit_now</action>
		</button>
		'$SCALEGRIP'
		</hbox>
      </vbox></window>'
      #echo "$FDLG" >&2
      #mysplash --stop
      REPLY=$($GTKDIALOG -p FDLG -c |grep '=') || echo "$FDLG" >&2
      #echo "$REPLY"
      eval "$REPLY"
      [ "$EXIT" = "OK" ] || exit 0
      #CHOICE=$(echo "$REPLY" | grep '=.*true'| head -n1| cut -f1 -d'=')
      CHOICE=$(echo "$REPLY" | grep '^V'| grep '=.*true' | head -n5| cut -f1 -d'=') #20151023 to select only variable pkg=true
      UpdateFlashNow

exit
