#/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.
#

safe_extensions()
{
#ugly hack needed in case iceweasel is being run in the ltsp-server
#inside the same ltsp chroot when ltsp-server is a 64 bits architecture.
#It happens when using this trick to run iceweasel: http://www.debian-administration.org/articles/583

	FILE_EXTENSION_MANIFEST=`find /var/tmp/ltsp/.mozilla -name extensions.ini|head -1`
	FIREFOX_PROFILE=`dirname ${FILE_EXTENSION_MANIFEST}`
	if [ -f $FILE_EXTENSION_MANIFEST ]; then
		perl -p -i -e  's#/opt/ltsp/i386/#/#g' ${FILE_EXTENSION_MANIFEST}
	fi
	if [ -f $FIREFOX_PROFILE/compatibility.ini ]; then
		perl -p -i -e  's#/opt/ltsp/i386/#/#g' ${FIREFOX_PROFILE}/compatibility.ini
	fi
	if [ -f $FIREFOX_PROFILE/xpti.dat ]; then
		perl -p -i -e  's#/opt/ltsp/i386/#/#g' ${FIREFOX_PROFILE}/xpti.dat
	fi
	
}


launch()
{
	safe_extensions
	su -c "cd /var/tmp/`cat /var/run/ltsplogin`;DISPLAY=:7.0  PULSE_SERVER=127.0.0.1:4713  ESPEAKER=127.0.0.1:16001  ${1}" ltsp &

}




if [ ! -z "$1" ]; then
	launch "${1}"
	exit 0
fi


while [ -d  /var/tmp/ltsp ] ; do

	if [ -f /var/tmp/ltsp/local.exec ]; then

		COMMAND=`head -1 /var/tmp/ltsp/local.exec`
		rm -f /var/tmp/ltsp/local.exec
		launch "${COMMAND}"
	fi
	
	sleep 1     
done
