#!/bin/bash

############################################################################
#                                                                          #
#  Copyright (c) 2001, 2026 NoMachine, http://www.nomachine.com/.          #
#                                                                          #
#  The NXCODEC program and any derived work based on the NX software       #
#  is copyright of NoMachine. Redistribution and use of the present        #
#  software is subjects to the terms and conditions 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àrl                       #
#                                                                          #
#  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

#
# Skip build for Android and iOS
#

if test "$TARGET" = "Android" || test "$TARGET" = "iOS"; then

  empty_makefile $ROOT || makefile_failed

  touch ./config.status

  exit 0

fi

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

if test "$TARGET" = "Linux32"   || test "$TARGET" = "Linux64"   || \
   test "$TARGET" = "Cygwin"    || test "$TARGET" = "MinGW"     || \
   test "$TARGET" = "MacOSX"    || test "$TARGET" = "Solaris"   || \
   test "$TARGET" = "Android"   || test "$TARGET" = "iOS"       || \
   test "$TARGET" = "FreeBSD32" || test "$TARGET" = "FreeBSD64" || \
   test "$TARGET" = "MinGW64"; 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"

  #
  # Run the script with our parameters.
  #

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

  echo -ne "reconfig.status\n"

#
# 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 -ldl -pthread -lrt"

    else

      LIBS="$LIBS -ldl -lpthread -lrt"

    fi

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

    LIBS="$LIBS -lws2_32"

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

    LIBS="$LIBS -lpthread"

  elif test "$TARGET" = "FreeBSD32" || test "$TARGET" = "FreeBSD64"; then

    LIBS="$LIBS -lpthread"

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

    LIBS="$LIBS -lpthread -lsocket -lrt"

  fi

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

    LIBS="$LIBS -L$BUILD/lib $(get_osx_stdlib)"

  else

    LIBS="$LIBS -L$BUILD/lib -lstdc++"

  fi

  LIBS="$BUILD/lib/libnxp.a $LIBS"

  export LIBS
  export LD

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

    CFLAGS="$CFLAGS -fno-common"
    CXXFLAGS="$CXXFLAGS -fno-common"
    
    if test "$ARCHITECTURE" = "arm64"; then

    CFLAGS="$CFLAGS -DMACOSX_ARM_BUILD"
    CXXFLAGS="$CXXFLAGS -DMACOSX_ARM_BUILD"

    fi

  fi

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

    CFLAGS="$CFLAGS -pthreads"
    CXXFLAGS="$CXXFLAGS -pthreads"

  fi

  #
  # Make all warnings into errors.
  #

  CFLAGS="$CFLAGS -Werror"
  CXXFLAGS="$CXXFLAGS -Werror"

  export CFLAGS
  export CXXFLAGS

  #
  # FIXME: Set our Android LD directly in reconfig.status script
  #

  if test "$TARGET" = "Android"; then
    replace_string reconfig.status "\"\$LD\"" "${CC}"
    chmod 755 reconfig.status
  fi

  run_configure auto || run_failed

  #
  # Get the NX tree version.
  #

  tree_get_version

  #
  # Now post-process the generated makefile.
  #

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

    replace_string Makefile "-pthread" ""

  fi

  replace_targets Makefile

  objects="Main.o"

  archives="libnxcodec.a"

  libraries="$LIBS"

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

    add_rc "nxcodec.bin" "NoMachine Codec Process" "nxcodec.ico"

    objects="$objects nxcodec.rc.o"

  fi

  add_program Makefile "nxcodec" "." "$objects" "$archives" "$libraries"

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

    add_command Makefile install "mv ${ROOT}/Build/bin/nxcodec.exe ${ROOT}/Build/bin/nxcodec.bin"

  else

    add_command Makefile install "mv ${ROOT}/Build/bin/nxcodec ${ROOT}/Build/bin/nxcodec.bin"

  fi

  #
  # Make available the resulting program with
  # a different name. This name will be used
  # in the package install.
  #

  flush_targets Makefile

else

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

  exit 1

fi
