#!/bin/bash

# Copyright (C) 2007-2012 X2Go Project - http://wiki.x2go.org
#
# This program 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Copyright (C) 2007-2012  Oleksandr Shneyder <oleksandr.shneyder@obviously-nice.de>
# Copyright (C) 2007-2012  Heinz-Markus Graesing <heinz-m.graesing@obviously-nice.de>

DISPLAY=":$1"
X2GO_AGENT_PID=$2
X2GO_SESSION=$3
cmd=$5
sndsys=$6
X2GO_SESS_TYPE=$7

X2GO_LIB_PATH=`echo -n \$(x2gobasepath)/lib/x2go`

$X2GO_LIB_PATH/x2gosyslog "$0" "info" "$(basename $0) called with options: $@"

export DISPLAY
export X2GO_AGENT_PID
export X2GO_SESSION
cmd=`echo $cmd |sed 's/X2GO_SPACE_CHAR/ /g'`

X2GO_ROOT="${HOME}/.x2go"
MESSAGE_FILE="$X2GO_ROOT/C-$X2GO_SESSION/cmdoutput"
echo "exec $cmd" >> "$MESSAGE_FILE"

NX_XINERAMA_CONF="$X2GO_ROOT/C-$X2GO_SESSION/xinerama.conf"
export NX_XINERAMA_CONF

# make Kerberos's ticket cache known inside the X2Go session
test -n $KRB5CCNAME && export KRB5CCNAME

# disable rastering in >= KDE4.8 and Qt4.8
export QT_GRAPHICSSYSTEM=native

IMEXIT="false"

NX_XINERAMA_LIBS=/usr/lib/nx/X11/Xinerama
test -n "$LD_LIBRARY_PATH" && LD_LIBRARY_PATH=$NX_XINERAMA_LIBS:$LD_LIBRARY_PATH || LD_LIBRARY_PATH="$NX_XINERAMA_LIBS"
export LD_LIBRARY_PATH

if [ "$sndsys" == "esd" ]; then
	export ESPEAKER=localhost:$4
elif [ "$sndsys" == "arts" ]; then
	export ARTS_SERVER=localhost:$4
fi

if [ "$cmd" == "WWWBROWSER" ]; then
	if [ -e "/usr/bin/firefox" ]; then
		cmd="/usr/bin/firefox"
	elif  [ -e "/usr/bin/iceweasel" ]; then
		cmd="/usr/bin/iceweasel"
	elif  [ -e "/usr/bin/abrowser" ]; then
		cmd="/usr/bin/abrowser"
	elif  [ -e "/usr/bin/konqueror" ]; then
		cmd="/usr/bin/konqueror"
	elif  [ -e "/usr/bin/galeon" ]; then
		cmd="/usr/bin/galeon"
	elif  [ -e "/usr/bin/chromium-browser" ]; then
		cmd="/usr/bin/chromium-browser"
	fi
fi

if [ "$cmd" == "MAILCLIENT" ]; then
	if [ -e "/usr/bin/thunderbird" ]; then
		cmd="/usr/bin/thunderbird"
	elif  [ -e "/usr/bin/icedove" ]; then
		cmd="/usr/bin/icedove"
	elif  [ -e "/usr/bin/kmail" ]; then
		cmd="/usr/bin/kmail"
	elif  [ -e "/usr/bin/evolution" ]; then
		cmd="/usr/bin/evolution"
	fi
fi

if [ "$cmd" == "OFFICE" ]; then
	if [ -e "/usr/bin/loffice" ]; then
		cmd="/usr/bin/loffice"
	elif [ -e "/usr/bin/ooffice" ]; then
		cmd="/usr/bin/ooffice"
	fi
fi

if [ "$cmd" == "TERMINAL" ]; then
	IMEXIT="true"
	if [ -e "/usr/bin/konsole" ]; then
		cmd="/usr/bin/konsole"
		# KDE4 konsole behaves differently from other terminals
		IMEXIT="false"
	elif  [ -e "/usr/bin/gnome-terminal" ]; then
		cmd="/usr/bin/gnome-terminal"
	elif  [ -e "/usr/bin/lxterminal" ]; then
		cmd="/usr/bin/lxterminal"
	elif  [ -e "/usr/bin/rxvt" ]; then
		cmd="/usr/bin/rxvt"
	elif  [ -e "/usr/bin/xterm" ]; then
		cmd="/usr/bin/xterm"
	fi
fi

EXEC=`which $cmd`

BNAME=`basename "$EXEC"`
if [ "$BNAME" == "rdesktop" ]
then
	IMEXIT="true"
fi

# run x2goserver-extensions for pre-runcommand
x2gofeature X2GO_RUN_EXTENSIONS &>/dev/null && x2goserver-run-extensions "$SESSION_NAME" pre-runcommand || true

sucessful_run=false
if [ "$EXEC" != "" ] && [ -x $EXEC ]; then
	$X2GO_LIB_PATH/x2gosyslog "$0" "debug" "running command $EXEC"

	x2gofeature X2GO_XSESSION &>/dev/null && [ "x$X2GO_SESS_TYPE" = "xD" ] && {
		STARTUP=$cmd
		$X2GO_LIB_PATH/x2gosyslog "$0" "notice" "launching session with Xsession-x2go mechanism, using STARTUP=\"$STARTUP\""
		STARTUP="$STARTUP" /etc/x2go/Xsession
	} || {
		$cmd
	}

	#### some applications can quit immediately, we will wait here as long as x2goagent exists
	if [  "$X2GO_SESS_TYPE" == "R" ] && [ "$IMEXIT" == "false" ]; then
		$X2GO_LIB_PATH/x2gosyslog "$0" "debug" "command $EXEC forked to background, waiting for agent to finish"
		while [ -d /proc/$X2GO_AGENT_PID ]; do
			sleep 1
		done
	fi
	# if we reach here the possibility of a successful command execution is rather high
	successful_run=true

	# run x2goserver-extensions for post-runcommand
	x2gofeature X2GO_RUN_EXTENSIONS &>/dev/null && x2goserver-run-extensions "$SESSION_NAME" post-runcommand || true

else
	$X2GO_LIB_PATH/x2gosyslog "$0" "err" "ERROR: command $EXEC failed to execute"
	echo "X2GORUNCOMMAND ERR NOEXEC:$cmd" > "$MESSAGE_FILE"

	# run x2goserver-extensions for fail-runcommand
	x2gofeature X2GO_RUN_EXTENSIONS &>/dev/null && x2goserver-run-extensions "$SESSION_NAME" fail-runcommand || true

fi

# stop x2godesktopsharing process gracefully...
x2gofeature X2GO_DESKTOPSHARING >/dev/null && x2goterminate-desktopsharing "$X2GO_SESSION" || true

kill -TERM  $X2GO_AGENT_PID
$X2GO_LIB_PATH/x2gochangestatus 'F' "$X2GO_SESSION"  > /dev/null
export HOSTNAME
x2goumount-session "$X2GO_SESSION"

# run logout scripts
test -r /etc/x2go/x2go_logout && . /etc/x2go/x2go_logout

# clean up session dir if not in debug mode and if session has been successful
if [ "$($X2GO_LIB_PATH/x2gologlevel)" != "7" ] && [ "x$successful_run" = "xtrue" ]; then
	(sleep 10; rm -Rf "$HOME/.x2go/C-$X2GO_SESSION")&
fi
