dnl -*- sh -*-
dnl the "configure" script is made from this by running GNU "autoconf"
dnl
dnl Configuration script for the Simply Powerful Toolkit (SPTK).
dnl
dnl Copyright 1999-2004 by Alexey Parshin.
dnl
dnl This library is free software; you can redistribute it and/or
dnl modify it under the terms of the GNU Library General Public
dnl License as published by the Free Software Foundation; either
dnl version 2 of the License, or (at your option) any later version.
dnl
dnl This library is distributed in the hope that it will be useful,
dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
dnl Library General Public License for more details.
dnl
dnl You should have received a copy of the GNU Library General Public
dnl License along with this library; if not, write to the Free Software
dnl Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
dnl USA.
dnl

dnl We need at least autoconf 2.59...
AC_PREREQ(2.59)

dnl Required file in package...
AC_INIT(sptk,3.0.4)
AM_INIT_AUTOMAKE(sptk,3.0.4)

dnl SPTK library versions... (This should be changed to use standard Automake versioning stuff)
SPTK_VERSION=3.0.4

AC_SUBST(SPTK_VERSION)

dnl Docs say we need this if we have SUBDIRS defines...
AC_PROG_MAKE_SET

dnl Find commands...
AC_PROG_CC
AC_PROG_CXX
AC_PROG_LIBTOOL
AC_PROG_INSTALL
dnl Why do we need TROFF? Do we have man pages?
dnl AC_PATH_PROGS(NROFF,nroff groff)

dnl OS-specific pre-tests...
case $host in
    *cygwin* | *mingw*)
        # Handle Cygwin option *first*, before all other tests.
   AC_ARG_ENABLE(cygwin, [  --enable-cygwin         use the CygWin libraries [default=no]])
   if test x$enable_cygwin != xyes; then
       CFLAGS="$CFLAGS -mno-cygwin"
       CPPFLAGS="$CPPFLAGS -mno-cygwin"
       CXXFLAGS="$CXXFLAGS -mno-cygwin"
   fi
   ;;
esac

dnl Handle compile-time options...
AC_ARG_ENABLE(debug,    [  --enable-debug          turn on debugging [default=no]])
AC_ARG_ENABLE(excel,    [  --enable-excel          add M$ Excel support (default=yes)])
AC_ARG_ENABLE(odbc,     [  --enable-odbc           use ODBC if installed (default=yes)])
AC_ARG_ENABLE(fltk,     [  --enable-fltk           use FLTK if installed (default=yes)])
AC_ARG_ENABLE(examples, [  --enable-examples	   build examples (default=no)])

if test x$enable_debug = xyes; then
    DEBUGFLAG="-g3 -O0"
else
    DEBUGFLAG=""
fi

AM_CONDITIONAL(BUILD_EXCEL, test x$enable_excel != xno)
AM_CONDITIONAL(BUILD_EXAMPLES, test x$enable_examples = xyes)
AC_SUBST(DEBUGFLAG)

if test x$enable_odbc != xno; then
    AC_CHECK_HEADER(sqlext.h,
        AC_CHECK_LIB(odbc, SQLAllocConnect,
        AC_DEFINE(HAVE_ODBC,[1],[Defined if ODBC of some sorts is present])
        ODBC_LIBS="-lodbc"))
fi

AC_SUBST(ODBC_LIBS)
AM_CONDITIONAL(USE_ODBC, test "$ODBC_LIBS" = "-lodbc")

dnl Define the libraries and link options we'll need.
if test "x$enable_fltk" != "xno"; then
   AC_PATH_PROG(FLTK_CONFIG,fltk-config)
fi

if test x"$FLTK_CONFIG" != x; then
   FLTK_LDFLAGS=`fltk-config --ldflags`
   FLTK_CXXFLAGS=`fltk-config --cxxflags`
   FLTK_VERSION=`fltk-config --version`
fi

AM_CONDITIONAL(USE_FLTK, test x"$FLTK_CONFIG" != x )
AC_SUBST(FLTK_LDFLAGS)
AC_SUBST(FLTK_CXXFLAGS)

THEMES="$prefix"
if test "$prefix" = "NONE"; then
   THEMES="/usr/local"
fi
AC_DEFINE_UNQUOTED(THEMES_PREFIX,"$THEMES",[Themes installation prefix])

dnl Architecture checks...
AC_C_BIGENDIAN

AC_CHECK_SIZEOF(short, 2)
AC_CHECK_SIZEOF(int, 4)
AC_CHECK_SIZEOF(long, 4)

if test $ac_cv_sizeof_int -eq 4; then
  if test $ac_cv_sizeof_long -eq 4; then
    AC_DEFINE(UINTEGER32,[unsigned long],[32bit unsigned])
    AC_DEFINE(INTEGER32,[long],[32bit integer])
  else
    AC_DEFINE(UINTEGER32,[unsigned int],[32bit unsigned])
    AC_DEFINE(INTEGER32,[int],[32bit integer])
  fi
fi

dnl SPTK library uses math library functions...
AC_SEARCH_LIBS(pow, m)


AC_CHECK_HEADER(aspell.h,
    AC_CHECK_LIB(aspell, new_aspell_config,
   AC_DEFINE(HAVE_ASPELL,[1],[Defined if aspell is present])
   ASPELL_LIBS="-laspell"))

AC_SUBST(ASPELL_LIBS)
AM_CONDITIONAL(USE_ASPELL, test "$ASPELL_LIBS" = "-laspell")

dnl See if we need a .exe extension on executables...
AC_EXEEXT

dnl Define OS-specific stuff...
HLINKS=
POSTBUILD=:
THREADS=

case $host in
    *cygwin* | *mingw*)
   dnl Cygwin environment...
   CFLAGS="-mwindows -DWIN32 $CFLAGS"
   CXXFLAGS="-mwindows -DWIN32 $CXXFLAGS"
   LDFLAGS="-mwindows $LDFLAGS"
   LIBS="$LIBS -lole32 -luuid -lcomctl32 -lwsock32"
   OPTIM="$OPTIM"
   ;;

    *darwin*)
        # MacOS X uses Carbon for graphics...
        LIBS="$LIBS -framework Carbon -framework ApplicationServices"

   # Add a postbuild step after linking applications
   POSTBUILD="/Developer/Tools/Rez -t APPL -o"
   ;;

    *)
   # All others are UNIX/X11...

   dnl I'm not sure X stuff below works at all -- IV
   dnl Check for X11...
   AC_PATH_XTRA

   if test x$no_x = xyes; then
       #AC_MSG_ERROR(Configure could not find required X11 libraries, aborting.)
       LIBS="$LIBS $X_EXTRA_LIBS"
       AC_MSG_WARN(Can not find X - building without it.)
   else
       LIBS="$LIBS -lXext -lX11 $X_EXTRA_LIBS"
       LDFLAGS="$X_LIBS $LDFLAGS"
   fi

   if test "x$X_PRE_LIBS" != x; then
       AC_MSG_WARN(Ignoring libraries \"$X_PRE_LIBS\" requested by configure.)
   fi

   CFLAGS="$CFLAGS$X_CFLAGS"
   CXXFLAGS="$CXXFLAGS$X_CFLAGS"

   if test "x$x_includes" != x; then
       ac_cpp="$ac_cpp -I$x_includes"
   fi

esac

AC_SUBST(POSTBUILD)

AC_CHECK_LIB(pthread,pthread_rwlock_timedrdlock,AC_DEFINE(HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK,[1],[Have pthread_rwlock_timedrdlock]))
AC_CHECK_LIB(pthread,pthread_rwlock_timedwrlock,AC_DEFINE(HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK,[1],[Have pthread_rwlock_timedwrlock]))

dnl Write all of the files...
AC_CONFIG_HEADERS(sptk3/sptk-config.h)
AC_OUTPUT(Makefile sptk3/Makefile sptk3/excel/Makefile sptk3/xml/Makefile examples/Makefile .themes.sptk/Makefile)

echo
echo "-------------------------------------"
echo

echo "Installation paths:"
echo "  for shared libraries       $prefix/lib"
echo "  for include files          $prefix/include"
echo "  for theme files            $prefix/share/sptk"
echo 

if test x$enable_excel != xno; then
   echo "Excel support:  compile with SPDB"
else
   echo "Excel support:  don't compile"
fi

if test "$ODBC_LIBS" = ""; then
   echo "ODBC support:   don't compile"
else
   echo "ODBC support:   compile with SPDB"
fi

if test "$FLTK_CONFIG" = ""; then
   echo "FLTK support:   don't compile"
else
   echo "FLTK support:   compile with SPTK"

   if test "x$ASPELL_LIBS" = "x"; then
      echo "Aspell support: don't compile"
   else
      echo "Aspell support: compile with SPTK"
   fi
fi

if test "$DEBUGFLAG" = ""; then
   echo "Debug info:     not generated"
else
   echo "Debug info:     generated ($DEBUGFLAG)"
fi

if test x$enable_examples = xyes; then
    echo "Examples:	built"
else
    echo "Examples:	not built"
fi

echo

dnl
