#!/bin/sh
TARBALLDIR="/var/lib/flash-player-plugin"
FILENAME="flash-plugin-10.3.181.14-release.i386.rpm"
FILETYPE="rpm"
MD5SUM1="257d2a592f416436d49e0322b30b4ef4"
MD5SUM2="257d2a592f416436d49e0322b30b4ef4"
MD5SUM3=
URL1="http://fpdownload.macromedia.com/get/flashplayer/current/flash-plugin-10.3.181.14-release.i386.rpm"
URL2="http://linuxdownload.adobe.com/linux/i386/flash-plugin-10.3.181.14-release.i386.rpm"
URL3=
FILE1_SRC="usr/lib/flash-plugin/libflashplayer.so"
FILE1_DST="/usr/lib/mozilla/plugins/libflashplayer.so"
FILE2_SRC="usr/lib/flash-plugin/LICENSE"
FILE2_DST="/usr/lib/mozilla/plugins/LICENSE.flashplayer"
FILE3_SRC="usr/lib/flash-plugin/README"
FILE3_DST="/usr/lib/mozilla/plugins/README.flashplayer"

FILE4_SRC="usr/bin/flash-player-properties"
FILE4_DST="/usr/bin/flash-player-properties"
FILE5_SRC="usr/share/applications/flash-player-properties.desktop"
FILE5_DST="/usr/share/applications/flash-player-properties.desktop"

FILE6_SRC="usr/share/icons/hicolor/16x16/apps/flash-player-properties.png"
FILE6_DST="/usr/share/icons/hicolor/16x16/apps/flash-player-properties.png"
FILE7_SRC="usr/share/icons/hicolor/22x22/apps/flash-player-properties.png"
FILE7_DST="/usr/share/icons/hicolor/22x22/apps/flash-player-properties.png"
FILE8_SRC="usr/share/icons/hicolor/24x24/apps/flash-player-properties.png"
FILE8_DST="/usr/share/icons/hicolor/24x24/apps/flash-player-properties.png"
FILE9_SRC="usr/share/icons/hicolor/32x32/apps/flash-player-properties.png"
FILE9_DST="/usr/share/icons/hicolor/32x32/apps/flash-player-properties.png"
FILE10_SRC="usr/share/icons/hicolor/48x48/apps/flash-player-properties.png"
FILE10_DST="/usr/share/icons/hicolor/48x48/apps/flash-player-properties.png"

FILE11_SRC="usr/lib/kde4/kcm_adobe_flash_player.so"
FILE11_DST="/usr/lib/kde4/kcm_adobe_flash_player.so"
FILE12_SRC="usr/share/kde4/services/kcm_adobe_flash_player.desktop"
FILE12_DST="/usr/share/kde4/services/kcm_adobe_flash_player.desktop"

FILE13_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
	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 ! 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

extractdir=$(mktemp -d --tmpdir=/tmp)
if [ -z "$extractdir" ]; then
	echo "Error during extraction." >&2
	exit 1
fi

cd "$extractdir"
if [ "$FILETYPE" = "rpm" ]; then
	rpm2cpio "$TARBALLDIR/$FILENAME" | cpio -i --quiet -d
else
	tar -xzf "$TARBALLDIR/$FILENAME"
fi

FILENUM=1
FILE_SRC="$FILE1_SRC"
FILE_DST="$FILE1_DST"
while [ -n "$FILE_SRC" ]; do
	mv -f "$FILE_SRC" "$FILE_DST"
	FILENUM=$((FILENUM+1))
	eval FILE_SRC="\$FILE${FILENUM}_SRC"
	eval FILE_DST="\$FILE${FILENUM}_DST"
done
rm -rf "$extractdir"
echo "Installation successful."
/usr/bin/gtk-update-icon-cache --force --quiet /usr/share/icons/hicolor &>/dev/null
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
	echo "Note: Adobe Flash Player configurator can only be accessed from the normal Tools"
	echo "menu instead of KDE System Settings (due to this being a 64bit system)."
	rm -f /usr/share/kde4/services/kcm_adobe_flash_player.desktop
	sed -i 's,NotShowIn=KDE;,,' /usr/share/applications/flash-player-properties.desktop
	# otherwise KDE hides it:
	sed -i 's,GNOME;,,' /usr/share/applications/flash-player-properties.desktop
fi
