dnl Process this file with autoconf to produce a configure script.
AC_INIT(src/pconf_detect.c)
AM_INIT_AUTOMAKE(libprinterconf, 0.5)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CPP
AC_PROG_CXX
AC_PROG_INSTALL
AC_PROG_LIBTOOL
AC_PROG_LN_S

AC_CONFIG_MACRO_DIR([m4])

AM_PROG_LIBTOOL
AM_CONFIG_HEADER(config.h)


WARNS=""
AC_ARG_ENABLE(debug, 
  [  --enable-debug turn on debugging (assumes gcc/gdb) [default=no]],
WARNS="-W -Wall -Wundef -Wpointer-arith -Wbad-function-cast -Wcast-align -Wwrite-strings -Waggregate-return -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -Winline -Wshadow -O0 -ggdb3")
AC_SUBST(WARNS)

AC_ARG_ENABLE(local-snmpkit, 
  [ --enable-local-snmpkit specify the local paths to snmpkit],
  [ SNMPKIT_LIBS="${enableval}/libsnmpkit.la"
    SNMPKIT_INCLUDES="-I${enableval}"
  ],[
    SNMPKIT_LIBS="-lsnmpkit"
  ]
)
AC_SUBST(SNMPKIT_LIBS)
AC_SUBST(SNMPKIT_INCLUDES)

AC_ARG_WITH(glib,          [  --with-glib=1,2      use GLIB1 or 2 ])

if ! test "x$with_glib" = "x1" -o   "x$with_glib" = "x2" ; then
  AC_ERROR([--with-glib=$with_glib  is wrong; provide argument, use  =1 or =2])
fi

if test "x$with_glib" = "x2"   ; then
  pkg_modules="glib-2.0"
else
  pkg_modules="glib >= 1.2.0"
fi

PKG_CHECK_MODULES(GLIB, [$pkg_modules], :  ,
   [  AC_ERROR([ glib is not available ])
   exit 1 ])

AC_SUBST(GLIB_CFLAGS)
AC_SUBST(GLIB_LIBS)


AC_LANG_CPLUSPLUS

AC_ARG_ENABLE(local-printsys, 
  [ --enable-local-printsys specify the local paths to printsys],
  [ PRINTSYS_LIBS="${enableval}/libprintsys.la"
    PRINTSYS_INCLUDES="-I${enableval}"
  ],[
    PRINTSYS_LIBS="-lprintsys"
    PRINTSYS_INCLUDES=""
  ]
)
AC_SUBST(PRINTSYS_LIBS)
AC_SUBST(PRINTSYS_INCLUDES)

AC_CHECK_FUNC(pthread_join,
    AC_MSG_RESULT(using libc's pthread_join),
    AC_CHECK_LIB(pthread, pthread_join))

PKG_CHECK_MODULES(TDB, "tdb", :  ,
   [ AC_CHECK_LIB(tdb, tdb_store, TDB_LIBS="-ltdb", AC_MSG_ERROR(You must have tdb ))])
AC_SUBST(TDB_CFLAGS)
AC_SUBST(TDB_LIBS)

if test -z "${PRINTSYS_LIBS}"; then
   AC_CHECK_LIB(printsys,lps_init,[:],
	AC_MSG_ERROR(must have libprintsys to compile), ${GLIB_LIBS} ${TDB_LIBS})
fi

# if the --enable-local-snmpkit was passed in assume that the 
# the person who is building this knows what they are doing and
# skip the test to make sure that the header file and lib is 
# actually installed.
if test -z "${SNMPKIT_LIBS}"; then
  AC_CHECK_HEADER(snmpkit,,[
    AC_MSG_ERROR(Must have snmpkit headers to compile libprinterconf)])
  LIBS="$LIBS -lsnmpkit"
  AC_TRY_LINK([#include <snmpkit>],[set_snmpsock_props(30, 5, 0)],
        [ : ],
        [AC_MSG_ERROR(Must have snmpkit library to compile libprinterconf)])
fi

dnl loads in the magic pixie dust for the m4 files
TESTSCRIPTS=`cd src/tests && echo *.m4 | sed "s/m4/sh/g"`
AC_SUBST(TESTSCRIPTS)

AC_OUTPUT(Makefile src/Makefile src/tests/Makefile doc/Makefile printerconf.spec)

