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

AC_INIT(stunnel.c)

AC_CANONICAL_HOST
AC_PROG_CC
if test "$GCC" = "yes"; then CFLAGS="$CFLAGS -Wall"; fi
AC_PROG_INSTALL
AC_PROG_MAKE_SET

VERSION="3.8"
AC_SUBST(VERSION)
AC_DEFINE_UNQUOTED(VERSION, "$VERSION")

dnl Check for user-specified random device
AC_ARG_WITH(random,
    [  --with-random=FILE      read randomness from FILE (default=/dev/urandom)],
    [ RANDOM_OPT="-rand $withval" ],
    [
        dnl Check for random device
        AC_CHECK_FILE("/dev/urandom",
            [
                RANDOM_OPT="-rand /dev/urandom";
                AC_SUBST(RANDOM_OPT)
            ]
        )
    ]
)

checkssldir() { :
    if test -d "$1/certs"; then
        if test -f "$1/include/openssl/ssl.h"; then
            AC_DEFINE(HAVE_OPENSSL)
            ssldir="$1"
            return 0
        fi
        if test -f "$1/include/ssl.h"; then
            ssldir="$1"
            return 0
        fi
    fi
    return 1
}

checkkrb5dir() { :
	if test -f "$1/include/krb5.h"; then
		AC_DEFINE(HAVE_KRB5)
		krb5dir="$1"
		return 0
	fi
    return 1
}

dnl Check for SSL directory
AC_MSG_CHECKING([for SSL directory])
AC_ARG_WITH(ssl,
    [  --with-ssl=DIR          location of installed SSL libraries/include files],
    [
        dnl Check the specified localtion only
        checkssldir "$withval"
    ],
    [
        dnl Search default localtions of SSL library
        for maindir in /usr/local /usr/lib /usr/pkg /usr; do
            for dir in $maindir $maindir/openssl $maindir/ssl; do
                checkssldir $dir && break 2
            done
        done
    ]
)
if test -z "$ssldir"; then
    AC_MSG_RESULT(Not found)
    echo
    echo "Couldn't find your SSL library installation dir"
    echo "Use --with-ssl option to fix this problem"
    echo
    exit 1
fi
AC_MSG_RESULT($ssldir)
AC_SUBST(ssldir)
AC_DEFINE_UNQUOTED(ssldir, "$ssldir")

dnl Check for KRB5 directory
AC_MSG_CHECKING([for KRB5 directory])
AC_ARG_WITH(krb5,
    [  --with-krb5=DIR         location of installed KRB5 libraries/include files],
    [
		if test "$withval" != "yes"; then
			dnl Check the specified localtion only
			checkkrb5dir "$withval"
		else
			dnl Search default localtions of KRB5 library
			for maindir in /usr/local /usr/lib /usr/pkg /opt; do
				for dir in $maindir $maindir/krb5 ; do
					checkkrb5dir $dir && break 2
				done
			done
		fi
    ],
    [
        dnl Otherwise, no KRB5 for you
		AC_DEFINE(NO_KRB5)
		echo
    ]
)
if test "$krb5dir" != ""; then
	AC_MSG_RESULT($krb5dir)
    AC_SUBST(krb5dir)
fi

AC_MSG_CHECKING([whether you want to include the idea cipher])
AC_ARG_WITH(idea,
[  --without-idea          disable the idea cipher],
	[use_idea=no ; CFLAGS="$CFLAGS -DNO_IDEA"],
	[use_idea=yes])
AC_MSG_RESULT($use_idea)

dnl Substitute host.
AC_SUBST(host)
AC_DEFINE_UNQUOTED(HOST, "$host")

dnl Checks for standard libraries.
AC_CHECK_LIB(nsl, gethostbyname)
AC_CHECK_LIB(socket, socket)
AC_CHECK_LIB(pthread, pthread_create)
AC_CHECK_LIB(c_r, pthread_create)
AC_CHECK_LIB(util, openpty)

dnl Check for libwrap library.
AC_MSG_CHECKING([for hosts_access in -lwrap])
saved_LIBS="$LIBS"
LIBS="-lwrap $saved_LIBS"
AC_TRY_LINK([int hosts_access(); int allow_severity, deny_severity;],
[hosts_access()],
[AC_MSG_RESULT(yes); AC_DEFINE(HAVE_LIBWRAP)],
[AC_MSG_RESULT(no)]; LIBS="$saved_LIBS")

dnl Add SSL includes and libraries
CFLAGS="$CFLAGS -I$ssldir/include"
LIBS="$LIBS -L$ssldir/lib -lssl -lcrypto"

if test "$krb5dir" != ""; then
dnl Add KRB5 includes and libraries
CFLAGS="$CFLAGS -I$krb5dir/include"
LIBS="$LIBS -L$krb5dir/lib -lgssapi_krb5 -lkrb5 -lcom_err"
LIBS="$LIBS $krb5dir/lib/libcrypto.a -lresolv"
fi

dnl Check for RSAref library.
AC_MSG_CHECKING([for RSAref library])
saved_LIBS="$LIBS"
LIBS="$saved_LIBS -lRSAglue -lrsaref"
AC_TRY_LINK([], [],
[AC_MSG_RESULT(yes); ],
[AC_MSG_RESULT(no)]; LIBS="$saved_LIBS")

dnl Check PTY device files.
AC_CHECK_FILE("/dev/ptmx", AC_DEFINE_UNQUOTED(HAVE_DEV_PTMX))
AC_CHECK_FILE("/dev/ptc", AC_DEFINE_UNQUOTED(HAVE_DEV_PTS_AND_PTC))

dnl Checks for header files.
# AC_HEADER_DIRENT
# AC_HEADER_STDC
# AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(getopt.h unistd.h sys/select.h tcpd.h pthread.h pty.h stropts.h)

# dnl Checks for typedefs, structures, and compiler characteristics.
# AC_C_CONST
# AC_TYPE_SIGNAL
# AC_TYPE_SIZE_T
# AC_TYPE_PID_T
# AC_HEADER_TIME

dnl Checks for library functions.
AC_CHECK_FUNCS(getopt snprintf vsnprintf openpty _getpty)

AC_OUTPUT(Makefile stunnel.8)

