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

set -e

FLASH_VERSION=10.0.45.2
FILENAME=adobe-flashplugin_${FLASH_VERSION}.orig.tar.gz
SHA256SUM_TGZ="30be012f0a680ef54fd6bca50c6bdb4addf29368b3d105496daf480abd8ac97b"
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-installer/local seen false
	db_fset flashplugin-installer/httpget seen false
	db_set flashplugin-installer/httpget false
	exit 0 # still install the Debian package, undoes the fix for #387263
}

fp_download_and_unpack() {

	cd /var/cache/flashplugin-installer

	db_get flashplugin-installer/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-installer/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 $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-installer-unpackdir
NEWDIR=/var/cache/flashplugin-installer

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-installer/
		rm -rf adobe-flashplugin*

		echo "Flash Plugin installed."

		if test ! -x /usr/bin/nspluginwrapper; then
			for p in $VARIANTS; do 
				update-alternatives --quiet --install "/usr/lib/$p/plugins/flashplugin-alternative.so" "$p-flashplugin" /usr/lib/flashplugin-installer/libflashplayer.so 50
				if readlink /etc/alternatives/"$p-flashplugin" | grep -c flashplugin-nonfree >/dev/null; then
					update-alternatives --quiet --set "$p-flashplugin" /usr/lib/flashplugin-installer/libflashplayer.so
				fi
			done
			ln -sf /usr/lib/flashplugin-installer/libflashplayer.so /usr/share/ubufox/plugins
		else
			NSPLUGIN_DIR=/var/lib/flashplugin-installer/ /usr/bin/nspluginwrapper -n -i /usr/lib/flashplugin-installer/libflashplayer.so
			for p in $VARIANTS; do 
				update-alternatives --quiet --install "/usr/lib/$p/plugins/flashplugin-alternative.so" "$p-flashplugin" /var/lib/flashplugin-installer/npwrapper.libflashplayer.so 50
				if readlink /etc/alternatives/"$p-flashplugin" | grep -c flashplugin-nonfree >/dev/null; then
					update-alternatives --quiet --set "$p-flashplugin" /var/lib/flashplugin-installer/npwrapper.libflashplayer.so
				fi
			done
			ln -sf /usr/lib/flashplugin-installer/npwrapper.libflashplayer.so /usr/share/ubufox/plugins
		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 flashplayer10_install_linux_051508.tar.gz
		safe_move flashplayer10_install_linux_070208.tar.gz
		safe_move flashplayer10_install_linux_091508.tar.gz
		rmdir $OLDDIR 2> /dev/null || true

		db_fset flashplugin-installer/local seen false
		db_fset flashplugin-installer/httpget seen false
		db_set flashplugin-installer/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
