#!/bin/bash

# Copyright (C) 2007-2023 X2Go Project - https://wiki.x2go.org
# Copyright (C) 2007-2023 Oleksandr Shneyder <o.shneyder@phoca-gmbh.de>
# Copyright (C) 2007-2023 Heinz-Markus Graesing <heinz-m.graesing@obviously-nice.de>
#
# 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.

usage() {
        echo "$(basename $0): needs <X2GO-SESSION> as first argument. Alternatively, you can set \$X2GO_SESSION with the correct session name" 1>&2
        exit -1
}

if [ $# -eq 1 ]; then
        SESSION_NAME="$1"
else
        SESSION_NAME="$X2GO_SESSION"
fi

if [ -z $SESSION_NAME ]; then
        usage
fi

X2GO_ROOT="${HOME}/.x2go"
PID_FILE="$X2GO_ROOT/C-$SESSION_NAME/cmd.pid"


X2GO_LIB_PATH="$(x2gopath libexec)";

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

X2GO_AGENT_PID=`$X2GO_LIB_PATH/x2gogetagent "$SESSION_NAME"`
X2GO_AGENT_PID=`echo "$X2GO_AGENT_PID"| awk {'print $1'}`

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

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

#terminate x2goruncommand process and all processes from group
#in case of XDMCP session cmd.pid won't exist
if [ -f $PID_FILE ];then
	X2GO_CMD_PID=`cat $PID_FILE`
	kill -- -$X2GO_CMD_PID
fi

if kill -TERM $X2GO_AGENT_PID &>/dev/null; then
	$X2GO_LIB_PATH/x2gosyslog "$0" "notice" "session with ID "$SESSION_NAME" has been terminated successfully"

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

else
	err_msg="ERROR: failed to terminate session with ID $SESSION_NAME"

	# some x2goclient versions behave weirdly if we give an error message here, so we only write syslog for now...
	#echo "$err_msg" 1>&2

	$X2GO_LIB_PATH/x2gosyslog "$0" "err" "$err_msg"

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

# disable ssh-agent-forwarding socket file symlink
X2GOSSH_AUTH_SOCK="$X2GO_ROOT/C-$SESSION_NAME/ssh-agent.PID"
if [ -L "$X2GOSSH_AUTH_SOCK" ]; then
	rm -f "$X2GOSSH_AUTH_SOCK"
fi

$X2GO_LIB_PATH/x2gochangestatus 'F' "$SESSION_NAME"  > /dev/null
