#!/bin/bash

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

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

if test "$TARGET" = "Linux32" || test "$TARGET" = "Linux64"; 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"

  flags="--disable-symver"

  export CCINCLUDES="$CCINCLUDES -I$BUILD/include/fuse"
  export CFLAGS="$CFLAGS -I$BUILD/include/fuse"
  export CXXFLAGS="$CXXFLAGS"

  LIBS="-L$BUILD/lib -pthread -lnxfs -lgthread-2.0 -lrt -lglib-2.0 -ldl"
  export LIBS

  run_configure auto $flags || run_failed

  #
  # Now post-process the generated makefile.
  #
  
  replace_targets Makefile

  replace_string Makefile "\$(sshfs_LINK) \$(sshfs_OBJECTS) \$(sshfs_LDADD) \$(LIBS)" ""

  objects="sshfs-fuse_opt.o sshfs-sshfs.o sshfs-cache.o"

  archives=""

  libraries="-pthread -lnxfs -lgthread-2.0 -lrt -lglib-2.0 -ldl"

  PROGRAMNAME=""

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

  flush_targets Makefile

elif test "$TARGET" = "Cygwin"  || test "$TARGET" = "MinGW" \
     test "$TARGET" = "MacOSX" || test "$TARGET" = "Solaris"; then
  
  empty_makefile $ROOT || makefile_failed

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

  touch ./config.status

  exit 0

else

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

  exit 1

fi
