#!/bin/bash

#/**************************************************************************/
#/*                                                                        */
#/* Copyright (c) 2001, 2011 NoMachine, http://www.nomachine.com/.         */
#/* Copyright (c) 2015-2023 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>*/
#/*                                                                        */
#/* The X2Go KDrive test scripts have been derived from test scripts       */
#/* shipped with nx-libs (formerly known as nx-X11 / nxagent by NoMachine. */
#/*                                                                        */
#/* Redistribution and use of the present software is allowed according    */
#/* to terms specified by the GPL-2 (strictly version 2 only).             */
#/*                                                                        */
#/* All rights reserved.                                                   */
#/*                                                                        */
#/* NOTE: This software has received contributions from various other      */
#/* contributors, only the core maintainers and supporters are listed as   */
#/* copyright holders. Please contact us, if you feel you should be listed */
#/* as copyright holder, as well.                                          */
#/*                                                                        */
#/**************************************************************************/

#
# Uncomment this to enable echo.
#
#set -x
#

ulimit -c "unlimited"

X2GOKDRIVECLIENTBIN="x2gokdriveclient"
X2GOKDRIVEAGENTBIN="x2gokdrive"



X2GO_PORT="9"

X2GO_SYSTEM="${HOME}/.x2go"

X2GO_ROOT="${HOME}/.x2go"

#
# This should be randomly generated.
#
#X2GO_COOKIE="$(xauth list |grep "${HOSTNAME}/unix:\<${X2GO_PORT}\>" | grep 'MIT' | cut -f '5' -d ' ')"
X2GO_COOKIE="123efa980d2cba234ef6f73deac810ff"

#
# Create the directories for the X2Go KDrive session.
#

rm -rf "${X2GO_ROOT}/C-${X2GO_PORT}" || exit
mkdir -p "${X2GO_ROOT}/C-${X2GO_PORT}" || exit

rm -rf "${X2GO_ROOT}/S-${X2GO_PORT}" || exit
mkdir -p "${X2GO_ROOT}/S-${X2GO_PORT}" || exit

#
# Create the fake cookie for this display.
#

echo "Creating the X authorization cookie."

xauth add "${HOSTNAME}/unix:${X2GO_PORT}" "MIT-MAGIC-COOKIE-1" "${X2GO_COOKIE}"
xauth add "${HOSTNAME}:${X2GO_PORT}" "MIT-MAGIC-COOKIE-1" "${X2GO_COOKIE}"

#
# Options are written in a file 'options' in the session
# directory. The agent will use the DISPLAY settings, so
# we pass in the DISPLAY the name of the options file.
#
# cache=8M,images=32M,link=modem,type=unix-kde,cleanup=0,
# accept=62.98.198.1,cookie=$X2GO_COOKIE,
# id=giulietta.nomachine.com-1098-6A4649FD0FCA57FAC275AF3F1C45B10F,
# media=1:1098
#

X2GO_HOST="nx/nx,cache=8192k,link=modem,menu=1,keybd=1,samba=0,cups=0,limit=0,\
accept=127.0.0.1,cookie=$X2GO_COOKIE,errors=${X2GO_ROOT}/C-${X2GO_PORT}/session"

echo "${X2GO_HOST}:${X2GO_PORT}" >"${X2GO_ROOT}/C-${X2GO_PORT}/options"

#
# Run the agent. if you don't have a font server running,
# remove the argument "-fp unix/:7100"
#

X2GO_AGENT=":${X2GO_PORT}"

echo "Running the X client side X2Go KDrive agent."

SAVED_DISPLAY="${DISPLAY}"

DISPLAY="nx/nx,options=${X2GO_ROOT}/C-${X2GO_PORT}/options:${X2GO_PORT}"
export DISPLAY

#valgrind -v --num-callers=8 --error-limit=no --trace-children=no \
#valgrind --num-callers=8 --tool=memcheck --leak-check=yes --show-reachable=yes --track-fds=yes \
#ldd "${X2GOKDRIVEAGENTBIN}"
"${X2GOKDRIVEAGENTBIN}" -name 'X2Go' -geometry "800x600+100+100" "${@}" \
"${X2GO_AGENT}" 2>>"${X2GO_ROOT}/C-${X2GO_PORT}/session" &

#
# The X server side proxy will forward the connection
# to the original DISPLAY.
#

DISPLAY="${SAVED_DISPLAY}"
export DISPLAY

#
# These are the x2gokdriveclient options used to run a typical session.
#
# cookie=$X2GO_COOKIE,root=/home/pinzari/.nx,media=32824,
# session=kde_on_giulietta,id=giulietta.nomachine.com-1098-6A4649FD0FCA57FAC275AF3F1C45B10F,
# connect=giulietta.nomachine.com:1098
#

#X2GO_HOST="nx/nx,keybd=1,samba=1,cups=1,connect=localhost,cookie=${X2GO_COOKIE},errors=${X2GO_ROOT}/S-${X2GO_PORT}/session"
X2GO_HOST="nx/nx,keybd=1,samba=0,cups=0,connect=localhost,cookie=${X2GO_COOKIE},errors=${X2GO_ROOT}/S-${X2GO_PORT}/session"

echo "${X2GO_HOST}:${X2GO_PORT}" >"${X2GO_ROOT}/S-${X2GO_PORT}/options"

echo "Running the X server side X2Go KDrive Client."

#valgrind -v --num-callers=8 --error-limit=no --trace-children=no \
#valgrind --num-callers=8 --tool=memcheck --leak-check=yes --show-reachable=yes --track-fds=yes \
#ldd "${X2GOKDRIVECLIENTBIN}"
"${X2GOKDRIVECLIENTBIN}" -S "nx/nx,options=${X2GO_ROOT}/S-${X2GO_PORT}/options:${X2GO_PORT}" \
2>>"${X2GO_ROOT}/S-${X2GO_PORT}/session" &

echo "Session running on display :$X2GO_PORT."
