#!/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"

###
### Adapt to your needs: X2GOKDRIVEAGENT_HOST, X2GOKDRIVECLIENT_HOST
###
### This script launches x2gokdrive and has to be executed on the X2GOKDRIVEAGENT_HOST.
### On the X2GOKDRIVECLIENT_HOST you will have to execute x2gokdriveclient by copy+pasting
### some command lines...
###
### (Instructions for copy+pasting are given when this script has been launched).
###
X2GOKDRIVEAGENT_HOST="127.0.0.1" # (e.g., 192.168.1.1, this scripts is launched on the X2GOKDRIVEAGENT_HOST machine)
X2GOKDRIVECLIENT_HOST="127.0.0.1" # (e.g., 192.168.1.2, you want to connect x2gokdriveclient -> x2gokdrive from the X2GOKDRIVECLIENT_HOST machine)

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 "${X2GOKDRIVECLIENT_HOST}/unix:${X2GO_PORT}" "MIT-MAGIC-COOKIE-1" "${X2GO_COOKIE}"
xauth add "${X2GOKDRIVECLIENT_HOST}:${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=${X2GOKDRIVECLIENT_HOST},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/.x2go,media=32824,
# session=kde_on_giulietta,id=giulietta.nomachine.com-1098-6A4649FD0FCA57FAC275AF3F1C45B10F,
# connect=giulietta.nomachine.com:1098
#

printf '\n%s\n\n' 'Now... COPY+PASTE the below lines to your local system'
echo '--------------8<---------'

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

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

echo "\"${X2GOKDRIVECLIENTBIN}\" -S \"nx/nx,options=${X2GO_ROOT}/S-${X2GO_PORT}/options:${X2GO_PORT}\" 2>>\"${X2GO_ROOT}/S-${X2GO_PORT}/session\""
printf '%s\n\n' '-------------->8---------'
