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

set -e

FLASH_VERSION=10.2.159.1
FILENAME=adobe-flashplugin_${FLASH_VERSION}.orig.tar.gz
SHA256SUM_TGZ="6776e75e77496ec5513158a8beecd7f7bbc3898b2cae0ccbe4e9113755aa095c"
PARTNER_URL=http://archive.canonical.com/pool/partner/a/adobe-flashplugin/$FILENAME

# 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 : flashplayer10_install_linux_091508.tar.gz and install_flash_player_10_linux/
# currently  : install_flash_player_10_linux.tar.gz and install_flash_player_10_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"/$FILENAME ]; then

		echo "Installing from local file $RET/$FILENAME"
		cp -f -p "$RET"/$FILENAME ${FILENAME}_TEMP
		mv -f ${FILENAME}_TEMP $FILENAME

	else # no local file

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

		# use apt proxy
		APT_PROXIES=$(apt-config shell \
		http_proxy Acquire::http::Proxy \
		https_proxy Acquire::https::Proxy \
		ftp_proxy Acquire::ftp::Proxy \
		)

		if [ -n "$APT_PROXIES" ]; then
			eval export $APT_PROXIES
		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
		echo "tries = 2" >> wgetrc

		# downloading the plugin
		echo "Downloading..."
		rm -f $FILENAME
			WGETRC=wgetrc wget $PARTNER_URL \
				|| fp_exit_with_error "download failed"
		rm -f wgetrc
		echo "Download done."

	fi # end if local file

	# verify SHA256 checksum of (copied or downloaded) tarball
	rm -rf adobe_flashplugin*/
        echo "$SHA256SUM_TGZ  $FILENAME" | sha256sum -c > /dev/null 2>&1 \
		|| fp_exit_with_error "sha256sum mismatch $FILENAME"

	# unpacking and checking the plugin
	tar xzf $FILENAME || fp_exit_with_error "cannot unpack plugin"
}

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 adobe-flashplugin*/libflashplayer.so /usr/lib/flashplugin-nonfree/
		rm -rf adobe-flashplugin*
		#install -m 644 libflashplayer.so /usr/lib/flashplugin-nonfree/
		#rm -f libflashplayer.so

		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
