#!/bin/bash

############################################################################
#                                                                          #
#  Copyright (c) 2001, 2026 NoMachine, http://www.nomachine.com/.          #
#                                                                          #
#  Simple configure script used to build as part of the NX system.         #
#                                                                          #
#  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 other options.        #
#                                                                          #
#  NX and NoMachine are trademarks of NoMachine S.a.r.l.                   #
#                                                                          #
#  All rights reserved.                                                    #
#                                                                          #
############################################################################

#
# Include our configure functions.
#

if test -z "$ROOT"; then
  if test -x "../../../Configure"; then
    ROOT="../../.."
  else
    echo -ne "configure: error: can't find the main configure script\n\n"
    exit 1
  fi
fi

. $ROOT/Configure --functs || include_failed

#
# Setup the configuration environment.
#

setup_configure || setup_failed

#
# Check if the target is based on autoconf.
#

if test "$TARGET" = "Linux32"   || test "$TARGET" = "Linux64"   || \
   test "$TARGET" = "Cygwin"    || test "$TARGET" = "MinGW"     || \
   test "$TARGET" = "MinGW64"   || test "$TARGET" = "MacOSX"    || \
   test "$TARGET" = "Solaris"   || test "$TARGET" = "Android"   || \
   test "$TARGET" = "FreeBSD32" || test "$TARGET" = "FreeBSD64" || \
   test "$TARGET" = "iOS"; then

  #
  # Generate a new configure script.
  #

  echo -ne "configure: generating the original configure script... "

  autoconf -o reconfig.status || create_failed

  echo -ne "reconfig.status\n"

  #
  # Unroll the loops since the modest
  # increase in size can benefit all
  # the libnx modules.
  #

  flags="-funroll-loops"

  #
  # Don't use the glibc C replacements
  # for the system functions. This is
  # in the top configure for the other
  # platforms.
  #

  if test "$TARGET" = "Cygwin" || test "$TARGET" = "MinGW"; then

    flags="$flags -fno-builtin"

  fi

  #
  # Solaris wants -pthreads specified
  # in the compilation flags.
  #

  if test "$TARGET" = "Solaris"; then

    flags="$flags -pthreads"

  fi

  #
  # Make all warnings into errors and
  # apply the additional flags.
  #

  flags="$flags -Wall -Wpointer-arith -Werror"

  CFLAGS="$CFLAGS $flags"
  CXXFLAGS="$CXXFLAGS $flags"


  #
  # Run the script with our parameters.
  #

  echo -ne "configure: running the original configure script... "

  echo -ne "reconfig.status\n"

  run_configure auto || run_failed

  #
  # Get the NX tree version.
  #

  tree_get_version

  #
  # Now post-process the generated makefile.
  #

  replace_targets Makefile

  libs="-lm -lstdc++"
#
# FIXME: Do we need -lrt on OSX for the semaphores to work?
#        In this case we may factor the -lrt directive and
#        link the library on all platforms except MinGW.
#
  if test "$TARGET" = "Linux32" || test "$TARGET" = "Linux64"; then

    if test `gcc -dumpversion | cut -d '.' -f 1` -ge 3; then

      libs="$libs -lrt -pthread"

    else

      libs="$libs -lrt -lpthread"

    fi

  elif test "$TARGET" = "MinGW" || test "$TARGET" = "MinGW64"; then

    libs="$libs -liphlpapi -lws2_32"

  elif test "$TARGET" = "MacOSX"; then

    libs="$libs -lpthread"

  elif test "$TARGET" = "Solaris"; then

    libs="$libs -lrt -lpthread -lsocket"

  fi

  add_library Makefile "nxp" "." "$VERSION" "$VERSIONMAJOR" "$libs"

  flush_targets Makefile

else

  echo -ne "configure: error: don't know what to do for target $TARGET\n\n"

  exit 1

fi
