# Process this file with autoconf to produce a configure script.
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AC_CONFIG_SRCDIR([ESM.h])
#AC_CONFIG_HEADER([config.h])

AC_PREFIX_DEFAULT(/afs/cs/project/cmcl-pach/libra)

OSTYPE=`uname -s`

# Checks for programs.
AC_PROG_CXX
AC_PROG_CC

LIBS="-L${ac_default_prefix}/${OSTYPE}/lib"

# Checks for libraries.
#AC_CHECK_LIB([libra], [mask2int])
AC_CHECK_LIB([m], [pow])
#AC_CHECK_LIB([pthread], [pthread_create])
AC_CHECK_LIB([slp], [SLPOpen])
AC_CHECK_LIB([xml2], [xmlNewChild])
AC_CHECK_LIB([z], [gzopen])

# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([arpa/inet.h fcntl.h netinet/in.h stdlib.h sys/socket.h termios.h unistd.h])

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

# Checks for library functions.
AC_FUNC_FORK
AC_FUNC_SETVBUF_REVERSED
AC_CHECK_FUNCS([strtol strtoul])

test "x$prefix" = xNONE && prefix=$ac_default_prefix
test "x$exec_prefix" = xNONE && exec_prefix=${prefix}/${OSTYPE}

#if test "x$OSTYPE" = xOSF1 ; then \
#    pthread_lib = "-L/usr/shlib -lpthread"; \
#elif test "x$OSTYPE" = xFreeBSD ; then \
#    pthread_lib = "-lc_r"; \
#else \
#    pthread_lib = "-lpthread"; \
#fi
case $OSTYPE in
    OSF1)    pthread_lib="-L/usr/shlib -lpthread";;
    FreeBSD) pthread_lib="-lc_r";;
    *)       pthread_lib="-lpthread";;
esac

AC_ARG_WITH(libra,
 [  --with-libra=prefix     prefix where libra is installed [[prefix]]],
    libra_prefix="$withval", libra_prefix="$prefix")
AC_ARG_WITH(slp,
 [  --with-slp=prefix       prefix where slp is installed [[libraprefix]]],
    slp_prefix="$withval", slp_prefix="$libra_prefix")
AC_ARG_WITH(libxml2,
 [  --with-libxml2=prefix   prefix where libxml2 is installed [[libraprefix]]],
    xml2_prefix="$withval", xml2_prefix="$libra_prefix")
AC_CHECK_FILE(${libra_prefix}/${OSTYPE}/lib/liblibra.a, ,
              AC_MSG_ERROR("can't find liblibra.a"))
AC_CHECK_FILE(${slp_prefix}/include/slp.h, , AC_MSG_ERROR("can't find slp.h"))
AC_CHECK_FILE(${xml2_prefix}/bin/xml2-config,
              xml2_confdir="${xml2_prefix}/bin", xml2_confdir="NONE")
if test "x$xml2_confdir" = xNONE; then
    AC_CHECK_FILE(${xml2_prefix}/${OSTYPE}/bin/xml2-config,
                  xml2_confdir="${xml2_prefix}/${OSTYPE}/bin",
                  AC_MSG_ERROR("can't find xml2-config"))
fi

CPPFLAGS=`${xml2_confdir}/xml2-config --cflags`
CPPFLAGS="${CPPFLAGS} -I${libra_prefix}/include -I${xml2_prefix}/include"
ADDLIBS=`${xml2_confdir}/xml2-config --libs`
LIBS="-llibra ${ADDLIBS} ${LIBS} ${pthread_lib}"
#CFLAGS="-static -g"
CFLAGS="-g"  # XXX gonna work??

AC_DEFINE_UNQUOTED(${OSTYPE})

AC_CONFIG_FILES([Makefile])
AC_OUTPUT
