#!/bin/bash

# By Geoffrey, do with as you please.......
# 14072016
# Update Flash v1.5
# Used sed in place of perl as suggested by sfs
# yad no longer a dependency, uses gtkdialog 
# fixed url not found "get.adobe.com/flashplayer/" from some locale's, get update version info from alternate url "adobe.com/software/flash/about" rg66
# fix linux detection by sfs
# Adapted from "update_flash" by Geoffrey for ToOpPy LINUX
# 170516 (v1.6c > fix "grep -A2 Linux > grep -A3 Linux" - from Geoffrey's update_flash v1.8 http://murga-linux.com/puppy/viewtopic.php?t=100523)
# 191103 delay before running blinking systray icon - local configuration file update 

. /root/.getflash/getflash.conf
echo $LANG > /tmp/getflashmsg
LANG=C

## delay before running systray icon (configured in main script getflash - allow time for the wifi connection to be established) 
DELAY="`cat /root/.getflash/delay`"
echo "
`date`
Adobe Flash Player Update Checker: waiting $DELAY seconds before starting the script
________________________________"
sleep $DELAY #configured in main script getflash

## systray icon display duration (configured in main script getflash)
DURATION="`cat /root/.getflash/duration`"

if [ ! -f $HOME/.getflash/index.html ]; then 
IFCONFIG="`ifconfig | grep '^[pwe]' | grep -v 'wmaster'`"
while [ "$IFCONFIG" != "" ]; do
   sleep 1
    ping -c 1 8.8.8.8
    if [ $? -eq 0 ];then
      break
   else
      ping -c 1 www.google.com
      if [ $? -eq 0 ];then
         break
      fi
   fi
done &> /dev/null
fi

## updating local configuration file
echo "`date`
Adobe Flash Player Update Checker: Updating local configuration file
________________________________"

cd /tmp
[ -f /tmp/getflash.conf ] && rm -f /tmp/getflash.conf #wget > getflash.conf.1, getflash.conf.2 if getflash.conf already existing...)
wget -L "$CONFFILE_URL"
sleep 1
if [ ! -f /tmp/getflash.conf ]; then
	echo "
`date`
Adobe Flash Player Update Checker: No configuration file found on the server
________________________________"
else
	mv -f "/tmp/getflash.conf" "/root/.getflash/getflash.conf"
	echo "
`date`
Adobe Flash Player Update Checker: Configuration file downloaded then copied to /root/.getflash/getflash.conf
________________________________"
fi

if [ ! -f $HOME/.getflash/index.html ]; then
wget --no-check-certificate -O- --spider $FLASH_URL > $HOME/.getflash/flash_url 2>&1 
	if grep -q '200 OK' $HOME/.getflash/flash_url 2> /dev/null; then
		wget --no-check-certificate --user-agent="Mozilla/5.0 Linux" -q -N -P $HOME/.getflash $FLASH_URL
	else
		wget -q -N -P $HOME/.getflash $VERSION_URL
	fi
fi

if grep -q 'Adobe Flash Player Download' $HOME/.getflash/index.html 2> /dev/null; then
	export LATEST_VERSION=`sed -n '/<strong>Version /s/^.*<strong>Version \(.*\)<\/strong>.*$/\1/p' $HOME/.getflash/index.html`
else
	export LATEST_VERSION=`cat $HOME/.getflash/index.html | grep -A3 Linux | grep [0-9] | awk -F '[><]' '{print $3}' | tr -d "\n"` #rg66 #170516
fi
export INSTALLED_VERSION=`busybox strings -n13 /usr/lib${lsuffix}/mozilla/plugins/libflashplayer.so  | grep 'FlashPlayer_' | cut -d '_' -f2-5| tr '_' '.'`

if [ "$LATEST_VERSION" = "$INSTALLED_VERSION" ]; then
	echo "
`date`
Adobe Flash Player Update Checker: No update available.
(latest version: $LATEST_VERSION - installed $INSTALLED_VERSION)
________________________________"
	exit
else
LANG="`cat /tmp/getflashmsg`"
export TEXTDOMAIN=getflash
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
. gettext.sh

	TEXT="$(eval_gettext ' <b>Adobe Flash player update available!</b>
------------------------------------
 <i>Current: <b>$INSTALLED_VERSION</b>
 New: <b>$LATEST_VERSION</b></i>
 Please, click this icon to run GetFlashPlayer.')"
fi

while true; do echo 'icon:/usr/local/lib/X11/mini-icons/FLASH_1.png'; sleep 0.5; echo 'icon:/usr/local/lib/X11/mini-icons/FLASH_2.png'; sleep 0.5; done | yad --notification --icon-size=24 --auto-kill --no-middle --listen --text="$TEXT" --command="/usr/local/getflash/getflash" &
XPID1=$!
sleep $DURATION #configured in main script getflash
kill $XPID1
