#!/bin/sh

set -x

ulimit -c unlimited

cd ${HOME}/NX

NX_PORT=9
export NX_PORT

NX_SYSTEM=${HOME}/.nx
#NX_SYSTEM=/etc/NX

NX_ROOT=${HOME}/.nx
#NX_ROOT=/var/NX

# Create directory for agent log.

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

# These are options written in file 'options' in session
# directory when running a session with link type MODEM
# in nxserver 1.3.1. They are passed here in the DISPLAY
# environment.
#
# cache=8M,images=32M,link=modem,type=unix-kde,cleanup=0,
# accept=62.98.198.1,cookie=9B8283E42A6D9C57C6AB43AD40EE578D,
# id=giulietta.nomachine.com-1098-6A4649FD0FCA57FAC275AF3F1C45B10F,
# media=1:1098

NX_HOST=nx/nx,display=localhost:0,cache=4096k,link=modem,cups=1,samba=1,media=1,limit=0,accept=127.0.0.1
export NX_HOST

NX_AGENT=:${NX_PORT}
export NX_AGENT

DISPLAY=${NX_HOST}:${NX_PORT}
export DISPLAY

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

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

# Color reduction for 16 and 32 bits display.

# 1 bit, 8 colors.
# NX_IMAGEMASK=128,63,240,7

# 2 bits, 64 colors.
# NX_IMAGEMASK=192,7,240,7

# 3 bits, 512 colors.
# NX_IMAGEMASK=224,5,240,4

# 4 bits, 4096 colors.
# NX_IMAGEMASK=240,4,240,2

# 5 bits, 32768 colors.
# NX_IMAGEMASK=248,3,248,2

# 6 bits, 262144 colors.
# NX_IMAGEMASK=252,2,252,1

# For all supported colors uncomment this.
# NX_IMAGEMASK=255,0,255,0

#export NX_IMAGEMASK

#NX_IMAGESPLIT=48,48,96,96
#NX_IMAGESPLIT=40,40,80,80
#NX_IMAGESPLIT=80,80,80,80
#NX_IMAGESPLIT=16384,16384,16384,16384

#export NX_IMAGESPLIT

#NX_IMAGEFRAME=1
#export NX_IMAGEFRAME

# -imgstop int           wait for confirm from nxproxy after any PutImage bigger than int
# -getifocus             mask GetInputFocus requests but wait for confirm from nxproxy
# -imgframe              let NXlib to draw a tiny rectangle around images
# -imgsplit PW,PH,BW,BH  force NXlib to split pixmaps and bitmaps to given width x height
#                        defaul is 32,32,128,128
#
# Use -ac option if you have problems with access control.

# Get current cookie produced by desktop manager.

COOKIE=`xauth list |grep ${HOSTNAME}/unix:0 | grep MIT | cut -f 5 -d ' '`

echo -ne "\nCookie for session is ${COOKIE}\n"
echo -ne "If you are running your clients as another user\n"
echo -ne "enable access to this session by running something like \n"
echo -ne "xauth add ${HOSTNAME}/unix${NX_AGENT} MIT-MAGIC-COOKIE-1 ${COOKIE}\n"
echo -ne "and be sure you provide the right display number\n"

# Let agent pass the right cookie to real X server.

xauth add ${HOSTNAME}/unix:${NX_PORT} MIT-MAGIC-COOKIE-1 ${COOKIE}
xauth add ${HOSTNAME}:${NX_PORT} MIT-MAGIC-COOKIE-1 ${COOKIE}

# 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
# with MODEM link settings in NX version 1.3.1:
#
# nxagent -name 'NX - pinzari@giulietta.nomachine.com:1097' -keyboard it \
#         -geometry 1024x768+176+132 -bs when_requested -fp unix/:7100 :1097

#nx-X11/programs/Xserver/nxagent -bs forced -fp unix/:7100 -class TrueColor -noreset \
#-geometry 800x600+0+0 -auth ${HOME}/.Xauthority -name 'NX' ${NX_AGENT} 2>>${NX_ROOT}/C-${NX_PORT}/session &

#valgrind -v --num-callers=8 --error-limit=no --trace-children=no \
nx-X11/programs/Xserver/nxagent -noreset -name 'NX' -geometry 800x600+10+100 \
-bs when_requested -fp unix/:7100 ${NX_AGENT} 2>>${NX_ROOT}/C-${NX_PORT}/session &

xauth add ${HOSTNAME}/unix${NX_AGENT} MIT-MAGIC-COOKIE-1 ${COOKIE}
xauth add ${HOSTNAME}${NX_AGENT} MIT-MAGIC-COOKIE-1 ${COOKIE}

echo ""

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

#DISPLAY=unix:0
DISPLAY=:0
export DISPLAY

# These are the nxproxy options used to run a typical session
# with MODEM link settings in NX version 1.3.1 on the client:
#
# cookie=9B8283E42A6D9C57C6AB43AD40EE578D,root=/home/pinzari/.nx,media=32824,
# session=kde_on_giulietta,id=giulietta.nomachine.com-1098-6A4649FD0FCA57FAC275AF3F1C45B10F,
# connect=giulietta.nomachine.com:1098
#
# These options are stored in a file and usually passed to
# proxy by means of a options=file parameter.

#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 link=modem,cups=22,samba=22,media=22,connect=localhost:${NX_PORT} \
2>>${HOME}/.nx/S-${NX_PORT}/session &

echo ""
