# Process this file with autoconf to produce a configure script.
AC_INIT(argtable2, 3, sheitmann@users.sourceforge.net)
AC_CONFIG_SRCDIR([src/argtable2.c])
AC_CONFIG_HEADER([src/config.h])

# Detect host and target
AC_CANONICAL_HOST
AC_CANONICAL_TARGET

# Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_RANLIB
AC_PATH_PROG(AR,ar)
AC_PATH_PROG(RM,rm)
AC_PATH_PROG(LN,ln)
AC_PATH_PROG(STRIP,strip)
AC_PATH_PROG(LD,ld)
AC_PROG_MAKE_SET

# Checks for libraries.
##AC_CHECK_LIB([memcheck], [memcheck_malloc])
AC_CHECK_LIB(memcheck, malloc, LMEMCHECK=-lmemcheck, LMEMCHECK=, [-L/usr/local/lib])
#AC_CHECK_LIB(dmalloc, malloc, LMEMCHECK="-DDMALLOC -ldmalloc", LMEMCHECK=, [-L/usr/local/lib])
AC_SUBST(LMEMCHECK)

# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([libintl.h stdlib.h string.h strings.h unistd.h getopt.h])
AC_CHECK_HEADER([getopt.h],GETOPTOBJS=,GETOPTOBJS="getopt.o getopt1.o")
AC_SUBST(GETOPTOBJS)

# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_SIZE_T

# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_STRTOD
AC_CHECK_FUNCS([strchr strcspn strrchr strtol])
#AC_CHECK_FUNCS([getopt_long])

# If the system provides getopt_long then we use it,
# otherwise we compile in the version supplied with argtable.
#AC_CHECK_FUNC(getopt_long,GETOPTOBJS=,GETOPTOBJS="getopt.o getopt1.o")
#AC_SUBST(GETOPTOBJS)

# Configure dynamic library target according to platform
case $target in
   *-macosx* | *-darwin*) 
      LIBDYNAMIC=libargtable2.dylib ;
      LIBDYNAMICVERS=libargtable2.$PACKAGE_VERSION.dylib ;;
   *)
      LIBDYNAMIC=libargtable2.so ;
      LIBDYNAMICVERS=libargtable2.so.$PACKAGE_VERSION ;;      
esac
AC_SUBST(LIBDYNAMIC)
AC_SUBST(LIBDYNAMICVERS)

# check for --enable-debug argument
AC_ARG_ENABLE(
    [debug],
    [  --enable-debug          enable library debugging symbols],
    [
       case $enableval in
          yes) echo "enabling debugging symbols" & DEBUGFLAGS="-g -UNDEBUG";;               
          no)  echo "disabling debugging symbols" & DEBUGFLAGS="-DNDEBUG";;
          *)   echo "illegal argument to --enable-debug" & exit 1;;
       esac
    ],
    [DEBUGFLAGS="-DNDEBUG"]
    )
AC_SUBST(DEBUGFLAGS)

AC_CONFIG_FILES([Makefile
                 argtable.spec
                 example/Makefile
                 src/Makefile
                 tests/Makefile])
AC_OUTPUT
