#
# configure.ac for Ubit
#
# Written by Philippe Dax and Stuart Pook
# report bugs to elc@enst.fr
#
# Process this file with autoconf version 2.57 to produce a configure script.
#

# name, version, config
AC_INIT([ubit],[3.6.4])


# Make sure we are using a recent autoconf version
#AC_PREREQ(2.57)


AC_CONFIG_SRCDIR([src/uuappli.cpp])


# where to find auxiliary scripts
AC_CONFIG_AUX_DIR(support)


# use config.h
AC_CONFIG_HEADERS([src/config.h])


# canonicalize host and os
AC_CANONICAL_SYSTEM

AM_INIT_AUTOMAKE([no-define])


# package and version
set x `echo $PACKAGE_VERSION | tr '.' ' '`
major=$2
minor=$3
micro=$4
UBIT_VERSION=${major}.${minor}.${micro}

# libubit versioning (CURRENT:REVISION:AGE)
UBIT_CURRENT=0	# interface: if interface changes then C++ and R=0
UBIT_REVISION=2	# revision: R++ if no effect on C
UBIT_AGE=0	# age: A++ if changes doesn't affect bin compatibility else A=0

LTUBIT_VERSION_INFO=${UBIT_CURRENT}:${UBIT_REVISION}:${UBIT_AGE}
AC_SUBST(LTUBIT_VERSION_INFO)


#
# Compiler
#
AC_LANG([C++])
AC_PROG_CC
AC_PROG_CXX
AC_PROG_CXXCPP
AC_ISC_POSIX
AC_AIX
AC_GNU_SOURCE

AH_TEMPLATE(GCC_VERSION, [gcc version])
AH_TEMPLATE(GCC_MAJOR_VERSION, [gcc major version])
if test "$GCC" = yes; then
  gcc_version=`${CXX} --version | sed 1q | cut -f3 -d' '`
  gcc_major_version=`echo ${gcc_version} | cut -f1 -d'.'`
  AC_DEFINE_UNQUOTED(GCC_VERSION, ["${gcc_version}"])
  AC_DEFINE_UNQUOTED(GCC_MAJOR_VERSION, ${gcc_major_version})
fi

# programs needed by the build and install process
AC_PROG_MAKE_SET
AC_PROG_INSTALL

#
# can shared libraries be used?
# (shared libs dont work on Solaris when using gcc2)
#
case "$target" in
*-*-solaris*)
  AC_DISABLE_SHARED ;;
*)
  AC_ENABLE_SHARED ;;
esac
AC_PROG_LIBTOOL


#
# optimization options
#
AC_ARG_ENABLE(optimize,
              [  --enable-optimize       allow to optimize and make code faster [default=no]],
              [optimize_ok=$enableval], [optimize_ok=no])

if test "$GCC" = yes; then
  if test "$optimize_ok" = yes; then
    CXXFLAGS="-O2 -Wall -ffast-math -fomit-frame-pointer -fexpensive-optimizations -fstrict-aliasing -funroll-loops"
  else
    CXXFLAGS="-Wall"
  fi
fi


#
# debugging options
#
AC_ARG_ENABLE(debug,
              [  --enable-debug          allow debugging [default=no]],
              [debug_ok=$enableval], [debug_ok=no])

if test "$debug_ok" = yes; then
  CXXFLAGS="-g $CXXFLAGS"
  CXXFLAGS=`echo $CXXFLAGS | sed -e 's/-O[123456]*//'`
  LDFLAGS="-g"
else
  CXXFLAGS="-O2 $CXXFLAGS"
  LDFLAGS=""
fi


#
# X libraries
#
AC_PATH_XTRA

OSDEP_LIBS=""
XLIBS="-lXmu -lX11"
case "$target" in
sparc-sun-solaris*)

  AC_ARG_ENABLE(visualgamma,
                AC_HELP_STRING([--enable-visualgamma],
                        [allow use of visualgamma if available [[default=no]]]))
  if test "${enable_visualgamma:=no}" = "yes"; then
    AC_MSG_CHECKING(for XSolarisGetVisualGamma)
    AH_TEMPLATE(HAVE_XSOLARISGETVISUALGAMMA)
    AC_EGREP_HEADER(XSolarisGetVisualGamma, X11/Xmu/XmuSolaris.h, is_gamma=yes, is_gamma=no)
    if test $is_gamma = yes; then
      AC_MSG_RESULT([yes])
      AC_DEFINE(HAVE_XSOLARISGETVISUALGAMMA)
    else
      AC_MSG_RESULT([no])
    fi
  fi
  ;;
*-*-darwin*)
  CPPFLAGS="-I/usr/X11R6/include -I/sw/include $CPPFLAGS"
  OSDEP_LIBS="-L/sw/lib"
  ;;
*-*-netbsd*)
  CPPFLAGS="-I/usr/X11R6/include -I/usr/pkg/include $CPPFLAGS"
  OSDEP_LIBS="-L/usr/pkg/lib"
  ;;
*)
  ;;
esac

AC_SUBST(X_CFLAGS)
AC_SUBST(X_LIBS)
AC_SUBST(XLIBS)
LIBS="$LIBS ${X_LIBS} ${XLIBS}"
CPPFLAGS="$X_CFLAGS $CPPFLAGS"
LDFLAGS="${OSDEP_LIBS} ${X_LIBS} ${XLIBS} $LDFLAGS"


#
# searches for libungif then libgif (if libungif not found)
#
AH_TEMPLATE(HAVE_GIF_LIB_H)
AC_CHECK_HEADER([gif_lib.h],[AC_DEFINE([HAVE_GIF_LIB_H])],
                AC_MSG_WARN([unable to find gif/ungif header file]))

AC_CHECK_LIB(ungif, DGifOpenFileName, [],
             AC_MSG_WARN([unable to link with ungif!]), )

if test "$ac_cv_lib_ungif_DGifOpenFileName" = no; then
  AC_CHECK_LIB(gif, DGifOpenFileName, [],
               AC_MSG_WARN([unable to link with gif!]), )
fi


#
# searches for libjpeg
#
AH_TEMPLATE(HAVE_JPEGLIB_H)
AC_CHECK_HEADER([jpeglib.h],[AC_DEFINE([HAVE_JPEGLIB_H])],
                AC_MSG_WARN([unable to find jpeg header files]))

AC_CHECK_LIB(jpeg, jpeg_read_scanlines, [],
             AC_MSG_WARN([unable to link with JPEG lib!]), )

#
# searches for libxpm
#
AC_CHECK_LIB(Xpm, XpmGetErrorString, [],
             AC_MSG_WARN([unable to link with Xpm!]), [])


#
# searches for libxerces-c
#
AC_ARG_WITH(xerces-prefix, 
            AC_HELP_STRING([--with-xerces-prefix=DIR],
                           [where xerces-c is installed]),
                           [if test "$with_xerces_prefix" != ""; then
                            LDFLAGS="-L${with_xerces_prefix}/lib $LDFLAGS"
                            CPPFLAGS="-I${with_xerces_prefix}/include $CPPFLAGS"
                           fi])

AH_TEMPLATE(HAVE_XERCESC_DOM_HPP)
AC_CHECK_HEADER([xercesc/dom/DOM.hpp],[AC_DEFINE([HAVE_XERCESC_DOM_HPP])],
                AC_MSG_WARN([unable to find xerces header files]))

AH_TEMPLATE(HAVE_LIBXERCESC)
AC_CHECK_LIB(xerces-c, main,
             [AC_DEFINE([HAVE_LIBXERCESC])], 
             [AC_MSG_WARN([unable to link with xerces])])
AM_CONDITIONAL(XERCES, test $ac_cv_lib_xerces_c_main = yes)

# to be completed:
# HAVE_XERCES_NAMESPACE
# XERCES_NAMESPACE


#
# searches for libnsl, libsocket.
#
AC_CHECK_LIB(nsl, gethostbyname)
AC_CHECK_LIB(socket, connect)


#
# is type socklen_t available?
#
AC_MSG_CHECKING(for socklen_t)
AH_TEMPLATE(HAVE_NO_SOCKLEN_T)
AC_EGREP_HEADER(socklen_t, sys/socket.h, is_socklen=yes, is_socklen=no)
if test $is_socklen = yes; then
  AC_MSG_RESULT([yes])
else
  AC_MSG_RESULT([no])
  AC_DEFINE(HAVE_NO_SOCKLEN_T)
fi


#
# misc headers
#
AC_CHECK_HEADERS([unistd.h stdint.h X11/Xmu/XmuSolaris.h])

# generate ubit_stdint.h (stdint.h is not available on all systems)
# marche pas: AC_CREATE_STDINT_H(src/ubit_stdint.h)
#AX_CREATE_STDINT_H(src/ubit_stdint.h)

#
# Create the Makefiles
#
AC_CONFIG_FILES([
	Makefile
	src/Makefile
	src/ext/Makefile
	src/xml/Makefile
	demos/Makefile
	demos/hyperbol/Makefile
	demos/misc/Makefile
	demos/more/Makefile
	examples/Makefile
	ums/Makefile
	ums/drivers/Makefile
	images/Makefile
	support/Makefile
	ubit-config
	])


#
# commands
#
AC_CONFIG_COMMANDS([default],
	[[ chmod +x ubit-config
	cd src; if test ! -L ubit; then ln -s . ubit; fi; cd ..;
	echo `date` > stamp-h ]],[[]])

#
# config.status
#
AC_OUTPUT

