AC_PREREQ([2.53])
AC_INIT(rlm_dbm.c)
AC_REVISION($Revision$)
AC_DEFUN(modname,[rlm_dbm])

if test x$with_[]modname != xno; then

	AC_PROG_CC
	AC_PROG_CPP

	dnl extra argument: --with-rlm-dbm-lib-dir
	rlm_dbm_lib_dir=
	AC_ARG_WITH(rlm-dbm-lib-dir,
	[  --with-rlm-dbm-lib-dir=DIR       Directory for DBM library files []],
	[ case "$withval" in
	    no)
		AC_MSG_ERROR(Need rlm-dbm-lib-dir)
		;;
	    yes)
		;;
	    *)
		rlm_dbm_lib_dir="$withval"
		;;
	  esac ]
	)

	dnl extra argument: --with-rlm-dbm-include-dir
	rlm_dbm_inc_dir=
	AC_ARG_WITH(rlm-dbm-include-dir,
	[  --with-rlm-dbm-include-dir=DIR   Directory for DBM include files []],
	[ case "$withval" in
	    no)
		AC_MSG_ERROR(Need rlm-dbm-include-dir)
		;;
	    yes)
		;;
	    *)
		rlm_dbm_include_dir="$withval"
		;;
	  esac ]
	)

	smart_try_dir=$rlm_dbm_include_dir
	FR_SMART_CHECK_INCLUDE(ndbm.h)
	if test "x$ac_cv_header_ndbm_h" = "xyes"; then
          SMART_CFLAGS="${SMART_CFLAGS} -DHAVE_NDBM_H"
	else
		FR_SMART_CHECK_INCLUDE(gdbm/ndbm.h)
		if test "x$ac_cv_header_gdbm_ndbm_h" = "xyes"; then
			SMART_CFLAGS="${SMART_CFLAGS} -DHAVE_GDBM_NDBM_H"
		else
			ac_cv_header_gdbm_ndbm_h="bad autoconf assumption"
			FR_SMART_CHECK_INCLUDE(gdbm-ndbm.h)
			if test "x$ac_cv_header_gdbmmndbm_h" = "xyes"; then
				SMART_CFLAGS="${SMART_CFLAGS} -DHAVE_GDBMNDBM_H"
			else
				fail="$fail (ndbm.h or gdbm/ndbm.h or gdbm-ndbm.h)"
			fi
		fi
	fi
	AC_MSG_CHECKING([for dbm in default lib])
	AC_TRY_LINK([extern void* dbm_open();],dbm_open(),[ac_cv_lib_default_dbm_open=yes
		AC_MSG_RESULT(yes)],[AC_MSG_RESULT(no)])
	if test "x$ac_cv_lib_default_dbm_open" != "xyes"; then
		smart_try_dir=$rlm_dbm_lib_dir
		FR_SMART_CHECK_LIB(ndbm, dbm_open)
		if test "x$ac_cv_lib_ndbm_dbm_open" != "xyes"; then
			dnl GNU DBM < 1.8.1
			FR_SMART_CHECK_LIB(gdbm, dbm_open)
			if test "x$ac_cv_lib_gdbm_dbm_open" != "xyes"; then
				dnl GNU DBM >= 1.8.1
				FR_SMART_CHECK_LIB(gdbm_compat, dbm_open)
				if test "x$ac_cv_lib_gdbm_compat_dbm_open" != "xyes"; then
					fail="$fail (libndbm or libgdbm or libgdbm_compat)"
				fi
			fi
		fi
	fi

	targetname=modname
else
	targetname=
	echo \*\*\* module modname is disabled.
fi

if test x"$fail" != x""; then
	AC_MSG_WARN([FAILURE: ]modname[ requires: $fail.]);
	if test x"${enable_strict_dependencies}" = x"yes"; then
		AC_MSG_ERROR([set --without-]modname[ to disable it explicitly.])
	else
		AC_MSG_WARN([silently not building ]modname[.])
		targetname=""
	fi
fi

if test "x$targetname" != "x"; then
	dbm_utils="rlm_dbm_parser rlm_dbm_cat"
	dbm_install="rlm_dbm_install"
fi

dbm_ldflags=$SMART_LIBS
dbm_cflags=$SMART_CFLAGS
AC_SUBST(dbm_ldflags)
AC_SUBST(dbm_cflags)
AC_SUBST(targetname)
AC_SUBST(dbm_utils)
AC_SUBST(dbm_install)
AC_OUTPUT(Makefile)
