#!/bin/sh
# postinst script for flashplugin-nonfree
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#		* <postinst> `configure' <most-recently-configured-version>
#		* <old-postinst> `abort-upgrade' <new version>
#		* <conflictor's-postinst> `abort-remove' `in-favour' <package>
#		  <new-version>
#		* <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#		  <failed-install-package> <version> `removing'
#		  <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package
#

VARIANTS="iceape iceweasel mozilla firefox xulrunner midbrowser xulrunner-addons"

. /usr/share/debconf/confmodule

# previously : FP9_plugin_beta_112006.tar.gz and flash-player-plugin-9.0.21.78/
# previously : install_flash_player_9_linux.tar.gz and install_flash_player_9_linux/
# currently  : install_flash_player_9.tar.gz and install_flash_player_9_linux/

fp_exit_with_error() {
	echo $1
	echo "The Flash plugin is NOT installed."
	db_fset flashplugin-nonfree/local seen false
	db_fset flashplugin-nonfree/httpget seen false
	db_set flashplugin-nonfree/httpget false
	exit 0 # still install the Debian package, undoes the fix for #387263
}

fp_download_and_unpack() {

	cd /var/cache/flashplugin-nonfree

	db_get flashplugin-nonfree/local
	if [ -d "$RET" -a -f "$RET"/install_flash_player_9.tar.gz ]; then

		echo "Installing from local file $RET/install_flash_player_9.tar.gz"
		cp -f -p "$RET"/install_flash_player_9.tar.gz install_flash_player_9.tar.gz_TEMP
		mv -f install_flash_player_9.tar.gz_TEMP install_flash_player_9.tar.gz

	else # no local file

		db_get flashplugin-nonfree/httpget
		if [ "$RET" != "true" ]; then
			fp_exit_with_error "download or license refused"
		fi

		# setting wget options
		:> wgetrc
		echo "noclobber = off" >> wgetrc
		echo "dir_prefix = ." >> wgetrc
		echo "dirstruct = off" >> wgetrc
		echo "verbose = on" >> wgetrc
		echo "progress = dot:default" >> wgetrc

		# downloading the plugin
		echo "Downloading..."
		rm -f install_flash_player_9.tar.gz
			WGETRC=wgetrc wget http://download.macromedia.com/pub/flashplayer/installers/current/9/install_flash_player_9.tar.gz \
				|| fp_exit_with_error "download failed"
		rm -f wgetrc
		echo "Download done."

	fi # end if local file

	# verify MD5 checksum of (copied or downloaded) tarball
	rm -rf install_flash_player_9_linux/
	echo "fe7a55f86faf663b36befa19848ddd28f808666d5cf7b14311599128c6d85fb7  install_flash_player_9.tar.gz"| sha256sum -c > /dev/null 2>&1 \
		|| fp_exit_with_error "sha256sum mismatch install_flash_player_9.tar.gz"

	# unpacking and checking the plugin
	tar xzf install_flash_player_9.tar.gz || fp_exit_with_error "cannot unpack plugin"
	echo "e22c813885bf8e9f76968d67e95e2fe75dab2ac7218237ab71e7f64c0da26d57  install_flash_player_9_linux/libflashplayer.so"| sha256sum -c > /dev/null 2>&1 \
		|| fp_exit_with_error "plugin changed, not trusted"
}

OLDDIR=/usr/lib/flashplugin-nonfree-unpackdir
NEWDIR=/var/cache/flashplugin-nonfree

safe_move() {
	[ ! -f $OLDDIR/$1 ] || [ -f $NEWDIR/$1 ] || mv $OLDDIR/$1 $NEWDIR/$1 2> /dev/null || true
	[ ! -f $OLDDIR/$1 ] || [ ! -f $NEWDIR/$1 ] || rm -f $OLDDIR/$1 2> /dev/null || true
}

case "$1" in
	configure)

		fp_download_and_unpack

		install -m 644 install_flash_player_9_linux/libflashplayer.so /usr/lib/flashplugin-nonfree/
		rm -rf install_flash_player_9_linux/

		echo "Flash Plugin installed."

		if test ! -x /usr/bin/nspluginwrapper; then
			for p in $VARIANTS; do 
update-alternatives --install "/usr/lib/$p/plugins/flashplugin-alternative.so" "$p-flashplugin" /usr/lib/flashplugin-nonfree/libflashplayer.so 50; done
		else
			NSPLUGIN_DIR=/var/lib/flashplugin-nonfree/ /usr/bin/nspluginwrapper -n -i /usr/lib/flashplugin-nonfree/libflashplayer.so
		for p in $VARIANTS; do 
			update-alternatives --install "/usr/lib/$p/plugins/flashplugin-alternative.so" "$p-flashplugin" /var/lib/flashplugin-nonfree/npwrapper.libflashplayer.so 50; done
		fi

		safe_move FP9_plugin_beta_101806.tar.gz
		safe_move FP9_plugin_beta_112006.tar.gz
		safe_move install_flash_player_7_linux.tar.gz
		safe_move install_flash_player_9_linux.tar.gz
		safe_move install_flash_player_9.tar.gz
		rmdir $OLDDIR 2> /dev/null || true

		db_fset flashplugin-nonfree/local seen false
		db_fset flashplugin-nonfree/httpget seen false
		db_set flashplugin-nonfree/httpget false
	;;

	abort-upgrade|abort-remove|abort-deconfigure)
		echo "postinst called with argument \`$1'" >&2
		exit 1
	;;

	*)
		echo "postinst called with unknown argument \`$1'" >&2
		exit 1
	;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

# vim: ts=2 sw=2
