#!/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)
#161016 v1.6 - Geoffrey: update download URL logic & support 64-bit; rerwin: move & rename config file
#to /etc/getflash.conf; add pet file name to alternative download wgets
# v1.6b Adapted for ToOpPy LINUX on 161027 - Join part of Geoffrey "update_flash" (systray icon
#[/usr/local/getflash/getflash_systray] instead of a separated GUI)
 #with rerwin "gestflash" (added configuration button "Enable/Disable...").
 #Move all retated config files to /root/.getflash - pets transfered to ToOpPy server
 #http://www.russoodlespuppylinuxstuff.net/argolance/tooppy_5x/applications/internet.
 #Revamping the script.
# v1.6c Keep only new version installation radiobutton.
# 170516 (v1.6d > fix "grep -A2 Linux > grep -A3 Linux" - from Geoffrey's update_flash
#v1.8 http://murga-linux.com/puppy/viewtopic.php?t=100523)
# 170527 New GUI - Modification of the configuration (remote) file "getflash.conf"
# 181201 Yaf-splash
# 191025 automatic update of the remote configuration file at startup
# 191103 modification of the script so that the update of the configuration file
#is done before the update of the /root/.getflash/index.html file
#(from which the number of the current version of the flash player is extracted).
# delay before running blinking system tray icon
# mysplash and yaf splash replaced with gtk-splash and deletion of all related lines
# integration of gtk-splash in the package

AppVersion=1.7.4

## Language settings
export TEXTDOMAIN=getflash
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
LANG_USER=$LANG

#181201
gtk-splash -placement top -icon /usr/share/pixmaps/FLASH.gif -bg white -close never -text "<b>$(gettext 'Downloading</b> the latest configuration file
<b>Checking</b> if an Adobe Flash update is available.')" &
XPID=$!

## If no basichtmlviewer/defaultbarehtmlviewer
if [ -f /usr/local/bin/basichtmlviewer ];then
	MYBROWSER="basichtmlviewer"
else
	MYBROWSER="defaultbarehtmlviewer"
fi
if [ -f /usr/local/bin/defaultbarehtmlviewer ];then
	MYBROWSER="defaultbarehtmlviewer"
else
	MYBROWSER="defaulthtmlviewer"
fi
export MYBROWSER

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

## systray icon display duration
DURATION="`cat /root/.getflash/duration`"

## delay before running systray icon (allow time for the wifi connection to be established) 
DELAY="`cat /root/.getflash/delay`"

## Check data and variables/settings for script 
. /etc/DISTRO_SPECS
. /root/.getflash/getflash.conf
. gettext.sh

## (u)rxvt?
RXVT=urxvt
which $RXVT &>/dev/null || RXVT=rxvt
TERMCONF="$RXVT -w 2 -b 10 +sb -geometry 80x4 -bl -e"

## Check installed version of flash player
if [ ! -f /usr/lib${lsuffix}/mozilla/plugins/libflashplayer.so ];then
  FLASHVERSION="0"
  TEXT=$(gettext "Flash player is not installed.")
  TEXT2=$(gettext "NOTE: Flash Player 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 "<b><u>Installed version:</u> %s</b>.")
  TEXT=$(printf "$TEXT" "$FLASHVERSION")
  TEXT2=$(gettext "NOTE: Flash Player v%s is currently installed.")
  TEXT2=$(printf "$TEXT2" "$FLASHVERSION")
fi

## Define messages for script
T_title="GetFlashPlayer - v$AppVersion"
_UNABLE_INTERNET="$(gettext 'No Internet connection founded, please check your configuration, then restart') GetFlashPlayer.

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

$TEXT2"

## Test processor arch
ARCH=$(uname -m)

#161016...
# From Geoffrey's Flash Auto Updater code
# http://www.murga-linux.com/puppy/viewtopic.php?p=928124#928124

case "$ARCH" in
 i?86) INSTRUCTION_SET="i386" ;;
 *64) INSTRUCTION_SET="x86_64" ;;
 *)exit 1 ;;
esac

## Check Internet connection, define messages, then start main GUI
internet_status() {
  LANG=C route | grep -q 'default[ ].*[ ]0\.0\.0\.0[ ]' && grep -wq 'nameserver' /etc/resolv.conf #&& ping -c1 google.com &>/dev/null
}
if ! internet_status; then
	Xdialog --icon /usr/local/lib/X11/pixmaps/error.png --title "$T_title" --left --wrap --msgbox "$_UNABLE_INTERNET" 0 80
	exit 1
else
	cd /tmp
	[ -f /tmp/getflash.conf ] && rm -f /tmp/getflash.conf #wget > getflash.conf.1, getflash.conf.2 if getflash.conf already existing...)
	$TERMCONF wget -L "$CONFFILE_URL"
	sleep 1
	if [ ! -f /tmp/getflash.conf ]; then
		Xdialog --icon /usr/local/lib/X11/pixmaps/error.png --title "$T_title" --left --wrap --msgbox "$(gettext 'No configuration file was found on the server.')" 0 100
	else
		mv -f "/tmp/getflash.conf" "/root/.getflash/getflash.conf"
	fi
fi

wget -q -N -P $HOME/.getflash $VERSION_URL
FLASH_VERSION=`cat $HOME/.getflash/index.html | grep -A3 Linux | grep [0-9] | awk -F '[><]' '{print $3}' | tr -d "\n"` #170516
eval  "DOWNLOADURL=$DOWNLOAD_URL"
NEWVERSION=$FLASH_VERSION

lsuffix= #161016 end

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

## kill running gtk-splash window
kill $XPID

#### Functions part
about() {
export ABOUT="
<window decorated=\"false\" skip_taskbar_hint=\"true\" window_position=\"2\">
<vbox border-width=\"5\">	
	<frame  $(gettext 'About') >
		<pixmap><input file>/usr/share/pixmaps/getflash.png</input></pixmap>
		<text use-markup=\"true\"><label>\"<b><span size='large'>GetFlashPlayer - v$AppVersion</span></b>\"</label></text>
		<text use-markup=\"true\"><label>\"<b><span size='large'>$(gettext 'Interface for Flash Player installation/update')</span></b>\"</label></text>
		<text use-markup=\"true\"><label>\"<i>$(gettext 'Original code by technosaurus and by Barry K.')</i>\"</label></text>
<hseparator height-request=\"10\"></hseparator>
		<pixmap><input file>/usr/share/pixmaps/FLASH.gif</input></pixmap>
		<text use-markup=\"true\"><label>\"<b><span size='large'>$(gettext 'Blinking systray notification icon')</span></b>\"</label></text>
		<text use-markup=\"true\"><label>\"<i>$(gettext "Adapted from 'Adobe Flash Player Auto Updater' by Geoffrey - Argolance (October 2016, November 2019)")</i>\"</label></text>
	</frame>
	<hbox>	
		<button can-focus=\"no\" relief=\"2\"><input file stock=\"gtk-close\"></input></button>
	</hbox>
</vbox>
</window>
"

$GTKDIALOG --class St_Ab --program=ABOUT
}

help_gui() {
export HELP_GUI="
<window title=\"$(gettext 'GetFlashPlayer Help')\" icon-name=\"gtk-help\">
<vbox border-width=\"5\">
	<hbox space-fill=\"false\" space-expand=\"true\">
		<pixmap><input file>/usr/share/doc/flashlogo.gif</input></pixmap>
		<text use-markup=\"true\"><label>\"$(gettext '<b>Adobe Flash Player</b> is a lightweight browser plug-in and Internet application runtime for audio/video playback and gameplay.
It is strongly recommanded to keep it up to date.')\"</label></text>
	</hbox>
<hseparator height-request=\"10\"></hseparator>
	<text use-markup=\"true\"><label>\"$(gettext '<b>GetFlashPlayer</b> is a utility that allows you to install <i>(or reinstall)</i> the Flash Player plug-in easily. Flash player plug-in is used by most of the Internet browsers installed in your system.
')\"</label></text>
	<text use-markup=\"true\"><label>\"<i><b>$(gettext '(Licence:</b> by clicking the Install button, you acknowledge that you have read and agree to the Adobe Software Licensing Agreement).')</i>\"</label></text>
	<hbox>
		<button>
			<input file>/usr/share/pixmaps/MySh_U.png</input>
			<label>\"$(gettext 'Adobe Flash Player general terms of use') \"</label>
			<action>$MYBROWSER $ADOBE_GENERAL_TERMS &</action>
		</button>
		<button no-focus=\"true\" relief=\"2\">
			<input file stock=\"gtk-close\"></input>
		</button>
	</hbox>
</vbox>
</window>
"

$GTKDIALOG -p HELP_GUI -c
}

if [ ! "$DOWNLOADURL" ];then 
	Xdialog --icon /usr/local/lib/X11/pixmaps/error.png --title "$T_title" --left --wrap --msgbox "$_UNABLE_SERVER" 0 80
	exit 1
fi
echo "getflash: $DOWNLOADURL" >&2
echo "NEWVERSION=$NEWVERSION"
#v1.1 download size
LENGTH=$(curl -s -I "$DOWNLOADURL" | grep -i 'Content-Length:' | cut -s -f2 -d':'| tr -dc '0-9') || LENGTH=0
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='---' #150311
	NEWMB="$(gettext "unknown size")"
fi
if [ "$FLASHVERSION" = "$NEWVERSION" ]; then
	TEXT="$TEXT
$(gettext "<i>(This is the latest available,
so you <b>do not</b> need to update it)</i>.")"
else
	TEXT="$TEXT
$(gettext "<b>It is recommended</b> to install the latest available version:")
<b><i>($NEWVERSION)</i></b>."
fi

funcImageToggle() {
if [ "$(</root/.getflash/update_flash)" = "true" ]; then
	ln -sf /usr/share/pixmaps/on.png /root/.getflash/toggle.png
	ln -sf /usr/local/getflash/getflash_systray /root/Startup/getflash_systray
	echo false > /root/.getflash/update_flash
	gtk-splash -icon /usr/local/lib/X11/mini-icons/FLASH_2.png -placement top -bg green -timeout 2 -text "$(gettext 'Tray icon ENABLED!')" &
else
	ln -sf /usr/share/pixmaps/off.png /root/.getflash/toggle.png
	rm -f /root/Startup/getflash_systray
	echo true > /root/.getflash/update_flash
	gtk-splash -icon /usr/local/lib/X11/mini-icons/FLASH_1.png -placement top -timeout 2 -text "$(gettext 'Tray icon DISABLED!')" &
fi
}
export -f funcImageToggle

#### GUI part
## Define messages, icons and size of GUI
MAIN_ICON="getflash"
TEXT_WIDTH_CHARS1="10"
TEXT_WIDTH1="500"

## Define arch to select available packages
_LV="<b>$NEWVERSION-$INSTRUCTION_SET</b> <i>($NEWMB)</i>"
export FDLG='<window title="'$T_title'" icon-name="'$MAIN_ICON'">
<vbox border-width="5">
	<hbox>
		<button can-focus="no" relief="2" tooltip-text=" '$(gettext "About")' ">
			<input file stock="gtk-about"></input>
			<action>about</action>
		</button>
		<button can-focus="no" relief="2" tooltip-text=" '$(gettext "GetFlashPlayer Help")' ">
			<input file stock="gtk-help"></input>
			<action>help_gui</action>
		</button>
	</hbox>
	<text justify="2" wrap="false" use-markup="true"><label>"<b><u>'$(gettext "Latest Adobe Flash Player available version:")'</u></b>
'$_LV'"</label></text>
	<text justify="2" use-markup="true"><label>"'$TEXT'"</label></text>
	<hbox space-fill="true" space-expand="true">
		<text><label>"'$(gettext "Download and re/install Adobe Flash Player:")'"</label></text>
		<button>
			<input file>/usr/share/pixmaps/flash-player-install.png</input>
			<action type="exit">OK</action>
		</button>
	</hbox>
<hseparator width-request="10"></hseparator>
	<hbox space-fill="true" space-expand="true">
		<text><label>"'$(gettext "Notification status:")'"</label></text>
		<button relief="2" tooltip-text=" '$(gettext "Check for new Flash Player plug-in version at session startup and display notification in the systray if update available.
 NOTE: Click on the systray icon will allow user to run GetFlashPlayer and possibly update current Flash Player.")'">
			<input file>"/root/.getflash/toggle.png"</input>
			<variable>STARTUP</variable>
			<action>bash -c funcImageToggle</action>
			<action function="refresh">STARTUP</action>
		</button>
		<text><label>"'$(gettext "Delay:")'"</label></text>
		<spinbutton tooltip-text=" '$(gettext "Delay before running the blinking notification icon")' " range-min="0" range-max="60" range-step="5" range-value="'$DELAY'" editable="false">
			<variable>DELAY</variable>
		</spinbutton>		
		<text><label>"'$(gettext "Duration:")'"</label></text>
		<spinbutton tooltip-text=" '$(gettext "Display duration of the blinking notification icon")' " range-min="0" range-max="60" range-step="5" range-value="'$DURATION'" editable="false">
			<variable>DURATION</variable>
		</spinbutton>		
	</hbox>
	<hbox>
		<button can-focus="no" relief="2" tooltip-text="'$(gettext "Quit GetFlashPlayer")'">
			<input file stock="gtk-cancel"></input>
			<action type="exit">quit_now</action>
		</button>
	</hbox>
</vbox>
</window>'
      REPLY=$($GTKDIALOG -p FDLG -c |grep '=') || echo "$FDLG" >&2
      eval "$REPLY"
      echo $DURATION > /root/.getflash/duration
      echo $DELAY > /root/.getflash/delay
     [ "$EXIT" = "OK" ] || exit 0
       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
       $TERMCONF /tmp/delayedrun_flashdl
        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 '^flashplayer11' $HOME/.packages/user-installed-packages > /tmp/user-installed-packages
         NEWENTRY="flashplayer11-$NEWVERSION-$INSTRUCTION_SET|flashplayer11|$NEWVERSION-$INSTRUCTION_SET||Internet|17580K||flashplayer11-$NEWVERSION-$INSTRUCTION_SET.pet||Adobe Flash Player 11 browser plugin and Preferences. Note, this may not work on older PCs, use instead an earlier 11.x or even 10.x||||"
         echo "$NEWENTRY" >> /tmp/user-installed-packages
         PACKAGE_ROOT=flashplayer11-$NEWVERSION-$INSTRUCTION_SET	#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`"
          #NEWCATEGORY="Categories=X-Internet;" #now info is in getflash.conf file #161016
          OLDICON="`grep "^Icon" /usr/share/applications/flash-player-properties.desktop`"
          #NEWICON="Icon=/usr/share/icons/flash-player-properties.png" #now info is in getflash.conf file #161016
          sed -i -e "s%$OLDNAME%$NEWNAME%" \
			  -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
      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
         TEXT="$(gettext 'The Adobe Flash Player has been installed in')\n/usr/lib${lsuffix}/mozilla/plugins\n$(gettext 'You need to restart your web browser to load the new Flash Player.')" #150311
        export FDLG='<window title="'$T_title'">
        <vbox>
         <hbox>
          <pixmap><width>60</width><input file>/usr/local/lib/X11/pixmaps/ok.png</input></pixmap>
          <text usemarkup="true"><input>echo -en "'$TEXT'"</input></text>
         </hbox>
         '"$FLASHBLOCK"'
         <hbox>
          <button ok></button>
         </hbox>
        </vbox>
       </window>'
        #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
        TEXT="$(gettext "Sorry, something went wrong and the player was not downloaded/installed. Operation aborted...")"
        export FDLG='<window title="'$T_title'">
        <vbox>
        <hbox>
        <pixmap><width>60</width><input file>/usr/local/lib/X11/pixmaps/error.png</input></pixmap>
        <text><input>echo -en "'$TEXT'"</input></text>
        </hbox>
        <hbox><button ok></button></hbox>
        </vbox></window>'
         $GTKDIALOG -p FDLG -c
      fi

exit
