#!/bin/sh

#/**************************************************************************/
#/*                                                                        */
#/* Copyright (c) 2001, 2011 NoMachine, http://www.nomachine.com/.         */
#/*                                                                        */
#/* NXSCRIPTS, NX protocol compression and NX extensions to this software  */
#/* are copyright of NoMachine. Redistribution and use of the present      */
#/* software is allowed according to terms specified in the file LICENSE   */
#/* which comes in the source distribution.                                */
#/*                                                                        */
#/* Check http://www.nomachine.com/licensing.html for applicability.       */
#/*                                                                        */
#/* NX and NoMachine are trademarks of Medialogic S.p.A.                   */
#/*                                                                        */
#/* All rights reserved.                                                   */
#/*                                                                        */
#/**************************************************************************/

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

ulimit -c unlimited

cd ${HOME}/NX

NX_PORT=9

NX_SYSTEM=${HOME}/.nx

NX_ROOT=${HOME}/.nx

#
# This should be randlomly generated.
#
#NX_COOKIE=`xauth list |grep "${HOSTNAME}/unix:\<${NX_PORT}\>" | grep MIT | cut -f 5 -d ' '`
NX_COOKIE=123efa980d2cba234ef6f73deac810ff

#
# Create the directories for the NX session.
#

rm -rf ${NX_ROOT}/C-${NX_PORT} || exit
mkdir -p ${NX_ROOT}/C-${NX_PORT} || exit

rm -rf ${HOME}/.nx/S-${NX_PORT} || exit
mkdir -p ${HOME}/.nx/S-${NX_PORT} || exit

#
# Set the path to libraries and NX executables.
#

LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${HOME}/NX/nxcomp:${HOME}/NX/nxcompext:\
${HOME}/NX/nxcompshad:${HOME}/NX/nx-X11/exports/lib"

export LD_LIBRARY_PATH

PATH="${PATH}:${HOME}/NX/nxclient/nxclient:${HOME}/NX/nx-X11/programs/Xserver/"
export PATH

#
# Create the fake cookie for this display.
#

echo -ne "Creating the X authorization cookie.\n"

xauth add ${HOSTNAME}/unix:${NX_PORT} MIT-MAGIC-COOKIE-1 ${NX_COOKIE}
xauth add ${HOSTNAME}:${NX_PORT} MIT-MAGIC-COOKIE-1 ${NX_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=$NX_COOKIE,
# id=giulietta.nomachine.com-1098-6A4649FD0FCA57FAC275AF3F1C45B10F,
# media=1:1098
#

NX_HOST=nx/nx,cache=8192k,link=modem,shadow=$1,\
accept=127.0.0.1,cookie=$NX_COOKIE,errors=${NX_ROOT}/C-${NX_PORT}/session

echo "${NX_HOST}:${NX_PORT}" >${NX_ROOT}/C-${NX_PORT}/options

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

NX_AGENT=:${NX_PORT}

echo -ne "Running the X client side NX agent.\n"

# SAVED_DISPLAY=$DISPLAY
# 
# DISPLAY=nxdev120:0
# export DISPLAY

#valgrind -v --num-callers=8 --error-limit=no --trace-children=no \
#valgrind --num-callers=8 --tool=memcheck --leak-check=no --show-reachable=yes --track-fds=no \
nx-X11/programs/Xserver/nxagent -S -options ${NX_ROOT}/C-${NX_PORT}/options \
-nopersistent -name 'NX' \
${NX_AGENT} 2>>${NX_ROOT}/C-${NX_PORT}/session &
