#!/bin/sh

# Copyright (C) 2006-2013 Bart Martens <bartm@knars.be>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -e

die_hard() {
	echo "ERROR: $1" >&2
	echo "More information might be available at:" >&2
	echo "  http://wiki.debian.org/PepperFlashPlayer" >&2
	exit 1
}

[ "$(id -u)" = "0" ] || die_hard "must be root"

show_usage() {
	echo "Usage:"
	echo "  update-pepperflashplugin-nonfree --install"
	echo "  update-pepperflashplugin-nonfree --uninstall"
	echo "  update-pepperflashplugin-nonfree --status"
	echo "Additional options:"
	echo "  --verbose"
	echo "  --quiet"
	exit 1
}

getopt_temp=$(getopt -o iusfvq --long install,uninstall,status,fast,verbose,quiet \
	-n 'update-pepperflashplugin-nonfree' -- "$@") || show_usage
eval set -- "$getopt_temp"

ACTION=none
fast=no
verbose=no
quiet=no

while [ $# -gt 0 ]
do
	case "$1" in
		-i|--install)
			ACTION="--install"
			shift
			;;
		-u|--uninstall)
			ACTION="--uninstall"
			shift
			;;
		-s|--status)
			ACTION="--status"
			shift
			;;
		-f|--fast)
			fast=yes
			shift
			;;
		-v|--verbose)
			verbose=yes
			shift
			;;
		-q|--quiet)
			quiet=yes
			shift
			;;
		--)
			shift
			break
			;;
		*)
			echo "Internal error!"
			exit 1
			;;
	esac
done

[ "$ACTION" != "none" -a $# -eq 0 ] || show_usage
[ "$quiet" != "yes" ] || verbose=no

[ "$verbose" != "yes" ] || echo "options : $getopt_temp"

arch=""
case $(dpkg --print-architecture) in
	amd64)	arch="x86_64" ;;
	i?86)	arch="i386" ;;
	*)
		die_hard "unsupported architectures" ;;
esac

cachedir=/var/cache/pepperflashplugin-nonfree

upstream=""
installed=""
if [ "$ACTION" = "--install" ] || [ "$ACTION" = "--status" ]
then
	if [ -f /usr/lib/pepperflashplugin-nonfree/libpepflashplayer.so ]
	then
		installed=$(strings /usr/lib/pepperflashplugin-nonfree/libpepflashplayer.so | awk '/LNX/{gsub(/,/,".");print$2}')
	fi
	[ "$verbose" != "yes" ] || echo "getting version of upstream"
	upstream=None
	[ "$upstream" != "" ] || die_hard "failed to determine upstream version"
fi

case "$ACTION" in

	--install)
		[ "$verbose" != "yes" ] || echo "selected action = $ACTION"

		echo "The Adobe Flash Player is EoL and no longer available for download:"
		echo "https://www.adobe.com/products/flashplayer/end-of-life.html"

		[ "$verbose" != "yes" ] || echo "end of action $ACTION"
		;;

	--uninstall)
		[ "$verbose" != "yes" ] || echo "selected action = $ACTION"

		[ "$verbose" != "yes" ] || echo "removing files ..."
		rm -f /usr/lib/pepperflashplugin-nonfree/libpepflashplayer.so
		rm -f /usr/lib/pepperflashplugin-nonfree/manifest.json

		[ "$verbose" != "yes" ] || echo "end of action $ACTION"

		;;

	--status)
		[ "$verbose" != "yes" ] || echo "selected action = $ACTION"

		echo "Flash Player version installed on this system  : $installed"
		echo "Flash Player version available on upstream site: $upstream"

		[ "$verbose" != "yes" ] || echo "end of action $ACTION"

		;;

	*)

		show_usage

		;;

esac

[ "$verbose" != "yes" ] || echo "end of update-pepperflashplugin-nonfree"

exit 0
