#!/bin/bash

TV_SCRIPT_DIR="$(dirname "$(readlink -e "$0")")"

function CheckAndCreateHeadlessConfig ()
{
	if [ ! -f ${TV_CFG_DIR}/headless_config ]
	then
		headless_command=AUTO
cat > "${TV_CFG_DIR}/headless_config" << END 
#configure headless start tool
#you may insert any program to start here for headless mode
#Examples: startkde, gnome-session, fluxbox, xfce4-session, lxde-session, etc
headless_command=AUTO
END
	fi	
}

function SetCommand ()
{
	local desktop_command=""
	local started=0
	case `echo $headless_command | tr [:upper:] [:lower:]` in
		auto)
			RunAuto
			;;
		kde | kdesktop | startkde | kdestart)
			headless_command="startkde"
			;;
		gnome | startgnome | gnome-session)
			headless_command="gnome-session"
			;;
		fluxbox | startfluxbox | fluxbox-session)
			headless_command="fluxbox"
			;;
		xfce | startxfce | xfce4 | xfce-session)
			headless_command="xfce4-session"
			;;
		lxde | startlxde | lxde-session)
			headless_command="lxde-session"
			;;
		mate | startmate | mate-session)
			headless_command="mate-session"
			;;
		xterm | x-term | term)
			headless_command="xterm"
			;;
		gnome-terminal | gnometerminal | gterminal)
			headless_command="gnome-terminal"
			;;
		mate-terminal | mateterminal | mterminal)
			headless_command="gnome-terminal"
			;;
		konsole | kterminal | kdeterminal)
			headless_command="konsole"
			;;
			
	esac
}

function RunAuto() {
	tool_list="startkde fluxbox xfce4-session lxde-session gnome-session konsole gnome-terminal terminal xterm"
	for command in $tool_list
	do
		if which "$command" > /dev/null
		then
			headless_command=$command
			break
		fi
	done
}

function RunCommand() {
	export XAUTHORITY
	$headless_command
}


