dnl configure.in for Amavisd-new helper-progs
dnl based on MIMEDefang's configure.in
dnl Process this file with autoconf to produce a configure script.
dnl use version 2.13 (configure fails detecting libmilter if running 2.5x)
dnl start
AC_INIT


AC_PROG_CC                      dnl Find C compiler
AC_CHECK_PROGS(AR, ar, no)	dnl Check ar exists
AC_CHECK_PROGS(AWK, awk, no)	dnl Check awk exists
AC_CONFIG_HEADER(config.h)	dnl Generate config.h

dnl Add some common directories to PATH in case they are not there
PATH=$PATH:/usr/local/bin

AC_PATH_PROG(id,id,/usr/bin/id,/usr/local/bin:/bin:/usr/bin:/usr/xpg4/bin:$PATH)

AC_PROG_INSTALL


AC_PATH_PROG(NM, nm)

dnl Allow user to tell us where Sendmail is (or will be)
SENDMAILPROG=no
AC_ARG_WITH(sendmail,
[  --with-sendmail=PATH    specify location of Sendmail binary],
   SENDMAILPROG=$with_sendmail, SENDMAILPROG=no)


dnl Base directory for runtime files (log file, socket, temp dirs)
runtime_dir=/var/amavis
AC_MSG_CHECKING([for runtime directory])
AC_ARG_WITH(runtime-dir,
	      [  --with-runtime-dir=DIR        directory for runtime files [/var/amavis]],
	         runtime_dir=$withval)
AC_MSG_RESULT([$runtime_dir])
AC_DEFINE_UNQUOTED(RUNTIME_DIR, "$runtime_dir", [ Where all temporary files and directories are created. ])
AC_SUBST(runtime_dir)

dnl Path to socket for d/c communication
AC_MSG_CHECKING([socket name])
AC_ARG_WITH(sockname,
	      [  --with-sockname=PATH          path to socket for daemon/client communication
	                                            [/var/amavis/amavisd.sock]],
						      sockname=$withval, sockname=$runtime_dir/amavisd.sock)

AC_MSG_RESULT([$sockname])
AC_DEFINE_UNQUOTED(AMAVISD_SOCKET, "$sockname", [ The full path to the socket used for communication between client and daemon. ])
AC_SUBST(sockname)


dnl Amavis user for file ownership
AMAVISUSER=""
AC_ARG_WITH(user,
[  --with-user=LOGIN       use LOGIN as the AMAVISD user],
   AMAVISUSER=$with_user, AMAVISUSER=amavisd)

AC_SUBST(AMAVISUSER)

dnl Amavis-milter header/value 

x_header_tag="X-Virus-Scanned"
AC_MSG_CHECKING([for header_tag])
AC_ARG_WITH(x-header-tag,
[  --with-x-header-tag=X_HEADER_TAG       use X_HEADER_TAG as the AMAVISD header tag],
   x_header_tag=$withval)
AC_MSG_RESULT([$x_header_tag])
AC_DEFINE_UNQUOTED(X_HEADER_TAG, "$x_header_tag", [ tag for messages going through amavis-milter ])
AC_SUBST(x_header_tag)


x_header_val="by amavisd-new"
AC_MSG_CHECKING([for header_value])
AC_ARG_WITH(x-header-val,
[  --with-x-header-val=X_HEADER_VAL       use X_HEADER_VAL as the AMAVISD header value],
   x_header_val=$withval)
AC_MSG_RESULT([$x_header_val])
AC_DEFINE_UNQUOTED(X_HEADER_LINE, "$x_header_val", [ header value for messages going through amavis-milter ])
AC_SUBST(x_header_val)

dnl Allow user to tell us where milter includes are
MILTERINC=
AC_ARG_WITH(milterinc,
[  --with-milterinc=PATH   specify alternative location of milter includes],
   MILTERINC=$with_milterinc, MILTERINC=)

dnl Allow user to tell us where milter libraries are
MILTERLIB=
AC_ARG_WITH(milterlib,
[  --with-milterlib=PATH   specify alternative location of milter libraries],
   MILTERLIB=$with_milterlib, MILTERLIB=)



dnl Check if compiler allows "-pthread" option, but only if
dnl we are using GCC

if test "$GCC" = yes ; then
   AC_MSG_CHECKING([whether ${CC-cc} accepts -pthread])
   echo 'void f(){}' > conftest.c
   if test -z "`${CC-cc} -pthread -c conftest.c 2>&1`"; then
	   ac_cv_prog_cc_pthread=yes
	   PTHREAD_FLAG="-pthread"
   else
	PTHREAD_FLAG=""
	ac_cv_prog_cc_pthread=no
   fi
   AC_MSG_RESULT($ac_cv_prog_cc_pthread)
   rm -f conftest*
else
   PTHREAD_FLAG=""
fi

AC_CHECK_LIB(resolv, res_init)
AC_CHECK_LIB(socket, htons)
AC_CHECK_LIB(nsl, gethostbyname)
LIBS_WITHOUT_PTHREAD="$LIBS"
AC_SUBST(LIBS_WITHOUT_PTHREAD)
AC_CHECK_LIB(pthread, pthread_once)

AC_CHECK_FUNCS(snprintf mkdtemp mktemp)

dnl Find sendmail include file.  The nasty path is in case you're building
dnl Sendmail at the same level as MIMEDefang... we want to use that include
dnl file...
SMMILTER=`echo ../sendmail-*/include`
AC_PATH_PROG(MINCLUDE, libmilter/mfapi.h, no, $MILTERINC:$SMMILTER:/usr/include:/usr/local/include:/usr/local/include/sendmail:/opt/local/include)

dnl debugging
AC_ARG_ENABLE(debugging, [  --enable-debugging      Add debugging messages to syslog], ac_cv_debugging=$enableval, ac_cv_debugging=no)

if test "$ac_cv_debugging" = yes ; then
    ENABLE_DEBUGGING=-DENABLE_DEBUGGING
else
    ENABLE_DEBUGGING=
fi

dnl find libmilter.a and libsm.a
SMPATH=`echo ../sendmail-*/obj.*/libmilter`
AC_PATH_PROG(LIBMILTER, libmilter.a, no, $MILTERLIB:$SMPATH:/usr/local/lib:/lib:/usr/lib:/usr/lib/libmilter)
SMPATH=`echo ../sendmail-*/obj.*/libsm`
AC_PATH_PROG(LIBSM, libsm.a, no, $SMPATH:/usr/local/lib:/lib:/usr/lib:/usr/lib/libmilter)
if test "$LIBSM" = "no" ; then
	# try libsmutil
	unset ac_cv_path_LIBSM
	SMPATH=`echo ../sendmail-*/obj.*/libsmutil`
	AC_PATH_PROG(LIBSM, libsmutil.a, no, $SMPATH:/usr/local/lib:/lib:/usr/lib:/usr/lib/libmilter)
fi

dnl find Sendmail
if test "$SENDMAILPROG" = "no" ; then
   AC_PATH_PROG(SENDMAILPROG, sendmail, no, $PATH:/sbin:/usr/sbin:/usr/lib:/usr/libexec)
fi

dnl rm
AC_PATH_PROG(RM, rm, no, $PATH)

AC_DEFUN(MD_MILTER_SFIO,[
    AC_MSG_CHECKING([whether libmilter requires -lsfio])
    RESULT=`$NM $LIBMILTER | grep sfsprintf`
    if test -z "$RESULT" ; then
	AC_MSG_RESULT(no)
    else
	AC_MSG_RESULT(yes)
	LIBS="$LIBS -lsfio"
    fi
])

AC_DEFUN(MD_SM_LDAP,[
    AC_MSG_CHECKING([whether libsm requires -lldap])
    RESULT=`$NM $LIBSM | grep ldap_`
    if test -z "$RESULT" ; then
	AC_MSG_RESULT(no)
    else
	AC_MSG_RESULT(yes)
	LIBS="$LIBS -lldap -llber"
    fi
])

AC_DEFUN(MD_MILTER_SM,[
    AC_MSG_CHECKING([whether libmilter requires -lsm])
    RESULT=`$NM $LIBMILTER | grep sm_strlcpy`
    if test -z "$RESULT" ; then
	AC_MSG_RESULT(no)
	LIBSM=""
    else
	AC_MSG_RESULT(yes)
	LIBS="$LIBS"
	if test "$LIBSM" = "no" ; then
	    AC_MSG_WARN([Oops.. I couldn't find libsm.a.  Please install Sendmail 8.12])
	    AC_MSG_WARN([and its libraries.])
	    PROBLEM=1
	else
	    MD_SM_LDAP
	fi
    fi
])


dnl Sanity checks

if test "$LIBMILTER" = "no" ; then
    AC_MSG_WARN([Oops.. I couldn't find libmilter.a.  Please install Sendmail 8.12])
    AC_MSG_WARN([and its libraries.  You must run Build in the libmilter/ directory])
    AC_MSG_WARN([to compiler libmilter.])
    PROBLEM=1
fi

if test "$MINCLUDE" = "no" ; then
    AC_MSG_WARN([Oops.. I couldn't find libmilter/mfapi.h.  Please install Sendmail 8.12])
    AC_MSG_WARN([and its header files.])
    PROBLEM=1
fi

if test "$SENDMAILPROG" = "no" ; then
    AC_MSG_WARN([Oops.. I couldn't find the 'sendmail' program.  Please install it.])
    PROBLEM=1
fi

if test "$PROBLEM" = 1 ; then
    exit 1
fi

dnl GCC warning level
if test "$GCC" = yes; then
    if test "`uname -s`" = Linux; then
	    dnl CFLAGS="$CFLAGS -Wall -Wstrict-prototypes"
	    CFLAGS="$CFLAGS -Wall"
    fi
fi

dnl Fix up the include stuff
MINCLUDE=`dirname $MINCLUDE`
MINCLUDE=`dirname $MINCLUDE`

dnl If MINCLUDE is "/usr/include", do NOT add to include path, because
dnl this messes up compilation with gcc on Solaris.
if test "$MINCLUDE" = "/usr/include" ; then
   MINCLUDE=""
else
   MINCLUDE="-I${MINCLUDE}"
fi

AC_SUBST(ENABLE_DEBUGGING)
AC_SUBST(PTHREAD_FLAG)
VERSION=1.0
AC_SUBST(VERSION)

NEED_LIBSM=0
dnl Jigger for machines without snprintf
if test "$ac_cv_func_snprintf" != "yes" ; then
   AC_MSG_RESULT([Your OS lacks snprintf or vsnprintf, but we'll use the])
   AC_MSG_RESULT([equivalents in $LIBSM])
   NEED_LIBSM=1
fi

dnl Check if our libmilter is old...
if test "$AR" != "no" ; then
   AC_MSG_CHECKING(whether libmilter must be linked with libsm)
   $AR -t $LIBMILTER | fgrep strl.o > /dev/null 2>&1
   if test "$?" = 0 ; then
      AC_MSG_RESULT(no)
   else
      AC_MSG_RESULT(yes)
      NEED_LIBSM=1
   fi
fi

if test "$NEED_LIBSM" = 1 ; then
   if test "$LIBSM" = "no" ; then
      AC_MSG_ERROR([Problem: we need to link with libsm.a, but I can't find it.])
      exit 1
   fi
   LIBS="$LIBSM $LIBS"
fi


AC_OUTPUT(Makefile)



if test "$SENDMAILPROG" != "no" ; then
        sendmailversion=`$SENDMAILPROG -bv -d0.1 | $AWK '/Version/ {print $2}'`
        echo ""
        echo "Info: sendmail version $sendmailversion detected"
        echo ""
fi
