#!/bin/sh
TARBALLDIR="/var/lib/flash-player-plugin"
FILENAME="flash-plugin-9.0.48.0-release.i386.rpm"
FILETYPE="rpm"
MD5SUM="0d3a1a07c87f5533ee1c8e6993b00c43"
URL1="http://fpdownload.macromedia.com/get/flashplayer/current/flash-plugin-9.0.48.0-release.i386.rpm"
URL2=""
FILE1_SRC="./usr/lib/flash-plugin/libflashplayer.so"
FILE1_DST="/usr/lib/mozilla/plugins/libflashplayer.so"
FILE2_SRC=""
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

set -e

if [ ! -d "$TARBALLDIR" ]; then
	echo "Error: \"$TARBALLDIR\" does not exist." >&2
	exit 1
fi

if [ $(id -u) -ne 0 ]; then
	echo "Error: You must run this script as root." >&2
	exit 1
fi

LOCALFILE=
NO_CHECK=
while [ -n "$1" ]; do
	if [ "$1" = "--force" ]; then
		NO_CHECK=1
	else
		LOCALFILE="$1"
	fi
done

if [ -n "$LOCALFILE" ]; then
	if [ ! -e "$LOCALFILE" ]; then
		echo "Error: Specified file \"$LOCALFILE\" does not exist." >&2
		exit 1
	fi
	if [ -z "$NO_CHECK" ] && ! echo "$MD5SUM  $LOCALFILE" | md5sum -c &>/dev/null; then
		echo "Error: Specified file fails md5 hash check." >&2
		exit 1
	fi
		
	cp "$LOCALFILE" "$TARBALLDIR/$FILENAME"
	
else
	URLNUM=1
	FAILURE=false
	echo "Note that by downloading the Adobe Flash Player you indicate your acceptance of"
	echo "the EULA, available from http://www.adobe.com/products/eulas/players/flash/"
	while ! echo "$MD5SUM  $TARBALLDIR/$FILENAME" | md5sum -c &>/dev/null; do
		eval URL="\$URL$URLNUM"
		if [ -z "$URL" ]; then
			FAILURE=true
			break
		fi
		URLNUM=$((URLNUM+1))
		echo "Downloading from $URL:"
		curl --connect-timeout 20 -m 10800 -L "$URL" > "$TARBALLDIR/$FILENAME"
	done
	if $FAILURE && [ -z "$NO_CHECK" ]; then
                echo "Error: Unable to download Flash Player. This is likely due to this package" >&2
                echo "       being too old. Please contact plf-discuss@zarb.org or anssi@zarb.org or" >&2
                echo "       report a bug in the PLF bugzilla at http://plf.zarb.org/bugzilla/" >&2
                echo "       so that the package will be updated. Thank you." >&2
		echo "" >&2
		echo "       In the meantime, you can try installing Flash Player by running:" >&2
		echo "       download-flash-player-plugin --force" >&2
		echo "       Note that if successful at all, this command will likely install" >&2
		echo "       a different Flash Player version than intended." >&2
		exit 1
	fi
fi

FILENUM=1
FILE_SRC="$FILE1_SRC"
FILE_DST="$FILE1_DST"
while [ -n "$FILE_SRC" ]; do
	if [ "$FILETYPE" = "rpm" ]; then
		rpm2cpio "$TARBALLDIR/$FILENAME" | cpio -i --to-stdout --quiet "$FILE_SRC" > "$FILE_DST"
	else
		tar -xzOf "$TARBALLDIR/$FILENAME" "$FILE_SRC" > "$FILE_DST"
	fi
	FILENUM=$((FILENUM+1))
	eval FILE_SRC="\$FILE${FILENUM}_SRC"
	eval FILE_DST="\$FILE${FILENUM}_DST"
done

rm "$TARBALLDIR/$FILENAME"

echo "Installation successful."
if [ "$(uname -m)" == x86_64 ]; then
	if [ -x /usr/bin/nspluginwrapper ]; then
		echo "Detected x86_64 with nspluginwrapper, enabling the plugin on 64bit browsers too."
		/usr/bin/nspluginwrapper -i /usr/lib/mozilla/plugins/libflashplayer.so
	else
		echo "Install nspluginwrapper if you want to use the plugin with 64bit browsers too."
	fi
fi



exit 0
