#!/bin/bash

############################################################################
#                                                                          #
#  Copyright (c) 2011, 2016 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 Medialogic S.p.A.                    #
#                                                                          #
#  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

#
# Empty makefile if no windows.
#

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

  empty_makefile $ROOT || makefile_failed

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

  touch ./config.status

  exit 0

#
# Windows only.
#

elif test "$TARGET" = "Cygwin"  || test "$TARGET" = "MinGW" || \
     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.
  #
  
  LIBS="$LIBS -L$BUILD/lib -lstdc++ -lnx -lws2_32 -lshlwapi -lntdll \
        -lpsapi -lole32"

  export LIBS

  export CFLAGS="$CFLAGS -mno-ms-bitfields"
  export CXXFLAGS="$CFLAGS -mno-ms-bitfields"

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

  echo -ne "reconfig.status\n"

  run_configure auto || run_failed

  
  #
  # Now post-process the generated makefile.
  #

  replace_targets Makefile

  archives=""

  libraries="$LIBS"

  objects="Main.o Debug.o SftpClient.o Callbacks.o Utils.o Options.o \
           DokanClient.o Sftp.o Cache.o IOFifo.o"

  add_program Makefile "nxfs" "." "$objects" "$archives" "$libraries"
  
  flush_targets Makefile

else

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

  exit 1

fi
