#!/bin/bash

# Copyright (C) 2007-2014 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-2014 Oleksandr Shneyder <oleksandr.shneyder@obviously-nice.de>
# Copyright (C) 2007-2014 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="$(x2gopath libexec)";

$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'`
args=''

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=$(x2gopath xinerama)
NX_LIBS=$(x2gopath nx-x11)
test -n "$LD_LIBRARY_PATH" && \
  LD_LIBRARY_PATH="$NX_XINERAMA_LIBS:$NX_LIBS:$LD_LIBRARY_PATH" || \
  LD_LIBRARY_PATH="$NX_XINERAMA_LIBS:$NX_LIBS"
$X2GO_LIB_PATH/x2gosyslog "$0" "debug" "exporting LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH

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

# detect Ubuntu version via /etc/lsb-release (not supported by Debian)
if [ -e "/etc/lsb-release" ]; then
	source /etc/lsb-release
fi

# let x2goruncommand choose what command to use for a given desktop shell name (GNOME, UNITY, KDE, XFCE4, LXDE, TRINITY, MATE)

# NOTES on GNOME startup behaviour in different distributions
# ===========================================================

# Debian launches GNOME3 (in accelerated mode) when calling gnome-session

# Ubuntu launches Unity (in accelerated mode) when calling gnome-session.
# To make the GNOME variants start up properly on Debian/Ubuntu we have to make the following differentiations

# before and with Ubuntu 10.10 (maverick): GNOME -> gnome-session (would start GNOME2)
# Ubuntu 11.04 (natty): GNOME -> gnome-session --session=2d-gnome, UNITY -> gnome-session --session=2d-ubuntu (GNOME3 based desktop shells)
# Ubuntu 11.10 (oneiric) or later: GNOME -> gnome-session --session=gnome-fallback, UNITY -> gnome-session --session=ubuntu-2d (GNOME3 based desktop shells)

# before and with Debian squeeze: GNOME -> gnome-session (would start GNOME2)
# Debian wheezy: GNOME -> gnome-session --session=gnome-fallback, UNITY: not available (GNOME3 based desktop shells)

if [ "$cmd" == "GNOME" ] || [ "$cmd" == "gnome-session" ]; then

	cmd="/usr/bin/gnome-session"
	if [ "$DISTRIB_ID" == "Ubuntu" ] && [ "$(echo "$DISTRIB_RELEASE >= 11.10" | bc)" == "1" ]; then
		export DESKTOP_SESSION="gnome-fallback"
		args=" --session=$DESKTOP_SESSION"
	elif [ "$DISTRIB_ID" == "Ubuntu" ] && [ "$(echo "$DISTRIB_RELEASE == 11.04" | bc)" == "1" ]; then
		export DESKTOP_SESSION="2d-gnome"
		args=" --session=$DESKTOP_SESSION"
	elif [ "$DISTRIB_ID" == "Ubuntu" ] && [ "$(echo "$DISTRIB_RELEASE <= 10.10" | bc)" == "1" ]; then
		export DESKTOP_SESSION="gnome"
	elif cat /etc/debian_version | egrep "^(squeeze|6\.).*" >/dev/null; then
		export DESKTOP_SESSION="gnome"
	elif cat /etc/debian_version | egrep "^(wheezy|7\.).*" >/dev/null; then
		export DESKTOP_SESSION="gnome-fallback"
		args=" --session=$DESKTOP_SESSION"
	fi

elif ([ "$cmd" == "UNITY" ] || [ "$cmd" == "unity" ]); then
	cmd="/usr/bin/gnome-session"
	if [ "$DISTRIB_ID" == "Ubuntu" ] && [ "$(echo "$DISTRIB_RELEASE >= 12.10" | bc)" == "1" ]; then
		export DESKTOP_SESSION="ubuntu"
		args=" --session=$DESKTOP_SESSION"
	elif [ "$DISTRIB_ID" == "Ubuntu" ] && [ "$(echo "$DISTRIB_RELEASE == 11.10" | bc)" == "1" -o "$(echo "$DISTRIB_RELEASE == 12.04" | bc)" == "1" ]; then
		export DESKTOP_SESSION="ubuntu-2d"
		args=" --session=$DESKTOP_SESSION"
	elif [ "$DISTRIB_ID" == "Ubuntu" ] && [ "$(echo "$DISTRIB_RELEASE == 11.04" | bc)" == "1" ]; then
		export DESKTOP_SESSION="2d-ubuntu"
		args=" --session=$DESKTOP_SESSION"
	fi
	# on earlier Ubuntu versions or with non-Ubuntu Distros the ,,UNITY'' command in X2Go will launch the GNOME2 desktop shell

elif [ "$cmd" == "MATE" ]; then
	cmd="/usr/bin/mate-session"
elif [ "$cmd" == "KDE" ]; then
	cmd="/usr/bin/startkde"
elif [ "$cmd" == "XFCE4" ]; then
	cmd="/usr/bin/xfce4-session"
elif [ "$cmd" == "LXDE" ]; then
	cmd="/usr/bin/startlxde"
elif [ "$cmd" == "TRINITY" ]; then
	cmd="/usr/bin/starttrinity"
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/mate-terminal" ]; then
		cmd="/usr/bin/mate-terminal"
	elif  [ -e "/usr/bin/gnome-terminal" ]; then
		cmd="/usr/bin/gnome-terminal"
		# GNOME terminal starting with 3.8 also immediately exits
		IMEXIT="false"
	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

WCMD=`echo $cmd | cut -d " " -f 1`
EXEC=`type -P $WCMD`
EXEC_WRAPPER=""

BNAME=`basename "$EXEC"`
if [ "$BNAME" == "rdesktop" ]
then
	IMEXIT="true"
	if type padsp >/dev/null; then
		EXEC_WRAPPER="padsp"
		args=" -r sound:local"
	fi
	if [ -d "$HOME/media" ]; then
		args+=" -r disk:X2GoMedia=$HOME/media"
	fi
fi

if [ "$X2GO_SESS_TYPE" == "P" ]
then
	IMEXIT="false"
	EXEC="/bin/true"
	X2GO_SESS_TYPE="R"
fi


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

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

	x2gosetkeyboard >/dev/null 2>/dev/null &

	x2gofeature X2GO_XSESSION &>/dev/null && [ "x$X2GO_SESS_TYPE" = "xD" ] && {
		STARTUP="$cmd$args"
		$X2GO_LIB_PATH/x2gosyslog "$0" "notice" "launching session with Xsession-x2go mechanism, using STARTUP=\"$STARTUP\""
		XSESSION_EXEC="$cmd" STARTUP="/usr/bin/env LD_LIBRARY_PATH=${LD_LIBRARY_PATH} ${STARTUP}" /etc/x2go/Xsession
	} || {
		$X2GO_LIB_PATH/x2gosyslog "$0" "debug" "executing command \"$cmd$args\"..."
		$EXEC_WRAPPER $cmd$args
	}

	#### 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 "$X2GO_SESSION" 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 "$X2GO_SESSION" 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}"; rm -f "/tmp/.x2go-${USER}/session-C-${X2GO_SESSION}.log"; rmdir --ignore-fail-on-non-empty "/tmp/.x2go-${USER}"; )&
fi
