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

launch()
{	
	ISFIREFOX=$(echo "${1}"|grep firefox)$(echo "${1}"|grep iceweasel)$(echo "${1}"|grep www-browser)
	if [ ! -z "$ISFIREFOX" ]; then
		param=""
	   	for var in $1; do
	   		ISFIREFOX=$(echo "${var}"|grep firefox)$(echo "${var}"|grep iceweasel)$(echo "${var}"|grep www-browser)
          	if [  -z "$ISFIREFOX" ]; then
          	 	param=$param" "$var
          	fi
      	done
	  	/usr/local/bin/iceweasel $param &
	else
		su -c "cd /var/tmp/${LOGIN};DISPLAY=:7.0  PULSE_SERVER=127.0.0.1:4713  ESPEAKER=127.0.0.1:16001  ${1}" ${LOGIN} &
	fi

}


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

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

while [ -d  /var/tmp/${LOGIN} ] ; do

	if [ -f /var/tmp/${LOGIN}/local.exec ]; then
		COMMAND=$(head -1 /var/tmp/${LOGIN}/local.exec)
		rm -f /var/tmp/${LOGIN}/local.exec
		launch "${COMMAND}"
	fi
	
	sleep 1     
done
