#/bin/sh
# Copyright (C) 2008 José L. Redrejo Rodríguez, jredrejo at-no-spam debian.org.
# This file 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.
#
#
##ugly hack as firefox does not load extensions on sshfs


prepare()
{		if [ ! -d /var/run/firefox/${LOGIN}/.mozilla ]; then
			mkdir -p /var/run/firefox/${LOGIN}/.mozilla
			cd /var/tmp/${LOGIN}/

			rm -Rf `find /var/tmp/${LOGIN}/.mozilla -name "Cache"`
			tar -cpf - .mozilla 2> /dev/null | (cd /var/run/firefox/${LOGIN};tar xpf -)

			mount --bind /var/run/firefox/${LOGIN}/.mozilla /var/tmp/${LOGIN}/.mozilla
	
		fi
		}

free()
{
	umount -f /var/tmp/${LOGIN}/.mozilla 
	cd /var/run/firefox/${LOGIN}
	rm -Rf `find /var/run/firefox/${LOGIN}/.mozilla -name "Cache"`
	tar -cpf - .mozilla 2> /dev/null | (cd /var/tmp/${LOGIN};tar xpf -)
	rm -Rf /var/run/firefox/${LOGIN} 
		
}

launch()
{
	
	su -c "cd /var/tmp/${LOGIN};DISPLAY=:7.0  PULSE_SERVER=127.0.0.1:4713  ESPEAKER=127.0.0.1:16001  /usr/lib/iceweasel/iceweasel $@" ${LOGIN} &

}

LOGIN=$(cat $(ls -1 /var/run/ltsplogin*|head -1))


prepare

launch "$@"

sleep 2

ALIVE=$(ps -AF|grep firefox-bin|grep -v grep)
while [ ! -z "$ALIVE" ] ; do

	sleep 1 
	ALIVE=$(ps -AF|grep firefox-bin|grep -v grep)
	    
done

free
