From: Andreas Henriksson <andreas@fatal.se>
Date: Sat, 20 Jul 2013 18:17:14 +0200
Subject: Add configure option for pgsql support

Add configure option --enable-pgsql to manually be able to select
instead of automagically choosing at build-time, to be able to build
both the static- and postgresql-version of bandwidthd
---
 configure.in | 36 ++++++++++++++++++++++++++++++++----
 1 file changed, 32 insertions(+), 4 deletions(-)

diff --git a/configure.in b/configure.in
index 6d9eea5..adbe427 100644
--- a/configure.in
+++ b/configure.in
@@ -55,10 +55,35 @@ AC_CHECK_LIB(pcap, pcap_open_live, ,
 	[AC_CHECK_LIB(wpcap, pcap_open_live, ,[AC_MSG_ERROR([Bandwidthd requires but cannot find libpcap])])])
 
 # Optional Library
-AC_CHECK_FILE(/usr/local/pgsql/lib, LDFLAGS="$LDFLAGS -L/usr/local/pgsql/lib")
-AC_CHECK_FILE(/usr/local/pgsql/include, CPPFLAGS="$CPPFLAGS -I/usr/local/pgsql/include")
-AC_CHECK_LIB(pq, PQconnectdb,
-	[AC_CHECK_LIB(pq,PQexecParams, ,AC_MSG_WARN([libpq exists but is too old... bandwidthd requires support for PQexecParams]))])
+AC_PATH_PROG(PG_CONFIG, pg_config)
+
+use_pgsql=no
+
+if test x$enable_pgsql = xyes ; then
+  if test "x$PG_CONFIG" = "x" ; then
+    echo "*** Couldn't find pg_config. Disabling PostgreSQL support."
+  else
+    LDFLAGS="$LDFLAGS -L`$PG_CONFIG --libdir` -lpq"
+
+    pgsql_cflags=`$PG_CONFIG --includedir`
+    if test "x$pgsql_cflags" != "x/usr/include" ; then
+      CPPFLAGS="$CPPFLAGS -I$pgsql_cflags"
+    fi
+     
+    use_pgsql=yes
+    AC_DEFINE(HAVE_LIBPQ, 1, "Enable PostgreSQL compiletime option")
+  fi
+fi
+
+#AM_CONDITIONAL(HAVE_PGSQL, test x$use_pgsql != xno)
+
+#AC_SUBST(PG_LIBS)
+#AC_SUBST(PG_CFLAGS)
+
+#AC_CHECK_FILE(/usr/local/pgsql/lib, LDFLAGS="$LDFLAGS -L/usr/local/pgsql/lib")
+#AC_CHECK_FILE(/usr/local/pgsql/include, CPPFLAGS="$CPPFLAGS -I/usr/local/pgsql/include")
+#AC_CHECK_LIB(pq, PQconnectdb,
+#	[AC_CHECK_LIB(pq,PQexecParams, ,AC_MSG_WARN([libpq exists but is too old... bandwidthd requires support for PQexecParams]))])
 
 # Checks for header files.
 AC_HEADER_DIRENT
@@ -133,4 +158,7 @@ AC_DEFINE_DIR(CONFIG_FILE, config_file, [Name of bandwidthd.conf])
 AC_DEFINE_DIR(LOG_DIR, log_dir, [Name of log directory])
 AC_DEFINE_DIR(HTDOCS_DIR, htdocs_dir, [Name of htdocs directory])
 AC_DEFINE_DIR(EXTENSION_DIR, extension_dir, [Name of the extensions directory])
+AC_ARG_ENABLE([pgsql], AC_HELP_STRING([--enable-pgsql],
+	[Enable PostgreSQL support. Default is yes, if available.]),
+	enable_pgsql="$enableval", enable_pgsql=yes)
 AC_OUTPUT(Makefile)
