#!/bin/bash

############################################################################
#                                                                          #
#  Copyright (c) 2001, 2025 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

#
# Check for user-given --build parameter.
#

build_request=false

if test $# -eq 1 && test "$1" = "--build"; then
  build_request=true
fi

#
# 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

#
# Functions used in handling configuration.
#

set_flags()
{
  local flags="--enable-static --enable-shared --disable-gpl --disable-avs"

  case "$TARGET" in
    Linux64|Linux32|MacOSX)

      flags="$flags --enable-pic"

      if test "$TARGET" = "MacOS"; then
        flags="$flags --extra-asflags=-no-integrated-as"

        if test "$ARCHITECTURE" = "arm64"; then
          flags="$flags --disable-asm"
        fi
      fi

      if test -n "$CROSS"; then
        flags="$flags --cross-prefix=$CROSS-"
      fi
    ;; # Linux64|Linux32|MacOSX)

    MinGW)
       flags="$flags --host=i686-w64-mingw32"
     ;;

    MinGW64)
      flags="$flags --host=x86_64-w64-mingw32"
    ;;
  esac

  echo "$flags"
}

add_prebuilt_install()
{
  local makefile=$1
  local tag=$2

  case "${TARGET}:${ARCHITECTURE}" in
    Linux32:armv6*)
      add_command "$makefile" "$tag" \
        "cp ./prebuilt/${TARGET}/armv6/libx264.* ${ROOT}/Build/lib/"
    ;;

    Linux32:armv7*)
      add_command "$makefile" "$tag" \
        "cp ./prebuilt/${TARGET}/armv7/libx264.* ${ROOT}/Build/lib/"
    ;;

    Linux64:arm64|MacOSX:arm64)
      add_command "$makefile" "$tag" \
        "cp ./prebuilt/${TARGET}/arm64/libx264.* ${ROOT}/Build/lib/"
    ;;

    *)
      add_command "$makefile" "$tag" \
        "cp ./prebuilt/${TARGET}/libx264.* ${ROOT}/Build/lib/"
    ;;
  esac
}

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

if test "$TARGET" = "Linux32" || test "$TARGET" = "Linux64" || \
   test "$TARGET" = "MinGW64" || test "$TARGET" = "MinGW"   || \
   test "$TARGET" = "MacOSX"; then

  #
  # Generate a new configure script.
  #

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

  cp -p configure.in reconfig.status || create_failed

  echo -ne "reconfig.status\n"

  #
  # Checking if --build argument is present.
  # If not, use prebuilt libraries.
  #

  if $build_request; then

    LDFLAGS="$LDFLAGS -L$BUILD/lib"

    export LDFLAGS

    #
    # Generate a new Makefile.
    #

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

    cp -p Makefile.in Makefile || makefile_failed

    echo -ne "Makefile\n"

    #
    # Avoid linking problems on 64 bits.
    #

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

      if test "$(get_xcode_version major)" -ge 5; then

        #
        # Unsupported option in new clang. Please remove
        # after component upgrade - new version already
        # has fix for it.
        #

        replace_string reconfig.status "-falign-loops=16" ""

        chmod 755 reconfig.status

      fi
    fi

    run_configure "$(set_flags) --prefix=.libs" || run_failed

    #
    # Now post-process the generated makefile.
    #

    LDM32="-m32"
    LDM64="-m64"

    case "$ARCHITECTURE" in
      arm*|mips*|ppc*|power*)
        LDM32=
        LDM64=
        ;;
    esac

    LLDFLAGS="-lm"
    SOFLAGS=""
    case $TARGET in
      "Linux32")
        LLDFLAGS="$LLDFLAGS $LDM32"
        ;;
      "Linux64")
        LLDFLAGS="$LLDFLAGS $LDM64"
        SOFLAGS="-Xlinker -Bsymbolic"
        ;;
      "MacOSX")
        LLDFLAGS="$LLDFLAGS -m64 -arch ${ARCHITECTURE}"
        # orginal x264 makefile adds -Wl,-read_only_relocs,suppress on macosx
        # but on 64bit architectures (target for nx) there is a warning
        # ld: warning: -read_only_relocs cannot be used with x86_64
        #
        # nx output doesn't write -shared option, x264 scripts writes it
        SOFLAGS="-Xlinker -single_module"
        ;;
      "MinGW")
        LLDFLAGS="$LLDFLAGS $LDM32"
        SOFLAGS="-Xlinker --enable-auto-image-base -Xlinker --large-address-aware"
        ;;
      "MinGW64")
        LLDFLAGS="$LLDFLAGS $LDM64"
        SOFLAGS="-Xlinker --enable-auto-image-base"
        ;;
    esac

    if ! test "$TARGET" = "MinGW" && ! test "$TARGET" = "MinGW64"; then

      LLDFLAGS="$LLDFLAGS -lpthread"

    fi

    # there is a need to pass linker options also
    # the simplest way is to use LIBRARYLINK variable
    LIBRARYLINK="$LLDFLAGS $SOFLAGS"

    add_library config.mak "x264" "./.libs/lib" "128" "0" "$LIBRARYLINK"

    replace_environment config.mak

    replace_target config.mak all "original-all additional-all"

    replace_target config.mak install "all original-install additional-install"

    add_command config.mak install "cp -p .libs/include/x264.h $BUILD/include/"

    add_command config.mak install "cp -p .libs/include/x264_config.h $BUILD/include/"

    add_command config.mak distclean "rm -fr .libs"

    replace_string config.mak "^STRIP=" "#STRIP="

    add_dependency config.mak install "message"

    flush_targets config.mak

    replace_target Makefile clean "original-clean additional-clean"

    replace_target Makefile distclean "clean original-distclean additional-distclean"

    echo -ne "configure: creating ./config.status\n"

    made ./config.status

  #
  # Without build request use prebuilts.
  #

  elif test "$TARGET" = "Linux32" || test "$TARGET" = "Linux64" || \
       test "$TARGET" = "MinGW"   || test "$TARGET" = "MinGW64" || \
       test "$TARGET" = "MacOSX"; then

    create_makefile || makefile_failed

    replace_targets Makefile

    run_configure $(set_flags) --disable-asm || run_failed

    add_command Makefile install "rm -rf ${ROOT}/Build/lib/libx264.*"

    add_prebuilt_install Makefile install

    add_command Makefile install "cp ./x264.h ${ROOT}/Build/include/"
    add_command Makefile install "cp ./x264_142.h ${ROOT}/Build/include/"
    add_command Makefile install "cp ./x264_161.h ${ROOT}/Build/include/"
    add_command Makefile install "cp ./x264_config.h ${ROOT}/Build/include/"

    add_command Makefile distclean "rm -rf ${ROOT}/Build/lib/libx264.* \
      ${ROOT}/Build/include/x264.h ${ROOT}/Build/include/x264_142.h \
      ${ROOT}/Build/include/x264_161.h ${ROOT}/Build/include/x264_config.h \
      ./config.cache ./config.mak ./x264_config.h ./x264.pc ./config.h \
      ./config.log ./config.status ./install.status"

    flush_targets Makefile

    echo "configure: Using pre-built binary."

    made ./config.status

  fi

else

  echo -ne "configure: error: unsupported target $TARGET\n\n"

  exit 1

fi
