#!/bin/bash

# Copyright 2016-2020  Jay Flood, SP, Brasil
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Author: brokenman@porteus.org
#
# This is a starter script to download a live script and update flash.

# Source porteus functions
. /usr/share/porteus/porteus-functions
get_colors

## Set variables
FURL="https://get.adobe.com/flashplayer/"
WOPT='--no-check-certificate --user-agent="Mozilla Linux" --output-document - --quiet'
TMPDIR=/tmp/.flash_updater.$$

ARCH=`uname -m`
case "$ARCH" in
  i?86) ARCH="i386" ; LIBDIRSUFFIX=""; MARCH=i586; ANTIARCH=x86_64 ;;
  x86_64) ARCH="x86_64" ; LIBDIRSUFFIX="64" ; MARCH=x86_64; ANTIARCH=i386 ;;
  *) echo "Package for $(uname -m) architecture is not available." ; exit 1 ;;
esac

## Variables
cleanup(){
rm -rf $TMPDIR 2>/dev/null
rm /tmp/flashplayer-plugin*.txt /tmp/flashplayer-plugin*.md5 2>/dev/null
rm /tmp/makepkg-flashplayer* 2>/dev/null
exit	
}
trap cleanup SIGHUP SIGINT SIGTERM

########### START CHECKS
# Check for root
if [ `whoami` != "root" ]; then
	sayerror "Only root can do that"
	cleanup
fi

## Check for flash
rf=`find /usr -type f -name "libflashplayer.so"`
if [ "$rf" ]; then
	FPATH=`realpath $rf`
	LFVER=`awk '/LNX / {gsub(/\,/,".",$0);print substr($2,1,12)}' $FPATH`
		else
	FPATH="Flash is not installed"
fi

# Mae sure we have an internet connection
is_online || { sayerror "An internet connection is required."; exit; }

# Make temp directory
[ ! -d $TMPDIR ] && mkdir -p $TMPDIR || rm -rf $TMPDIR/*

# Get current flashplayer version
echo
echo "Searching online for latest flash player version ..."
RFVER=`wget --no-check-certificate --user-agent="Mozilla Linux" --output-document - --quiet https://get.adobe.com/flashplayer/ | grep -Po '(?<=Version\s)[^\<]*'`
if [ -z "$RFVER" ]; then
	sayerror "Could not connect to the adobe website"
	exit
fi

echo
echo "Installed version:	${txtbld}${txtgreen}${LFVER}${rst}"
echo "Latest version:		${txtbld}${txtgreen}${RFVER}${rst}"

echo
read -p " Would you like to continue? [y/n]" -n 1 -r -s && echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then cleanup; fi

read -p " Would you like to create a Porteus module? [y/n]" -n 1 -r -s && echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
	MAKEMOD=0
fi

# Kill any running versions
fpp=`ps ax | grep libflashplayer.so | grep -v grep | awk '{print$1}'`
if [ $fpp ]; then
	echo
	bold "Please close all browsers to ensure flashplayer is not in use."
	cleanup
fi

# Remove any installed flash
if [[ `ls /var/log/packages/flashplayer-plugin* 2>/dev/null` ]]; then
	removepkg flashplayer-plugin
fi

# Deactivate any running module
if [ -e /mnt/live/memory/images/flashplayer-plugin* ]; then
	deactivate /mnt/live/memory/images/flashplayer-plugin*
fi

# Download the file
download https://fpdownload.macromedia.com/get/flashplayer/current/licensing/linux/install_flash_player_11_linux.$ARCH.tar.gz $TMPDIR
mkdir $TMPDIR/build
cd $TMPDIR/build
tar xvf ../install_flash_player_11_linux.$ARCH.tar.gz
rm -rf LGPL readme.txt
[ ! -d usr/lib/mozilla/plugins ] && mkdir -p usr/lib/mozilla/plugins
mv libflashplayer.so usr/lib/mozilla/plugins/
find usr/lib -name "*.so" -exec chmod 755 {} \;

# Create 64 bit link
if [ "$ARCH" = x86_64 ]; then
	ln -sf /usr/lib/mozilla usr/lib64/mozilla
fi

# Create slackware package build files
mkdir install

cat > install/slack-desc << EOF
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description.  Line
# up the first '|' above the ':' following the base package name, and the '|'
# on the right side marks the last column you can put a character in.  You must
# make exactly 11 lines for the formatting to be correct.  It's also
# customary to leave one space after the ':'.

                  |-----handy-ruler-----------------------------------------|
flashplayer-plugin: flashplayer-plugin (flash plugin for web browsers)
flashplayer-plugin:
flashplayer-plugin: Provides Adobe Flash plugin for browsers that recognize
flashplayer-plugin: /usr/lib/mozilla/plugins as a valid plugin directory
flashplayer-plugin:
flashplayer-plugin: Plugin is subject to Adobe terms of use: 
flashplayer-plugin:  http://www.adobe.com/go/labs_term_of_use
flashplayer-plugin:
flashplayer-plugin: Plugin is subject to Adobe Flash EULA:
flashplayer-plugin:  http://labs.adobe.com/technologies/eula/flashplayer.html
flashplayer-plugin:
EOF

cat > install/doinst.sh << EOM
# Update the desktop database:
if [ -x usr/bin/update-desktop-database ]; then
  chroot . /usr/bin/update-desktop-database usr/share/applications > /dev/null 2>&1
fi

# Update hicolor theme cache:
if [ -d usr/share/icons/hicolor ]; then
  if [ -x /usr/bin/gtk-update-icon-cache ]; then
    chroot . /usr/bin/gtk-update-icon-cache -f -t usr/share/icons/hicolor 1> /dev/null 2> /dev/null
  fi
fi
EOM

makepkg -l y -c n /tmp/flashplayer-plugin-$RFVER-$MARCH-1.txz

if [ $MAKEMOD ]; then
	txz2xzm /tmp/flashplayer-plugin-$RFVER-$MARCH-1.txz
fi