#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.62])
AC_INIT([pgreplay], [1.5.0], [https://github.com/laurenz/pgreplay/issues], [pgreplay], [http://laurenz.github.io/pgreplay/])
AC_CONFIG_SRCDIR([parse.c])
AC_CONFIG_HEADERS([config.h])
AC_CANONICAL_BUILD

# Checks for programs.
AC_PROG_INSTALL
AC_PROG_CC([gcc cc])
# add -Wall to CFLAGS for gcc
if test "$GCC" = yes; then CFLAGS="-Wall $CFLAGS"; fi

# check if ld supports -rpath
AC_MSG_CHECKING([whether the linker supports -rpath])
save_LDFLAGS="$LDFLAGS"
LDFLAGS="$LDFLAGS -Wl,-rpath,/usr/lib"
AC_LINK_IFELSE([AC_LANG_SOURCE([[main() {}]])], [rpath=yes], [rpath=no])
LDFLAGS="$save_LDFLAGS"
AC_MSG_RESULT([$rpath])

AC_ARG_WITH([postgres],
	[AS_HELP_STRING([--with-postgres=DIR],
		[specify location of pg_config for your PostgreSQL installation])],
	[if ! test -x "$with_postgres"/pg_config; then
		echo "*** pg_config not found in '$with_postgres'" 1>&2
		exit 1
	fi
	pgbindir=`"$with_postgres"/pg_config --bindir`
	AC_SUBST([pgbindir])
	if test $build_os = mingw32; then pglibdir="$pgbindir"; else pglibdir=`"$with_postgres"/pg_config --libdir`; fi
	pgincludedir=`"$with_postgres"/pg_config --includedir`
	LDFLAGS="$LDFLAGS -L$pglibdir"
	if test $rpath = yes; then LDFLAGS="$LDFLAGS -Wl,-rpath,$pglibdir"; fi
	CFLAGS="$CFLAGS -I$pgincludedir"
	PATH=`"$with_postgres"/pg_config --bindir`:$PATH])

# Install with PostgreSQL by default
AC_PREFIX_PROGRAM(pg_config)

# Checks for libraries.
AC_CHECK_LIB([pq], [PQsendPrepare], [], [AC_MSG_ERROR([
*** cannot locate PostgreSQL client library
*** required is version 8.0 or better
])])

# special Windows settings
if test $build_os = mingw32; then
	AC_SUBST([EXTRA_OBJS], [windows.o])
	# assume socks library is present
	# we have a problem using AC_CHECK_LIB because the function is __stdcall
	LIBS="$LIBS -lwsock32"
fi

# Checks for header files.
AC_HEADER_TIME
AC_CHECK_HEADERS([fcntl.h netinet/in.h stdint.h stdlib.h string.h sys/time.h unistd.h inttypes.h])
AC_CHECK_HEADER([libpq-fe.h], [], [AC_MSG_ERROR([PostgreSQL header files not found])], [/* dummy */])

# Checks for typedefs, structures, and compiler characteristics.
AC_SYS_LARGEFILE
AC_TYPE_SIZE_T
AC_TYPE_SSIZE_T
AC_TYPE_UINT64_T
AC_TYPE_UINT32_T
AC_CHECK_SIZEOF(unsigned int)
AC_CHECK_SIZEOF(unsigned long)
AC_CHECK_SIZEOF(unsigned short)
AC_C_BIGENDIAN

# Checks for library functions.
AC_FUNC_STRTOD
AC_CHECK_FUNCS([gettimeofday select setenv strchr strtoul])

AC_CONFIG_FILES([Makefile test/runtest.sh])
AC_OUTPUT
chmod u+x test/runtest.sh
