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

AC_PREREQ(2.52)
AC_INIT(libircclient, 1.3, gyunaev@ulduzsoft.com)
AC_CONFIG_SRCDIR([include/libircclient.h])
AC_CONFIG_HEADER([src/config.h])

# Check for command-line
AC_ARG_ENABLE([debug],
            [AS_HELP_STRING([--enable-debug],
               [compile with debug information (no)])],
            [use_debug=$enableval],
            [use_debug=no])

AC_ARG_ENABLE([shared],
            [AS_HELP_STRING([--enable-shared],
               [build a shared library (no)])],
            [build_shared=$enableval],
            [build_shared=no])

AC_ARG_ENABLE([threads],
            [AS_HELP_STRING([--enable-threads],
               [compile with multithread support (yes)])],
            [use_threads=$enableval],
            [use_threads=yes])

AC_ARG_ENABLE([ipv6],
            [AS_HELP_STRING([--enable-ipv6],
               [compile with IPv6 support (no)])],
            [use_ipv6=$enableval],
            [use_ipv6=no])

AC_ARG_ENABLE([openssl],
            [AS_HELP_STRING([--enable-openssl],
               [compile with OpenSSL support (no)])],
            [use_openssl=$enableval],
            [use_openssl=no])

# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
AC_CHECK_TOOL(AR, ar, :)
AC_PROG_RANLIB

# Checks for header files.
AC_HEADER_STDC

# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
AC_C_CONST
AC_TYPE_SIZE_T
AC_HEADER_TIME

# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_SELECT_ARGTYPES
AC_FUNC_STAT
AC_CHECK_FUNCS([localtime_r socket])

AC_CHECK_LIB(socket, socket, AC_DEFINE(HAVE_SOCKET) LIBS="$LIBS -lsocket")

if test "$use_debug" = "yes"; then
	CFLAGS="${CFLAGS} -g -DENABLE_DEBUG"
else
	CFLAGS="${CFLAGS} -O3"
fi

# Choosing the target
TARGET="static"
if test "$build_shared" = "yes"; then
	CFLAGS="$CFLAGS -fpic"
	TARGET="shared"
fi

if test "$use_ipv6" = "yes"; then
	AC_CHECK_FUNCS([getaddrinfo inet_pton])
	AC_CHECK_LIB(nsl, getaddrinfo, AC_DEFINE(HAVE_GETADDRINFO) LIBS="$LIBS -lnsl")
	CFLAGS="$CFLAGS -DENABLE_IPV6"
else
	AC_CHECK_FUNCS([gethostbyname_r inet_ntoa])
	AC_CHECK_LIB(nsl, inet_ntoa, AC_DEFINE(HAVE_INET_NTOA) LIBS="$LIBS -lnsl")
fi

if test "$use_threads" = "yes"; then
	CFLAGS="$CFLAGS -DENABLE_THREADS -D_REENTRANT"
fi


# Checking for mingw
AC_MSG_CHECKING([For MinGW32])
case "$host" in
	*-*-mingw*)
		AC_MSG_RESULT([yes])
		CFLAGS="$CFLAGS -DWIN32_DLL"
		LIBS="$LIBS -lz -lwsock32 -lgdi32 -lkernel32" # for openssl
		TARGET="shared_mingw"
    		;;
	*) AC_MSG_RESULT([no]) ;;
esac

if test "$use_openssl" = "yes"; then
	AC_CHECK_LIB(crypto, [CRYPTO_new_ex_data], [], [AC_MSG_ERROR([OpenSSL not found])])
	AC_CHECK_LIB(ssl,    [SSL_library_init], [], [AC_MSG_ERROR([OpenSSL not found])])
	AC_CHECK_HEADER([openssl/ssl.h], [], [AC_MSG_ERROR([OpenSSL headers not found; did you install the -dev package?])])
	CFLAGS="$CFLAGS -DENABLE_SSL"
fi

AC_SUBST(TARGET)
AC_SUBST(CFLAGS)
AC_SUBST(LDFLAGS)
AC_SUBST(LIBS)
AC_SUBST(PREFIX)
AC_CONFIG_FILES([examples/Makefile src/Makefile])
AC_OUTPUT
