#!/bin/sh

set -e

PATH=/bin:/usr/bin

thisuser=`whoami`
if [ "$thisuser" = "root" ]
then
	echo "don't run this as root"
	exit 1
fi

for url in \
	http://ftp.debian.org/debian/pool/main/n/nss/libnss3-1d_3.12.0-5_i386.deb \
	http://ftp.debian.org/debian/pool/main/n/nspr/libnspr4-0d_4.7.1-4_i386.deb \
	http://ftp.debian.org/debian/pool/main/c/curl/libcurl3_7.18.2-5_i386.deb \
	http://ftp.debian.org/debian/pool/main/libs/libssh2/libssh2-1_0.18-1_i386.deb \
	http://ftp.debian.org/debian/pool/main/libi/libidn/libidn11_1.10-2.1_i386.deb \
; do
	file=`basename $url`
	[ -f $file ] || wget $url
	newfile=`echo $file | sed -e "s,_,-emul_," | sed -e "s,_i386,_amd64,"`

	rm -rf deb data control

	mkdir deb
	cd deb
	ar xf ../$file
	cd ..

	mkdir data
	cd data
	tar xzf ../deb/data.tar.gz
	#find *|grep so
	cd ..
	mkdir -p emul/ia32-linux
	mv data/* emul/ia32-linux
	mv emul data
	cd data
	fakeroot tar czf ../deb/data.tar.gz *
	cd ..
	rm -rf data
	
	mkdir control
	cd control
	tar xzf ../deb/control.tar.gz
	cat control \
		| sed -e "s/^\(Package: .*\)$/\1-emul/" \
		| sed -e "s/^\(Architecture: \)i386$/\1amd64/" \
		> control.NEW
	mv control.NEW control
	cat md5sums \
		| sed -e "s,^\([^ ][^ ]*\)  \([^ ]\),\1  emul/ia32-linux/\2," \
		> md5sums.NEW
	mv md5sums.NEW md5sums
	fakeroot tar czf ../deb/control.tar.gz *
	cd ..
	rm -rf control
	
	cp -p $file $newfile
	cd deb
	fakeroot ar r ../$newfile control.tar.gz
	fakeroot ar r ../$newfile data.tar.gz
	cd ..

	rm -rf deb data control
done

echo "Run these commands as root :"
echo "  apt-get --assume-yes remove flashplugin-nonfree"
for newfile in *_amd64.deb
do
	echo "  dpkg -i `pwd`/$newfile"
done
echo "  ldconfig"
echo "Then install flashplugin-nonfree from Debian Experimental."
echo "Troubleshooting:"
echo "  update-flashplugin-nonfree --install"
echo "  update-alternatives --auto flash-mozilla.so"
echo "Restart your browser."
echo "Enjoy!"

