#!/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 script to update pepperflash for opera

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

# Check for root
SCRIPT="$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")"
if [ `whoami` != "root" ]; then
	echo "This requires elevated privilege"
	exit
fi

red " \"Since Adobe no longer supports Flash Player after December 31, 2020"
red " and blocked Flash content from running in Flash Player beginning January 12, 2021,"
red " Adobe strongly recommends all users immediately uninstall Flash Player"
red " to help protect their systems.\"  " 
cyan " Porteus will no longer supply this module,"
cyan " and this utility will not be included in future versions of Porteus."
#~ # functions set work directory(default is /tmp) to current work directory
#~ set_tmp() {
#~ WRKDIR=/tmp 
#~ }

#~ set_pwd() {
#~ WRKDIR=`pwd` 
#~ }

#~ # echo usage
#~ show_help() {
	#~ echo " options:  -d : do operations in the present directory, instead of /tmp . "
	#~ echo "           -h        : show this usage. "
#~ }
#~ while getopts ":-d:-h:" o; do
#~ case "$1" in
 
    #~ -d)
       #~ set_pwd;;
    
    #~ -h)
      #~ show_help
      #~ exit 0;;
              
     #~ *)
      #~ show_help
      #~ exit 1;;
    
#~ esac
#~ done
#~ case "$1" in
      
    #~ "")
      #~ set_tmp;;
      
#~ esac

#~ # Set variables
#~ PKGNAM=pepperflashplugin-nonfree
#~ URLV=https://people.debian.org/~bartm/pepperflashplugin-nonfree
#~ RELEASE=${RELEASE:-stable}    # stable, beta, or unstable
#~ TMPDIR=$WRKDIR/.portpepper
#~ TARG=$TMPDIR/ch.$$
#~ SERVER=`awk -F= '/SERVER=/{print$NF}' /etc/porteus.conf`
#~ ARCH=${ARCH:-$(uname -m)}
#~ CWD=`pwd`

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

#~ die(){
#~ [ -d $TMPDIR ] && rm -rf $TMPDIR
#~ exit
#~ }

#~ # tell us where the work will be done
#~ echo "Work will be done in: $WRKDIR "
 
#~ ## Check if there's enough working space
#~ [ `df -l --output=avail $WRKDIR | tail -1` -lt 64000 ] && { red "There's not enough space to run this script"; exit 1; }

#~ # Checks
#~ echo "Commencing checks ..."
#~ if [ ! -d $TARG ]; then
	#~ mkdir -p $TARG && sayok "Created temp directory" || { sayerror "Failed to create temp dir"; die; }
#~ fi

#~ ## If tarball is in $WRKDIR then use it
#~ if [ -e $WRKDIR/flash_player_npapi_linux.$FARCH.tar.gz ]; then
	#~ LINK=$WRKDIR/flash_player_npapi_linux.$FARCH.tar.gz
	#~ bold "Using flashplayer tarball in $WRKDIR"
	#~ cp $LINK $TMPDIR || { red "Could not existing tarball in $WRKDIR"; exit; }
#~ else
	#~ if is_online_url $URLV; then
		#~ sayok "Version file is good"
		#~ #latver=`wget -q -O - https://people.debian.org/~bartm/pepperflashplugin-nonfree/latest-stable.txt | tail -n1`
		#~ latver=`wget --no-check-certificate --user-agent="Chromium Linux" --output-document - --quiet https://get.adobe.com/flashplayer/ | grep -Po '(?<=Version\s)[^\<]*'`
		#~ sayok "Latest: $latver"
			#~ else
		#~ sayerror "Failed to get latest version"
		#~ die
	#~ fi
	#~ #LINK=https://fpdownload.adobe.com/pub/flashplayer/pdc/$latver/flash_player_ppapi_linux.$FARCH.tar.gz
	#~ LINK=https://fpdownload.adobe.com/get/flashplayer/pdc/$latver/flash_player_ppapi_linux.$FARCH.tar.gz
	#~ echo
	#~ echo -e "Latest pepperflash version is: ${txtbold}${txtgreen}${latver}${rst}"
	#~ read -p " Would you like to continue? [y/n]" -n 1 -r -s && echo
	#~ if [[ $REPLY =~ ^[Yy]$ ]]; then
		#~ echo
	#~ else
		#~ die
	#~ fi

	#~ download $LINK $TMPDIR
#~ fi

#~ if [ ! -f $TMPDIR/flash_player_npapi_linux.$FARCH.tar.gz ] && [ ! -f $TMPDIR/flash_player_ppapi_linux.$FARCH.tar.gz ]; then
	#~ bold "There was an error downoading the file:"
	#~ echo "$LINK"
	#~ die
#~ fi

#~ [ -f $TMPDIR/flash_player_npapi_linux.$FARCH.tar.gz ] && tarball=flash_player_npapi_linux.$FARCH.tar.gz
#~ [ -f $TMPDIR/flash_player_ppapi_linux.$FARCH.tar.gz ] && tarball=flash_player_ppapi_linux.$FARCH.tar.gz

#~ # Create the module/package directory
#~ mkdir -p $TARG/usr/lib${LIBDIRSUFFIX}/PepperFlash
#~ cd $TMPDIR
#~ tar xvf $tarball
#~ chown root: libpepflashplayer.so manifest.json
#~ chmod 644 libpepflashplayer.so
#~ mv libpepflashplayer.so manifest.json $TARG/usr/lib${LIBDIRSUFFIX}/PepperFlash
#~ dir2xzm $TARG $WRKDIR/pepperflash-$latver-$ARCH-1.xzm  || { red "Failed to create porteus module."; exit 1; }

#~ echo
#~ echo -e "Your module is ready. We will now close opera and activate the module."
#~ read -p " Would you like to continue? [y/n]" -n 1 -r -s && echo

#~ if [[ $REPLY =~ ^[Yy]$ ]]; then
	#~ for a in `pidof opera`; do
		#~ kill $a 2>/dev/null
	#~ done
	#~ activate $WRKDIR/pepperflash-$latver-$ARCH-1.xzm quiet
	#~ echo
	#~ echo "Your pepperflash module is in $WRKDIR"
	#~ echo
#~ else
	#~ die
#~ fi


