#
# ngIRCd -- The Next Generation IRC Daemon
# Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# Please read the file COPYING, README and AUTHORS for more information.
#

define(VERSION_ID,esyscmd(git describe|sed -e 's/rel-//g'|sed -e 's/-/~/'|tr -d \\n))

# -- Initialisation --

AC_PREREQ(2.50)
AC_INIT(ngircd, VERSION_ID)
AC_CONFIG_SRCDIR(src/ngircd/ngircd.c)
AC_CANONICAL_TARGET
AM_INIT_AUTOMAKE(1.6)
AM_CONFIG_HEADER(src/config.h)

m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])

# -- Templates for config.h --

AH_TEMPLATE([DEBUG], [Define if debug-mode should be enabled])
AH_TEMPLATE([HAVE_socklen_t], [Define if socklen_t exists])
AH_TEMPLATE([SNIFFER], [Define if IRC sniffer should be enabled])
AH_TEMPLATE([STRICT_RFC], [Define if ngIRCd should behave strict RFC compliant])
AH_TEMPLATE([SYSLOG], [Define if syslog should be used for logging])
AH_TEMPLATE([ZLIB], [Define if zlib compression should be enabled])
AH_TEMPLATE([TCPWRAP], [Define if TCP wrappers should be used])
AH_TEMPLATE([IRCPLUS], [Define if IRC+ protocol should be used])
AH_TEMPLATE([WANT_IPV6], [Define if IPV6 protocol should be enabled])
AH_TEMPLATE([IDENTAUTH], [Define if the server should do IDENT requests])
AH_TEMPLATE([PAM], [Define if PAM should be used])
AH_TEMPLATE([HAVE_sockaddr_in_len], [Define if sockaddr_in.sin_len exists])

AH_TEMPLATE([TARGET_OS], [Target operating system name])
AH_TEMPLATE([TARGET_VENDOR], [Target system vendor])
AH_TEMPLATE([TARGET_CPU], [Target CPU name])

# -- C Compiler --

AC_PROG_CC
AC_PROG_CC_STDC
AC_C_PROTOTYPES

# -- Helper programs --

AC_PROG_AWK
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_RANLIB

# -- Compiler Features --

AM_C_PROTOTYPES
AC_C_CONST
AC_C_INLINE

# -- Hard coded system and compiler dependencies/features/options ... --

AC_DEFUN([GCC_STACK_PROTECT_CC],[
  ssp_cc=yes
  # we use -fstack-protector-all for the test to enfoce the use of the guard variable 
  AC_MSG_CHECKING([whether ${CC} accepts -fstack-protector])
  ssp_old_cflags="$CFLAGS"
  CFLAGS="$CFLAGS -fstack-protector-all"
  AC_TRY_LINK(,,, ssp_cc=no)
  echo $ssp_cc
  CFLAGS="$ssp_old_cflags"
  if test "X$ssp_cc" = "Xyes"; then
      CFLAGS="$CFLAGS -fstack-protector"
      AC_DEFINE([ENABLE_SSP_CC], 1, [Define if SSP C support is enabled.])
  fi
])

if test "$GCC" = "yes"; then
	# We are using the GNU C compiler. Good!
	CFLAGS="$CFLAGS -pipe -W -Wall -Wpointer-arith -Wstrict-prototypes"

	GCC_STACK_PROTECT_CC
fi

case "$target_os" in
	hpux*)
		# This is HP/UX, we need to define _XOPEN_SOURCE_EXTENDED
		# (tested with HP/UX 11.11)
		CFLAGS="$CFLAGS -D_XOPEN_SOURCE_EXTENDED"
		;;
esac

# Add additional CFLAGS, eventually specified on the command line:
test -n "$CFLAGS_ADD" && CFLAGS="$CFLAGS $CFLAGS_ADD"

CFLAGS="$CFLAGS -DSYSCONFDIR='\"\$(sysconfdir)\"'"

# -- Headers --

AC_HEADER_STDC
AC_HEADER_TIME
AC_HEADER_SYS_WAIT

AC_CHECK_HEADERS([ \
	ctype.h errno.h fcntl.h netdb.h netinet/in.h netinet/in_systm.h \
	stdlib.h string.h strings.h sys/socket.h sys/time.h unistd.h \
	],,AC_MSG_ERROR([required C header missing!]))

AC_CHECK_HEADERS([ \
	arpa/inet.h ctype.h malloc.h netinet/ip.h stdbool.h stddef.h varargs.h \
	],[],[],[[
	#ifdef HAVE_SYS_TYPES_H
		#include <sys/types.h>
	#endif
	#ifdef HAVE_SYS_SOCKET_H
		#include <sys/socket.h>
	#endif
	#ifdef HAVE_NETINET_IN_H
		#include <netinet/in.h>
	#endif
	]]
)

# -- Datatypes --

AC_MSG_CHECKING(whether socklen_t exists)
AC_TRY_COMPILE([
#include <sys/types.h>
#include <sys/socket.h>
	],[
	socklen_t a, b;
	a = 2; b = 4; a += b;
	],[
	AC_DEFINE(HAVE_socklen_t) AC_MSG_RESULT(yes)
	],[
	AC_MSG_RESULT(no)
])

AC_TYPE_SIGNAL
AC_TYPE_SIZE_T

AC_CHECK_MEMBER([struct sockaddr_in.sin_len], AC_DEFINE(HAVE_sockaddr_in_len),,
 [#include <arpa/inet.h>])

# -- Libraries --

# A/UX needs this.
AC_CHECK_LIB(UTIL,memmove)
# needed on solaris. GNU libc also has a libnsl, but we do not need it.
AC_SEARCH_LIBS(gethostbyname,nsl)
AC_CHECK_LIB(socket,bind)

# -- Functions --

AC_FUNC_FORK
AC_FUNC_STRFTIME

AC_CHECK_FUNCS([ \
	bind gethostbyaddr gethostbyname gethostname inet_ntoa \
	setsid setsockopt socket strcasecmp waitpid],,
	AC_MSG_ERROR([required function missing!]))

AC_CHECK_FUNCS([ \
	gai_strerror getaddrinfo getnameinfo inet_aton sigaction \
	sigprocmask snprintf vsnprintf strdup strlcpy strlcat strtok_r])

# -- Configuration options --

# use syslog?

x_syslog_on=no
AC_ARG_WITH(syslog,
	[  --without-syslog        disable syslog (autodetected by default)],
	[	if test "$withval" != "no"; then
			if test "$withval" != "yes"; then
				CFLAGS="-I$withval/include $CFLAGS"
				CPPFLAGS="-I$withval/include $CPPFLAGS"
				LDFLAGS="-L$withval/lib $LDFLAGS"
			fi
			AC_CHECK_LIB(be, syslog)
			AC_CHECK_FUNCS(syslog, x_syslog_on=yes,
				AC_MSG_ERROR([Can't enable syslog!])
			)
		fi
	],
	[
		AC_CHECK_LIB(be, syslog)
		AC_CHECK_FUNCS(syslog, x_syslog_on=yes)
	]
)
if test "$x_syslog_on" = "yes"; then
	AC_DEFINE(SYSLOG, 1)
	AC_CHECK_HEADERS(syslog.h,,AC_MSG_ERROR([required C header missing!]))
fi

# use zlib compression?

x_zlib_on=no
AC_ARG_WITH(zlib,
	[  --without-zlib          disable zlib compression (autodetected by default)],
	[	if test "$withval" != "no"; then
			if test "$withval" != "yes"; then
				CFLAGS="-I$withval/include $CFLAGS"
				CPPFLAGS="-I$withval/include $CPPFLAGS"
				LDFLAGS="-L$withval/lib $LDFLAGS"
			fi
			AC_CHECK_LIB(z, deflate)
			AC_CHECK_FUNCS(deflate, x_zlib_on=yes,
				AC_MSG_ERROR([Can't enable zlib!])
			)
		fi
	],
	[	AC_CHECK_LIB(z, deflate)
		AC_CHECK_FUNCS(deflate, x_zlib_on=yes)
	]
)
if test "$x_zlib_on" = "yes"; then
	AC_DEFINE(ZLIB, 1)
	AC_CHECK_HEADERS(zlib.h,,AC_MSG_ERROR([required C header missing!]))
fi

# detect which IO API to use:

x_io_backend=none

AC_ARG_WITH(select,
	[  --without-select        disable select IO support (autodetected by default)],
	[	if test "$withval" != "no"; then
			if test "$withval" != "yes"; then
				CFLAGS="-I$withval/include $CFLAGS"
				CPPFLAGS="-I$withval/include $CPPFLAGS"
				LDFLAGS="-L$withval/lib $LDFLAGS"
			fi
			AC_CHECK_FUNCS(select, x_io_select=yes,
				AC_MSG_ERROR([Can't enable select IO support!])
			)
		fi
	],
	[
		AC_CHECK_FUNCS(select, x_io_select=yes)
	]
)

AC_ARG_WITH(poll,
	[  --without-poll          disable poll support (autodetected by default)],
	[	if test "$withval" != "no"; then
			if test "$withval" != "yes"; then
				CFLAGS="-I$withval/include $CFLAGS"
				CPPFLAGS="-I$withval/include $CPPFLAGS"
				LDFLAGS="-L$withval/lib $LDFLAGS"
			fi
			AC_CHECK_FUNCS(poll, [
				AC_CHECK_HEADERS(poll.h,
					x_io_backend=poll\(\),
					AC_MSG_ERROR(
					     [Can't enable poll IO support!])
				)
			], [
				AC_MSG_ERROR([Can't enable poll IO support!])
			])
		fi
	],
	[
		AC_CHECK_FUNCS(poll, [
			AC_CHECK_HEADERS(poll.h, x_io_backend=poll\(\))
		])
	]
)

AC_ARG_WITH(devpoll,
	[  --without-devpoll       disable /dev/poll IO support (autodetected by default)],
	[	if test "$withval" != "no"; then
			if test "$withval" != "yes"; then
				CFLAGS="-I$withval/include $CFLAGS"
				CPPFLAGS="-I$withval/include $CPPFLAGS"
				LDFLAGS="-L$withval/lib $LDFLAGS"
			fi

				AC_CHECK_HEADERS(sys/devpoll.h,,AC_MSG_ERROR([required C header missing!]))
		fi
	],
	[
		AC_CHECK_HEADERS(sys/devpoll.h, x_io_backend=/dev/poll)
	]
)

AC_ARG_WITH(epoll,
	[  --without-epoll         disable epoll IO support (autodetected by default)],
	[	if test "$withval" != "no"; then
			if test "$withval" != "yes"; then
				CFLAGS="-I$withval/include $CFLAGS"
				CPPFLAGS="-I$withval/include $CPPFLAGS"
				LDFLAGS="-L$withval/lib $LDFLAGS"
			fi
			AC_CHECK_FUNCS(epoll_create, x_io_epoll=yes,
				AC_MSG_ERROR([Can't enable epoll IO support!])
			)
		fi
	],
	[
		AC_CHECK_FUNCS(epoll_create, x_io_epoll=yes)
	]
)

AC_ARG_WITH(kqueue,
	[  --without-kqueue        disable kqueue IO support (autodetected by default)],
	[	if test "$withval" != "no"; then
			if test "$withval" != "yes"; then
				CFLAGS="-I$withval/include $CFLAGS"
				CPPFLAGS="-I$withval/include $CPPFLAGS"
				LDFLAGS="-L$withval/lib $LDFLAGS"
			fi
			AC_CHECK_FUNCS(kqueue, x_io_backend=kqueue\(\),
				AC_MSG_ERROR([Can't enable kqueue IO support!])
			)
		fi
	],
	[
		AC_CHECK_FUNCS(kqueue, x_io_backend=kqueue\(\))
	]
)

if test "$x_io_epoll" = "yes" -a "$x_io_select" = "yes"; then
	# when epoll() and select() are available, we'll use both!
	x_io_backend="epoll(), select()"
else
	if test "$x_io_epoll" = "yes"; then
		# we prefere epoll() if it is available
		x_io_backend="epoll()"
	else
		if test "$x_io_select" = "yes" -a "$x_io_backend" = "none"; then
			# we'll use select, when available and no "better"
			# interface has been detected ...
			x_io_backend="select()"
		fi
	fi
fi

if test "$x_io_backend" = "none"; then
	AC_MSG_ERROR([No useable IO API activated/found!?])
fi

# use SSL?

AC_ARG_WITH(openssl,
	[  --with-openssl          enable SSL support using OpenSSL],
	[	if test "$withval" != "no"; then
			if test "$withval" != "yes"; then
				CFLAGS="-I$withval/include $CFLAGS"
				CPPFLAGS="-I$withval/include $CPPFLAGS"
				LDFLAGS="-L$withval/lib $LDFLAGS"
			fi
			AC_CHECK_LIB(crypto, BIO_s_mem)
			AC_CHECK_LIB(ssl, SSL_library_init)
			AC_CHECK_FUNCS(SSL_library_init, x_ssl_openssl=yes,
				AC_MSG_ERROR([Can't enable openssl])
			)
		fi
	]
)

AC_ARG_WITH(gnutls,
	[  --with-gnutls           enable SSL support using gnutls],
	[	if test "$withval" != "no"; then
			if test "$withval" != "yes"; then
				CFLAGS="-I$withval/include $CFLAGS"
				CPPFLAGS="-I$withval/include $CPPFLAGS"
				LDFLAGS="-L$withval/lib $LDFLAGS"
			fi
			AC_CHECK_LIB(gnutls, gnutls_global_init)
			AC_CHECK_FUNCS(gnutls_global_init, x_ssl_gnutls=yes,
				AC_MSG_ERROR([Can't enable gnutls])
			)
		fi
	]
)

x_ssl_lib="no"
if test "$x_ssl_gnutls" = "yes"; then
	if test "$x_ssl_openssl" = "yes";then
		AC_MSG_ERROR([Cannot enable both gnutls and openssl])
	fi
	x_ssl_lib=gnutls
fi
if test "$x_ssl_openssl" = "yes"; then
	x_ssl_lib=openssl
fi

# use TCP wrappers?

x_tcpwrap_on=no
AC_ARG_WITH(tcp-wrappers,
	[  --with-tcp-wrappers     enable TCP wrappers support],
	[	if test "$withval" != "no"; then
			if test "$withval" != "yes"; then
				CFLAGS="-I$withval/include $CFLAGS"
				CPPFLAGS="-I$withval/include $CPPFLAGS"
				LDFLAGS="-L$withval/lib $LDFLAGS"
			fi
			AC_MSG_CHECKING(for hosts_access)
			LIBS="-lwrap $LIBS"
			AC_TRY_LINK([
#include <tcpd.h>
int allow_severity = 0;
int deny_severity = 0;
				],[
				tcpd_warn("link test");
				],[
				AC_MSG_RESULT(yes)
				AC_DEFINE(TCPWRAP, 1)
				x_tcpwrap_on=yes
				],[
				AC_MSG_RESULT(no)
				AC_MSG_ERROR([Can't enable TCP wrappers!])
			])
		fi
	]
)

# do IDENT requests using libident?

x_identauth_on=no
AC_ARG_WITH(ident,
	[  --with-ident            enable "IDENT" ("AUTH") protocol support],
	[	if test "$withval" != "no"; then
			if test "$withval" != "yes"; then
				CFLAGS="-I$withval/include $CFLAGS"
				CPPFLAGS="-I$withval/include $CPPFLAGS"
				LDFLAGS="-L$withval/lib $LDFLAGS"
			fi
			AC_CHECK_LIB(ident, ident_id)
			AC_CHECK_FUNCS(ident_id, x_identauth_on=yes,
				AC_MSG_ERROR([Can't enable IDENT support!])
			)
		fi
	]
)
if test "$x_identauth_on" = "yes"; then
	AC_DEFINE(IDENTAUTH, 1)
	AC_CHECK_HEADERS(ident.h,,AC_MSG_ERROR([required C header missing!]))
fi

# compile in PAM support?

x_pam_on=no
AC_ARG_WITH(pam,
	[  --with-pam              enable user authentication using PAM],
	[	if test "$withval" != "no"; then
			if test "$withval" != "yes"; then
				CFLAGS="-I$withval/include $CFLAGS"
				CPPFLAGS="-I$withval/include $CPPFLAGS"
				LDFLAGS="-L$withval/lib $LDFLAGS"
			fi
			AC_CHECK_LIB(pam, pam_authenticate)
			AC_CHECK_FUNCS(pam_authenticate, x_pam_on=yes,
				AC_MSG_ERROR([Can't enable PAM support!])
			)
		fi
	]
)
if test "$x_pam_on" = "yes"; then
	AC_DEFINE(PAM, 1)
	AC_CHECK_HEADERS(security/pam_appl.h,pam_ok=yes)
	if test "$pam_ok" != "yes"; then
		AC_CHECK_HEADERS(pam/pam_appl.h,pam_ok=yes,
			AC_MSG_ERROR([required C header missing!]))
	fi
fi

# compile in IRC+ protocol support?

x_ircplus_on=yes
AC_ARG_ENABLE(ircplus,
	[  --disable-ircplus       disable IRC+ protocol],
	if test "$enableval" = "no"; then x_ircplus_on=no; fi
)
if test "$x_ircplus_on" = "yes"; then
	AC_DEFINE(IRCPLUS, 1)
fi

# enable support for IPv6?
x_ipv6_on=no
AC_ARG_ENABLE(ipv6,
	[  --enable-ipv6           enable IPv6 protocol support],
	if test "$enableval" = "yes"; then x_ipv6_on=yes; fi
)
if test "$x_ipv6_on" = "yes"; then
	# getaddrinfo() and getnameinfo() are optional when not compiling
	# with IPv6 support, but are required for IPv6 to work!
	AC_CHECK_FUNCS([ \
		getaddrinfo getnameinfo \
		],,AC_MSG_ERROR([required function missing for IPv6 support!]))
	AC_DEFINE(WANT_IPV6, 1)
fi

# compile in IRC "sniffer"?

x_sniffer_on=no; x_debug_on=no
AC_ARG_ENABLE(sniffer,
	[  --enable-sniffer        enable IRC traffic sniffer (enables debug mode)],
	if test "$enableval" = "yes"; then
		AC_DEFINE(SNIFFER, 1)
		x_sniffer_on=yes; x_debug_on=yes
	fi
)

# enable additional debugging code?

AC_ARG_ENABLE(debug,
	[  --enable-debug          show additional debug output],
	if test "$enableval" = "yes"; then x_debug_on=yes; fi
)
if test "$x_debug_on" = "yes"; then
	AC_DEFINE(DEBUG, 1)
	test "$GCC" = "yes" && CFLAGS="-pedantic $CFLAGS"
	AC_CHECK_FUNCS(mtrace)
fi

# enable "strict RFC rules"?

x_strict_rfc_on=no
AC_ARG_ENABLE(strict-rfc,
	[  --enable-strict-rfc     strict RFC conformance -- may break clients!],
	if test "$enableval" = "yes"; then
		AC_DEFINE(STRICT_RFC, 1)
		x_strict_rfc_on=yes
	fi
)

# -- Definitions --

AC_DEFINE_UNQUOTED(TARGET_CPU, "$target_cpu" )
AC_DEFINE_UNQUOTED(TARGET_VENDOR, "$target_vendor" )
AC_DEFINE_UNQUOTED(TARGET_OS, "$target_os" )

# Add additional CFLAGS, eventually specified on the command line, but after
# running this configure script. Useful for "-Werror" for example.
test -n "$CFLAGS_END" && CFLAGS="$CFLAGS $CFLAGS_END"

# -- Generate files --

AC_OUTPUT([ \
	Makefile \
	doc/Makefile \
	doc/src/Makefile \
	src/Makefile \
	src/portab/Makefile \
	src/ipaddr/Makefile \
	src/tool/Makefile \
	src/ngircd/Makefile \
	src/testsuite/Makefile \
	man/Makefile \
	contrib/Makefile \
	contrib/Anope/Makefile \
	contrib/Debian/Makefile \
	contrib/MacOSX/Makefile \
	contrib/MacOSX/ngIRCd.xcodeproj/Makefile \
	contrib/MacOSX/ngIRCd.pmdoc/Makefile \
])

type dpkg >/dev/null 2>&1
if test $? -eq 0; then
	# Generate debian/ link if the dpkg command exists
	# (read: if we are running on a debian compatible system)
	echo "creating Debian-specific links ..."
	test -f debian/rules || ln -s contrib/Debian debian
fi

# -- Result --

echo
echo "ngIRCd $PACKAGE_VERSION has been configured with the following options:"
echo

# Someone please show me a better way :)  [borrowed by OpenSSH]
B=`eval echo ${bindir}` ; B=`eval echo ${B}`
S=`eval echo ${sbindir}` ; S=`eval echo ${S}`
C=`eval echo ${sysconfdir}` ; C=`eval echo ${C}`
M=`eval echo ${mandir}` ; M=`eval echo ${M}`
D=`eval echo ${docdir}` ; D=`eval echo ${D}`

echo "             Target: ${target}"
test "$target" != "$host" && echo "               Host: ${host}"
echo "           Compiler: ${CC}"
test -n "$CFLAGS"	&& echo "     Compiler flags: ${CFLAGS}"
test -n "$CPPFLAGS"	&& echo " Preprocessor flags: ${CPPFLAGS}"
test -n "$LDFLAGS"	&& echo "       Linker flags: ${LDFLAGS}"
test -n "$LIBS"		&& echo "          Libraries: ${LIBS}"
echo
echo "    'ngircd' binary: $S"
echo " Configuration file: $C"
echo "       Manual pages: $M"
echo "      Documentation: $D"
echo

echo $ECHO_N "     Syslog support: $ECHO_C"
test "$x_syslog_on" = "yes" \
	&& echo $ECHO_N "yes   $ECHO_C" \
	|| echo $ECHO_N "no    $ECHO_C"
echo $ECHO_N "  Enable debug code: $ECHO_C"
test "$x_debug_on" = "yes" \
	&& echo "yes" \
	|| echo "no"

echo $ECHO_N "   zlib compression: $ECHO_C"
test "$x_zlib_on" = "yes" \
	&& echo $ECHO_N "yes   $ECHO_C" \
	|| echo $ECHO_N "no    $ECHO_C"
echo $ECHO_N "        IRC sniffer: $ECHO_C"
test "$x_sniffer_on" = "yes" \
	&& echo "yes" \
	|| echo "no"

echo $ECHO_N "   Use TCP Wrappers: $ECHO_C"
test "$x_tcpwrap_on" = "yes" \
	&& echo $ECHO_N "yes   $ECHO_C" \
	|| echo $ECHO_N "no    $ECHO_C"
echo $ECHO_N "    Strict RFC mode: $ECHO_C"
test "$x_strict_rfc_on" = "yes" \
	&& echo "yes" \
	|| echo "no"

echo $ECHO_N "      IDENT support: $ECHO_C"
test "$x_identauth_on" = "yes" \
	&& echo $ECHO_N "yes   $ECHO_C" \
	|| echo $ECHO_N "no    $ECHO_C"
echo $ECHO_N "      IRC+ protocol: $ECHO_C"
test "$x_ircplus_on" = "yes" \
	&& echo "yes" \
	|| echo "no"

echo $ECHO_N "      IPv6 protocol: $ECHO_C"
test "$x_ipv6_on" = "yes" \
	&& echo $ECHO_N "yes   $ECHO_C" \
	|| echo $ECHO_N "no    $ECHO_C"
echo $ECHO_N "        I/O backend: $ECHO_C"
	echo "\"$x_io_backend\""

echo $ECHO_N "        PAM support: $ECHO_C"
test "$x_pam_on" = "yes" \
	&& echo $ECHO_N "yes   $ECHO_C" \
	|| echo $ECHO_N "no    $ECHO_C"
echo $ECHO_N "        SSL support: $ECHO_C"
echo "$x_ssl_lib"

echo

# -eof-