#!/bin/sh

# Copyright (C) 2006-2013 Bart Martens <bartm@knars.be>
# Copiright (C) 2014 Andrey Cherepanov <cas@altlinux.org>
#
# 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/>.

script_version="1.6"

set -e

return_0() {
	return 0
}

trap "return_0" 0

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

[ `whoami` = "root" ] || die_hard "must be root"

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

getopt_temp=`getopt -o iusfvq --long install,uninstall,status,fast,verbose,quiet,beta,unstable,unverified,clean,version \
	-n 'update-pepperflash' -- "$@"` || show_usage
eval set -- "$getopt_temp" || show_usage

ACTION=none
fast=no
verbose=no
quiet=no
variant=stable
verified=yes

cachedir=/var/cache/pepperflash
mkdir -p "$cachedir"

while [ true ]
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
			;;
		--beta)
			variant=beta
			shift
			;;
		--unstable)
			variant=unstable
			shift
			;;
		--unverified)
			verified=no
			shift
			;;
		--clean|--version)
			ACTION="$1"
			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"

case "$ACTION" in

	--install|--status|--clean|--version)
		echo "update-pepperflash is now outdated" 2>&1
		echo "feel free to remove update-pepperflash package" 2>&1
		exit 0
		;;
	--uninstall)
		[ "$verbose" != "yes" ] || echo "selected action = $ACTION"

		[ "$verbose" != "yes" ] || echo "removing files ..."

		if [ "$verbose" != "yes" ]; then
			rm -f $(getconf LIBDIR)/pepper-plugins/manifest.json $cachedir/*.rpm $cachedir/*.txt
		else
			rm -fv $(getconf LIBDIR)/pepper-plugins/manifest.json $cachedir/*.rpm $cachedir/*.txt
		fi

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

		;;
	*)

		show_usage

		;;

esac
