#!/bin/sh

# Copyright (C) 2006-2007 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 version 2 as
# published by the Free Software Foundation.
#
# 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 "$1"
	exit 1
}

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

show_usage() {
	echo "Usage:"
	echo "  update-flashplugin-nonfree --install"
	echo "  update-flashplugin-nonfree --uninstall"
	exit 1
}

[ $# -eq 1 ] || show_usage
[ "$1" = "--install" ] || [ "$1" = "--uninstall" ] || show_usage

ACTION="$1"

UNPACKDIR=`mktemp -d /tmp/flashplugin-nonfree.XXXXXXXXXX` || die_hard "mktemp failed"
echo "$UNPACKDIR" | grep -q "^/tmp/flashplugin-nonfree\." || die_hard "paranoia"
cd "$UNPACKDIR" || die_hard "cd failed"

do_cleanup() {
	cd /
	echo "$UNPACKDIR" | grep -q "^/tmp/flashplugin-nonfree\." || die_hard "paranoia"
	rm -rf "$UNPACKDIR"
}

die_hard_with_a_cleanup() {
	do_cleanup
	die_hard "$1"
}

cp /usr/lib/flashplugin-nonfree/flashplugin-nonfree-get-lastminute . || die_hard_with_a_cleanup "cp failed"
cp /usr/lib/flashplugin-nonfree/pubkey.asc . || die_hard_with_a_cleanup "cp failed"

sh flashplugin-nonfree-get-lastminute || die_hard_with_a_cleanup "get-lastminute failed"
sh flashplugin-nonfree-lastminute "$ACTION" || die_hard_with_a_cleanup "failed: $ACTION"

do_cleanup

echo "Adobe Flash Player installed."
