#!/bin/bash
######################################################################
#                                                                    #
#  Copyright (c) 2001, 2025 NoMachine, http://www.nomachine.com.     #
#                                                                    #
#  All rights reserved.                                              #
#                                                                    #
######################################################################

RunDir=${0%nxserver}
EtcDir=$(cd "$RunDir" && pwd)

if [ -x  /usr/bin/nawk ]
then
  COMMAND_AWK="/usr/bin/nawk"
elif [ -x /usr/xpg4/bin/awk ]
then
  COMMAND_AWK="/usr/xpg4/bin/awk"
elif [ -x /usr/bin/awk ]
then
  COMMAND_AWK="/usr/bin/awk"
elif [ -x /bin/awk ]
then
  COMMAND_AWK="/bin/awk"
else
  COMMAND_AWK="awk"
fi

if [ -e "/usr/xpg4/bin/id" ];
then
  COMMAND_ID="/usr/xpg4/bin/id"
elif [ -e "/bin/id" ];
then
  COMMAND_ID="/bin/id"
elif [ -e "/usr/bin/id" ];
then
  COMMAND_ID="/usr/bin/id"
else
  echo "ERROR: id command not found"
  exit 1
fi

if [ -x "/bin/head" ];
then
  COMMAND_HEAD="/bin/head"
elif [ -x "/usr/bin/head" ];
then
  COMMAND_HEAD="/usr/bin/head"
else
  COMMAND_HEAD="head"
fi

if [ -x "/bin/cut" ];
then
  COMMAND_CUT="/bin/cut"
elif [ -x "/usr/bin/cut" ];
then
  COMMAND_CUT="/usr/bin/cut"
else
  COMMAND_CUT="cut"
fi

ROOTDIR=`$COMMAND_AWK -F'"' '/^ServerRoot/ {print $2}' ${EtcDir}/server/localhost/server.cfg`

if [ "x$ROOTDIR" = "x" ];
then
  echo "Cannot found ServerRoot in ${EtcDir}/server/localhost/server.cfg configuration file";
  exit
fi

NXUSERDIR=`$COMMAND_AWK -F'"' '/^NXUserHome/ {print $2}' ${EtcDir}/server/localhost/server.cfg`

if [ "x$NXUSERDIR" = "x" ];
then
  echo "Cannot found NXUserHome in ${EtcDir}/server/localhost/server.cfg configuration file";
  exit
fi

USERID=`$COMMAND_ID -ru`
if test $? != 0;
then
  echo "Error: Cannot check user id."
  exit 1;
fi

if test "x$USERID" = "x";
then
  echo "Error: Cannot check user id."
  exit 1;
fi

if test "$1" = "--login" || test "$2" = "/etc/NX/nxserver --login" ||
     test "$1" = "--remotelogin" || test "$2" = "/etc/NX/nxserver --remotelogin";
then
  if test "x$SSH_CONNECTION" != "x"; then

    NX_SSH_STATUS_FILE="$ROOTDIR/etc/sshstatus"

    if test -f "$NX_SSH_STATUS_FILE"; then

      cat "$NX_SSH_STATUS_FILE"

      exit 1
    fi
  fi
fi

if test $USERID != 0;
then
  id_nx=`$COMMAND_ID -u nx 2>/dev/null`

  if test "x$id_nx" = "x";
  then
    echo "Error: User nx not found."
    exit 1;
  fi

  if test $id_nx -ne $USERID; then
    if test "$1" = "noloop"; then
      echo "Error: Exit for avoid infinity loop."
      exit 1;
    elif test "$1" = "--login"; then
      exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop
    elif test "$1" = "--status"; then
      exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop status
    elif test "$1" = "--upnpstatus"; then
      exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop upnpstatus
    elif test "$1" = "--startsession"; then
      exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop --startsession --display=${DISPLAY} $@
    elif test "$1" = "--userauth"; then
      exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop --userauth $2
    elif test "$1" = "--usblist"; then
      exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop --usblist $2
    elif test "$1" = "--networkid"; then
      exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop networkid
    elif test "$1" = "--networkstatus"; then
      exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop networkstatus
    elif test "$1" = "--twofactorstatus"; then
      exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop twofactorstatus
    elif test "$1" = "--getnetworkweb"; then
      exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop getnetworkweb $2
    elif test "$1" = "--serverlist"; then
      exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop serverlist "${@:2}"
    elif test "$1" = "--nodelist"; then
      exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop nodelist "${@:2}"
    elif test "$1" = "--storageget"; then
      exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop storageget $2
    elif test "$1" = "--storageadd"; then
      exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop storageadd "${@:2}"
    elif test "$1" = "--clusterstatus"; then
      exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop clusterstatus
    elif test "$1" = "--clusterlist"; then
      exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop clusterlist $2
    elif test "$1" = "--subscriptioninfo"; then
      exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop subscriptioninfo
    elif test "$1" = "--subscription"; then
      exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop subscriptioninfo
    fi

  fi

elif test "$1" = "--login"; then
  exec "$ROOTDIR/bin/nxexec" /etc/NX/nxserver noloop
fi

CONFIG_FILE="${ROOTDIR}/etc/server.cfg"

ENABLED_DEBUG=$($COMMAND_AWK -F "EnableDebug " '/^ *EnableDebug/ {gsub (/[#,A-Za-z] */," ",$2); print $2}' "${CONFIG_FILE}" | $COMMAND_HEAD -n 1 | $COMMAND_CUT -d " " -f 1)

if test -z $ENABLED_DEBUG;
then
  ENABLED_DEBUG=0
elif test $ENABLED_DEBUG != 0;
then
  ENABLED_DEBUG=1

  COMMAND_DEBUG=`$COMMAND_AWK -F "CommandDebug " '/^ *CommandDebug/ {gsub (/"/,"",$2); gsub (/^ */,"",$2); print $2}' "${CONFIG_FILE}" | $COMMAND_HEAD -n 1`
  if test -z $COMMAND_DEBUG;
  then
    ENABLED_DEBUG=0
  else
    PARAMS_DEBUG=`$COMMAND_AWK -F "DebugOptions " '/^ *DebugOptions/ {gsub (/"/,"",$2); print $2}' "${CONFIG_FILE}" | $COMMAND_HEAD -n 1`
  fi
fi

if test "x$PATH" = "x";
then
  export PATH="/bin:/usr/bin"
fi

export LD_LIBRARY_PATH="$ROOTDIR/lib:$ROOTDIR/lib/perl:$ROOTDIR/lib/perl/include:$ROOTDIR/lib/perl/nxserver:$ROOTDIR/lib/perl/libperl"
export PATH="$ROOTDIR/lib:$ROOTDIR/lib/perl:$ROOTDIR/lib/perl/include:$ROOTDIR/lib/perl/nxserver:$PATH"
export PERL5LIB="$ROOTDIR/lib:$ROOTDIR/lib/perl:$ROOTDIR/lib/perl/include:$ROOTDIR/lib/perl/nxserver:$ROOTDIR/lib/perl/libperl"
export PERL5INCLUDE="$ROOTDIR/lib/perl/nxserver"
export NX_SYSTEM="$ROOTDIR"
export NX_ROOT="$NXUSERDIR"

if test $ENABLED_DEBUG = 0;
then
  exec "$ROOTDIR/bin/nxserver.bin" "$@"
else
  export NX_FREEPERL="no"

  exec $COMMAND_DEBUG ${PARAMS_DEBUG} "$ROOTDIR/bin/nxserver.bin" "$@"
fi
