dnl Process this file with autoconf to produce a configure script.

dnl Prolog

AC_INIT(NXlib.h)
AC_PREREQ(2.13)

dnl Reset default compilation flags.

CXXFLAGS="$CXXFLAGS -O3"
CFLAGS="$CFLAGS -O3"

dnl Reset default linking directives.

LIBSTATIC=""
LIBSHARED=""

dnl Prefer headers and libraries from nx-X11, if present.

if test -d "../nx-X11/include" ; then
    CXXFLAGS="$CXXFLAGS -I../nx-X11/exports/include -I../nx-X11/lib/X11 \
                        -I../nx-X11/include -I../nx-X11/programs/Xserver/include"
    CFLAGS="$CFLAGS -I../nx-X11/exports/include -I../nx-X11/lib/X11 \
                    -I../nx-X11/include -I../nx-X11/programs/Xserver/include"
    LDFLAGS="$LDFLAGS -L../nx-X11/exports/lib"
fi

dnl Check for programs.

AC_PROG_CXX
AC_PROG_CC
AC_LANG_CPLUSPLUS

dnl Check for BSD compatible install.

AC_PROG_INSTALL

dnl Check for extra header files.

AC_PATH_XTRA

dnl Custom addition.
ac_help="$ac_help
  --with-symbols          add the -g flag to produce the debug symbols
  --with-info             enable basic log output to trace the program
  --with-valgrind         clean up allocated buffers to avoid valgrind warnings
  --with-version          use this version for produced libraries

  --with-static-png       enable static linking of PNG library
  --with-static-jpeg      enable static linking of JPEG library"

dnl Check to see if we're running under Cygwin32.

AC_DEFUN(nxproxy_CYGWIN32,
[AC_CACHE_CHECK(for Cygwin32 environment, nxproxy_cv_cygwin32,
[AC_TRY_COMPILE(,[return __CYGWIN32__;],
nxproxy_cv_cygwin32=yes, nxproxy_cv_cygwin32=no)
rm -f conftest*])
CYGWIN32=
test "$nxproxy_cv_cygwin32" = yes && CYGWIN32=yes])

dnl CygWin32 requires the stdc++ library explicitly linked.

nxproxy_CYGWIN32
if test "$CYGWIN32" = yes; then
    LIBS="$LIBS -mwindows -lstdc++"
fi

dnl Check whether we're building on a AMD64.

AC_DEFUN(nxconf_AMD64,
[AC_CACHE_CHECK(for Amd64 environment, nxconf_cv_amd64,
[AC_TRY_COMPILE(,[return (__amd64__ || __x86_64__);],
nxconf_cv_amd64=yes, nxconf_cv_amd64=no)
rm -f conftest*])
AMD64=
test "$nxconf_cv_amd64" = yes && AMD64=yes])
nxconf_AMD64

dnl Check to see if we're running under Solaris.

AC_DEFUN(nxconf_SUN,
[AC_CACHE_CHECK(for SunOS environment, nxconf_cv_sun,
[AC_TRY_COMPILE(,[return __sun;],
nxconf_cv_sun=yes, nxconf_cv_sun=no)
rm -f conftest*])
SUN=
test "$nxconf_cv_sun" = yes && SUN=yes])
nxconf_SUN

if test "$SUN" = yes; then
  CFLAGS="$CFLAGS -I/usr/sfw/include"
fi

if test "$SUN" = yes; then
  LDFLAGS="$LDFLAGS -G -h \$(LIBLOAD) -L/usr/sfw/lib" 
else
  LDFLAGS="$LDFLAGS -shared -Wl,-soname,\$(LIBLOAD)"
fi

dnl Check to see if we're running under FreeBSD.

AC_DEFUN(nxconf_FreeBSD,
[AC_CACHE_CHECK(for FreeBSD environment, nxconf_cv_freebsd,
[AC_TRY_COMPILE(,[return __FreeBSD__;],
nxconf_cv_freebsd=yes, nxconf_cv_freebsd=no)
rm -f conftest*])
FreeBSD=
test "$nxconf_cv_freebsd" = yes && FreeBSD=yes])
nxconf_FreeBSD

dnl Build PIC libraries.

if test "$CYGWIN32" != yes -a "$DARWIN" != yes; then
  CXXFLAGS="$CXXFLAGS -fPIC"
  CFLAGS="$CFLAGS -fPIC"
fi

dnl On FreeBSD search libraries and includes under /usr/local.

if test "$FreeBSD" = yes; then
  LIBS="$LIBS  -L/usr/local/lib"
  CXXFLAGS="$CXXFLAGS -I/usr/local/include"
  CFLAGS="$CFLAGS -I/usr/local/include"
fi

dnl Check whether --with-static-png  was
dnl given and add -lpng or libpng.a to linking.

if test "${with_static_png}" = yes; then
  echo -e "enabling static linking of PNG library"
  if test "$CYGWIN32" = yes; then
    LIBS="$LIBSTATIC -static -lpng"
  else
    if test -f "/usr/lib/libpng.a" ; then
      LIBS="/usr/lib/libpng.a $LIBS"
    else
      if test -f "/usr/local/lib/libpng.a" ; then
        echo -e "assuming libpng.a in /usr/local/lib"
        LIBS="/usr/local/lib/libpng.a $LIBS"
      else
        echo -e "assuming libpng.a in default linker path"
        LIBS="libpng.a $LIBS"
      fi
    fi
  fi
else
  echo -e "enabling dynamic linking of PNG library"
  LIBS="-lpng $LIBS"
fi

dnl Check whether --with-static-jpeg  was
dnl given and add -ljpeg or libjpeg.a to linking.
if test "${with_static_jpeg}" = yes; then
  echo -e "enabling static linking of JPEG library"
  if test "$CYGWIN32" = yes; then
    LIBSTATIC="$LIBS -static -ljpeg"
  else
    if test -f "/usr/lib/libjpeg.a" ; then
      LIBS="/usr/lib/libjpeg.a $LIBS"
    else
      if test -f "/usr/local/lib/libjpeg.a" ; then
        echo -e "assuming libjpeg.a in /usr/local/lib"
        LIBS="/usr/local/lib/libjpeg.a $LIBS"
      else
        echo -e "assuming libjpeg.a in default linker path"
        LIBS="libjpeg.a $LIBS"
      fi
    fi
  fi
else
  echo -e "enabling dynamic linking of JPEG library"
  LIBS="-ljpeg $LIBS"
fi

dnl Check whether --with-symbols or --without-symbols was
dnl given and set the required optimization level.
if test "${with_symbols}" = yes; then
  echo -e "enabling production of debug symbols"
  CXXFLAGS="-g $CXXFLAGS"
  CFLAGS="-g $CFLAGS"
else
  echo -e "disabling production of debug symbols"
fi

dnl Check whether --with-info or --without-info was given.
if test "${with_info}" = yes; then
  echo -e "enabling info output in the log file"
  CXXFLAGS="$CXXFLAGS -DINFO"
  CFLAGS="$CFLAGS -DINFO"
else
  echo -e "disabling info output in the log file"
fi

dnl Check whether --with-valgrind or --without-valgrind was given.
if test "${with_valgrind}" = yes; then
  echo -e "enabling valgrind memory checker workarounds"
  CXXFLAGS="$CXXFLAGS -DVALGRIND"
  CFLAGS="$CFLAGS -DVALGRIND"
else
  echo -e "disabling valgrind memory checker workarounds"
fi

dnl Check whether --with-version was given.
AC_SUBST(LIBVERSION)
AC_SUBST(VERSION)
if test "${with_version}" = yes; then
  VERSION=${ac_option}
else
  VERSION=`cat VERSION`
fi
echo -e "compiling version ${VERSION}"

LIBVERSION=`echo ${VERSION} | cut -d '.' -f 1`

CXXFLAGS="$CXXFLAGS -DVERSION=\\\"${VERSION}\\\""
CFLAGS="$CFLAGS -DVERSION=\\\"${VERSION}\\\""

dnl Find makedepend somewhere.

AC_SUBST(MAKEDEPEND)

if test -x "../nx-X11/config/makedepend/makedepend" ; then
  MAKEDEPEND=../nx-X11/config/makedepend/makedepend
else
  if test -x "/usr/X11R6/bin/makedepend" ; then
    MAKEDEPEND=/usr/X11R6/bin/makedepend
  else
    if test -x "/usr/openwin/bin/makedepend" ; then
      MAKEDEPEND=/usr/openwin/bin/makedepend
    else
      MAKEDEPEND=/usr/bin/makedepend
    fi
  fi
fi

dnl Determine what to build based on the platform.
dnl Override the LIBS settings on Cygwin32 so that
dnl we always link with the exact set of libraries.

AC_SUBST(ALL)

if test "$CYGWIN32" = yes; then
    ALL="\$(LIBCYGARCHIVE) \$(LIBCYGSHARED) \$(LIBARCHIVE)"
else
    ALL="\$(LIBFULL) \$(LIBLOAD) \$(LIBSHARED) \$(LIBARCHIVE)"
fi

AC_OUTPUT(Makefile)