#!/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.                                                   */
#/*                                                                        */
#/**************************************************************************/

# This script has some parameters hard-coded, so that it can run
# the components on a specific machine. This is just for internal
# use. Check run-nxagent if you are looking for a script suitable
# for adding your personalizations.
#
# 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=6f6ef3ca309237936ce5ee9d6ac0f6f4

#
# 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=/NX/libpng-1.2.8:/NX/jpeg-6b:/NX/nxcomp:/NX/nxcompext:/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,link=lan,pack=no-pack,keybd=1,samba=1,cups=1,limit=0,accept=127.0.0.1,cookie=$NX_COOKIE,errors=${NX_ROOT}/C-${NX_PORT}/session
#NX_HOST=nx/nx,link=lan,pack=16m-jpeg-9,keybd=1,samba=1,cups=1,limit=0,accept=127.0.0.1,cookie=$NX_COOKIE,errors=${NX_ROOT}/C-${NX_PORT}/session
NX_HOST=nx/nx,cache=8192k,link=modem,keybd=1,samba=1,cups=1,limit=0,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

DISPLAY=nx/nx,options=${NX_ROOT}/C-${NX_PORT}/options:${NX_PORT}
export DISPLAY

#
# Run the agent. if you don't have a font server running,
# remove the argument "-fp unix/:7100"
#
# These are the options used to run a typical session
# in NX version 1.5.0:
#
# nxagent -name 'NX - pinzari@giulietta.nomachine.com:1097' -keyboard it \
#         -geometry 1024x768+176+132 -bs when_requested -fp unix/:7100 :1097
#

NX_AGENT=:${NX_PORT}

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

#
# On build01 need to set the font paths explicitly:
#

#valgrind -v --num-callers=8 --error-limit=no --trace-children=no \
#valgrind --tool=memcheck --track-fds=yes \
#nx-X11/programs/Xserver/nxagent -name 'NX' -geometry 800x600+100+100 \
#-fp unix/:7100 ${NX_AGENT} 2>>${NX_ROOT}/C-${NX_PORT}/session &
nx-X11/programs/Xserver/nxagent -name 'NX' -geometry 800x600+100+100 \
-fp /usr/X/lib/X11/fonts/100dpi \
-fp /usr/X/lib/X11/fonts/CSL \
-fp /usr/X/lib/X11/fonts/F3bitmaps \
-fp /usr/X/lib/X11/fonts/TTbitmaps \
-fp /usr/X/lib/X11/fonts/Type1 \
-fp /usr/X/lib/X11/fonts/misc \
-fp /usr/X/lib/X11/fonts/75dpi \
-fp /usr/X/lib/X11/fonts/TrueType \
-fp /usr/X/lib/X11/fonts/Type3 \
-fp /usr/X/lib/X11/fonts/encodings \
-fp /usr/X/lib/X11/fonts/misc \
${NX_AGENT} 2>>${NX_ROOT}/C-${NX_PORT}/session &

#
# The X server side proxy will forward the connection
# to the display :0.
#

DISPLAY=localhost:12
export DISPLAY

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

NX_HOST=nx/nx,keybd=1,samba=1,cups=1,connect=localhost,cookie=$NX_COOKIE,errors=${NX_ROOT}/S-${NX_PORT}/session

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

echo -ne "Running the X server side NX proxy.\n"

#cachegrind -v --dumps=100000000 \
#cachegrind -v \
#valgrind -v --leak-check=yes --leak-resolution=high --show-reachable=yes \
#            --show-reachable=yes --suppressions=nxproxy/nxproxy.supp \
#            --weird-hacks=lax-ioctls --num-callers=8 --logfile-fd=2 \
nxproxy/nxproxy -S nx/nx,options=${NX_ROOT}/S-${NX_PORT}/options:${NX_PORT} \
2>>${HOME}/.nx/S-${NX_PORT}/session &

echo -ne "Session running on display :$NX_PORT.\n"

#
# To run a few clients on build01 be sure you set the
# LD_LIBRARY_PATH to the following:
#
# export LD_LIBRARY_PATH=/opt/csw/lib/sparcv8plus:/opt/csw/lib
#
