#!/bin/sh
TARBALLDIR="/var/lib/flash-player-plugin-preview"
FILENAME="install_flash_player_11_linux.x86_64.tar.gz"
FILETYPE="tar"
MD5SUM1="f3cec2b9b38bd58f5e20f543d957bc17"
MD5SUM2=""
MD5SUM3=
URL1="http://fpdownload.macromedia.com/get/flashplayer/pdc/11.2.202.236/install_flash_player_11_linux.x86_64.tar.gz"
URL2=""
URL3=
FILE1_SRC="libflashplayer.so"
FILE1_DST="/usr/lib64/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

checkmd5sum() {
	[ -e "$1" ] || return 1
	FILEMD5="$(md5sum $1 | cut -d" " -f1)"
	[ -n "$FILEMD5" ] || return 1
	MD5NUM=1
	eval MD5SUM="\$MD5SUM$MD5NUM"
	while [ "$MD5SUM" ]; do
		[ "$MD5SUM" = "$FILEMD5" ] && return 0
		MD5NUM=$((MD5NUM+1))
		eval MD5SUM="\$MD5SUM$MD5NUM"
	done
	return 1
}


if [ -n "$LOCALFILE" ]; then
	if [ ! -e "$LOCALFILE" ]; then
		echo "Error: Specified file \"$LOCALFILE\" does not exist." >&2
		exit 1
	fi
	if [ -z "$NO_CHECK" ] && ! checkmd5sum "$LOCALFILE" &>/dev/null; then
		echo "Error: Specified file fails md5 hash check." >&2
		exit 1
	fi

	cp "$LOCALFILE" "$TARBALLDIR/$FILENAME"

else
	URLNUM=1
	FAILURE=false
	cat <<EOF
Note that by downloading the Adobe Flash Player Preview version you
indicate your acceptance of the following documents:
- Flash Player 10 License: http://labs.adobe.com/technologies/eula/flashplayer10.html
- Adobe.com Terms of Use: http://www.adobe.com/misc/terms.html
- Adobe Online Privacy Policy: http://www.adobe.com/misc/privacy.html
EOF
	while ! checkmd5sum "$TARBALLDIR/$FILENAME"; 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" >&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 download Flash Player manually from" >&2
		echo "       http://get.adobe.com/flashplayer/" >&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

echo "Installation successful."
