#!/bin/sh

#--admin			Запуск nxclient в обычном режиме
#--session|-s			Имя сессии, если файл в /root/.nx/config/session1.nxs, можно указывать только session1
#--linux_user|u			Имя пользователя
#--desktop|-d			gnome|kde|default|rootless-приложение
#--link_speed|-l		modem,isdn,adsl,wan,lan
#--cache_mem|-m			Размер кэша в памяти (MB)
#--ssl|-e)			true|false , для отключения шифрования указать в файле "nxloadconfig"
#				EXTERNAL_PROXY_IP="IP интерфейса сервера"
# 				для удаленных клиентов открыть порты TCP 5000-6000

#
# NX_ROOTLESS_XKB в lts.conf
#
# Раскоментировать строку NX_ROOTLESS_XKB=
# и добавить в файл "nxnode" после строки: application=$(getparam application)
# следующие строки:
# application=`echo $application | sed "s/\%2C/,/g"`
# application=`echo $application | sed "s/\%3B/;/g"`
# application=`echo $application | sed "s/\%3A/:/g"`
# 
# Для одной раскладки "nxnode" изменять не надо,
# например NX_ROOTLESS_XKB="-layout ru -variant winkeys"
#

#
# Звук и диски
#
# [ -r /usr/bin/fusermount ] && /etc/X11/xinit.d/lbus-start
# [ -n $NXUSERIP ] && export PULSE_SERVER=$NXUSERIP
#


# copy NX config files and "known_hosts"
HOME=/tmp # лучше конечно добавить в "copy_dirs" /root
cp -rT /root/ $HOME 
[ ! -d $HOME/.nx/config ] && mkdir -p $HOME/.nx/config


NX_SERVER=${NX_SERVER:-"$SERVER"}
NX_LINUX_USER=$HOSTNAME
#NX_ROOTLESS_XKB=${NX_ROOTLESS_XKB:-"-layout ru,us -variant winkeys, -option grp:alt_shift_toggle,grp_led:scroll"}

NX_SERVER="$NX_SERVER:$NX_PORT"

MORE_WORDS=TRUE
while [ $# -gt 0 -a "$MORE_WORDS" ]; do
    case "$1" in
    -*|+*)
        MORE_WORDS=
        ;;
    *)
        NX_SERVER="$1"
        shift
        ;;
    esac
done

MORE_ARGS=TRUE
while [ $# -gt 0 -a "$MORE_ARGS" ]; do
    case "$1" in
    --admin|-a)
	NX_ADMIN="true"
	shift 1
	;;
    --session|-s)
	NX_SESSION="$2"
	shift 2
	;;
    --linux_user|-u)
	NX_LINUX_USER="$2"
	shift 2
	;;
    --desktop|-d)
	NX_DESKTOP="$2"
 	shift 2
	;;
    --link_speed|-l)
	NX_LINK_SPEED="$2"
	shift 2
	;;
    --cache_mem|-m)
	NX_CACHE_MEM="$2"
	shift 2
	;;
    --ssl|-e)
	NX_SSL="$2"
	shift 2
	;;

  *)
    MORE_ARGS=;;
  esac;
done;

case "$NX_DESKTOP" in 
  gnome|GNOME)  NX_DESKTOP="gnome" ;;
  kde|KDE)      NX_DESKTOP="kde" ;;
  cde|CDE)      NX_DESKTOP="cde" ;;
  xdm|XDM)      NX_DESKTOP="xdm" ;;
  default)     
          NX_DESKTOP="console" 
          NX_DESKTOP_CUSTOM="default"
          ;;
  console)     
          NX_DESKTOP="console" 
          NX_DESKTOP_CUSTOM="console"
          ;;
  *)
          NX_DESKTOP_COMMAND=$NX_DESKTOP
          NX_DESKTOP="console"
          NX_DESKTOP_CUSTOM="application"
	  [ -z $NX_ROOTLESS_XKB ] || NX_DESKTOP_COMMAND="eval setxkbmap $NX_ROOTLESS_XKB ; $NX_DESKTOP_COMMAND"
          ;;
esac;

NX_PORT=`echo $NX_SERVER|cut -d: -f2`
NX_SERVER=`echo $NX_SERVER|cut -d: -f1`
[ "$NX_PORT" == "$NX_SERVER" ] && NX_PORT=""

# edit default session file
if [ -z $NX_SESSION ] ; then
    [ ! -e $HOME/.nx/config/terminal.nxs ] && cp /etc/ltsp/terminal.nxs $HOME/.nx/config/terminal.nxs
    NX_SESSION="$HOME/.nx/config/terminal.nxs"
    sed -i "s/Login\" >/Login\" >\n<option key=\"User\" value=\"$NX_LINUX_USER\" \/>/g" $HOME/.nx/config/terminal.nxs
    sed -i "s/\"Server host\" value=\".*\"/\"Server host\" value=\"$NX_SERVER\"/g" $HOME/.nx/config/terminal.nxs
    [ -z $NX_PORT ] || sed -i "s/\"Server port\" value=\".*\"/\"Server port\" value=\"$NX_PORT\"/g" $HOME/.nx/config/terminal.nxs
    [ -z $NX_DESKTOP ] || sed -i "s/\"Desktop\" value=\".*\"/\"Desktop\" value=\"$NX_DESKTOP\"/g" $HOME/.nx/config/terminal.nxs
    [ -z $NX_DESKTOP_CUSTOM ] || sed -i "s/\"Custom Unix Desktop\" value=\".*\"/\"Custom Unix Desktop\" value=\"$NX_DESKTOP_CUSTOM\"/g" $HOME/.nx/config/terminal.nxs
    [ -z $NX_DESKTOP_COMMAND ] || sed -i "s/\"Command line\" value=\".*\"/\"Command line\" value=\"$NX_DESKTOP_COMMAND\"/g" $HOME/.nx/config/terminal.nxs
    [ -z $NX_DESKTOP_COMMAND ] || sed -i "s/\"Virtual desktop\" value=\".*\"/\"Virtual desktop\" value=\"false\"/g" $HOME/.nx/config/terminal.nxs
    [ -z $NX_LINK_SPEED ] || sed -i "s/\"Link speed\" value=\".*\"/\"Link speed\" value=\"$NX_LINK_SPEED\"/g" $HOME/.nx/config/terminal.nxs
    [ -z $NX_CACHE_MEM ] || sed -i "s/\"Cache size\" value=\".*\"/\"Cache size\" value=\"$NX_CACHE_MEM\"/g" $HOME/.nx/config/terminal.nxs
    [ -z $NX_SSL ] || sed -i "s/\"Enable SSL encryption\" value=\".*\"/\"Enable SSL encryption\" value=\"$NX_SSL\"/g" $HOME/.nx/config/terminal.nxs
fi

# set the background
xsetroot -solid "#666699"

# configure/start minimal window manager
cat >> ~/.ratpoisonrc << EOF
escape C-M-S-F12
startup_message off
EOF
ratpoison &

# start nxclient with predefined session, sleep until it runs
while :; do
    [ -z $NX_ADMIN ] && nxclient --session $NX_SESSION || nxclient
    while ps -C nxssh >/dev/null; do sleep 1; done
done
