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

AC_INIT(src/base64.c)

AC_CANONICAL_HOST

AC_PROG_CC
AC_PROG_CPP
AC_PROG_RANLIB

AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(sys/time.h)
if test "$ac_cv_header_sys_time_h" = "yes"; then
	AC_HEADER_TIME
fi
AC_C_CONST
AC_TYPE_SIZE_T

AC_CHECK_HEADERS(stropts.h sys/filio.h)
AC_CHECK_HEADERS(stdint.h inttypes.h)

###############################################################################
# Check for standard size types.  The defaults are only valid on some
# systems so we hope that <inttypes.h> exists when they're wrong.
AC_CHECK_TYPE(int8_t,  signed char)
AC_CHECK_TYPE(int16_t, short)
AC_CHECK_TYPE(int32_t, long)
AC_CHECK_TYPE(int64_t, long long)

# Some systems have these in <stdint.h>, just to be difficult...
AC_CACHE_CHECK(for uint8_t in <stdint.h>, ucl_cv_uint8_t_in_stdint_h,
	AC_EGREP_HEADER(uint8_t,
			stdint.h,
			ucl_cv_uint8_t_in_stdint_h=yes,
			ucl_cv_uint8_t_in_stdint_h=no))
if test $ucl_cv_uint8_t_in_stdint_h = "no"
then
	 AC_CHECK_TYPE(uint8_t,  unsigned char)
fi

AC_CACHE_CHECK(for uint16_t in <stdint.h>, ucl_cv_uint16_t_in_stdint_h,
	AC_EGREP_HEADER(uint16_t,
			stdint.h,
			ucl_cv_uint16_t_in_stdint_h=yes,
			ucl_cv_uint16_t_in_stdint_h=no))
if test $ucl_cv_uint16_t_in_stdint_h = "no"
then
	 AC_CHECK_TYPE(uint16_t,  unsigned short)
fi

AC_CACHE_CHECK(for uint32_t in <stdint.h>, ucl_cv_uint32_t_in_stdint_h,
	AC_EGREP_HEADER(uint32_t,
			stdint.h,
			ucl_cv_uint32_t_in_stdint_h=yes,
			ucl_cv_uint32_t_in_stdint_h=no))
if test $ucl_cv_uint32_t_in_stdint_h = "no"
then
	 AC_CHECK_TYPE(uint32_t,  unsigned int)
fi

###############################################################################
# The following two macros cause autoconf to complain.
AC_C_BIGENDIAN
AC_C_CHAR_UNSIGNED
#
# Some of the codecs in rat don't with with unsigned characters. 
# Force the compiler to use signed chars, to be consistent.
if test $ac_cv_c_char_unsigned = yes
then
	if test "$GCC" = yes
	then
		CFLAGS="$CFLAGS -fsigned-char"
	else
		case "$host_os" in
		# I don't know when "-signed" was added to IRIX CC
		# so err on the side of using it.
		irix*)	CFLAGS="$CFLAGS -signed"
			;;
		# need e.g. --force-signed-chars=-signed
		*)	AC_MSG_ERROR([I don't know how to force signed chars])
			;;
		esac
	fi
fi
#  __CHAR_UNSIGNED__ will be defined; hope this is OK.

###############################################################################
# The following causes autoconf to complain.
AC_CHECK_FILE(/dev/urandom,AC_DEFINE(HAVE_DEV_URANDOM))

# If more files than mbus.c use vsnprintf, split it out into
# vsnprintf.c and add it to AC_REPLACE_FUNCS
# AC_CHECK_FUNC(vsnprintf,,AC_DEFINE(NEED_VSNPRINTF))
AC_REPLACE_FUNCS(vsnprintf)

###############################################################################
# If inet_aton is actually needed somewhere, split it out into
# inet_aton.c and add it to AC_REPLACE_FUNCS 
#
# AC_CHECK_FUNC succeeds on our IRIX 6.2 boxes, but it is not 
# declared anywhere, use egrep header to check (ugh lame, but works)

AC_CACHE_CHECK(for inet_pton in <arpa/inet.h>, ucl_cv_inet_pton_in_inet_h,
	AC_EGREP_HEADER(uint16_t,
			stdint.h,
			ucl_cv_inet_pton_in_inet_h=yes,
			ucl_cv_inet_pton_in_inet_h=no))
if test $ucl_cv_inet_pton_in_inet_h = "no"
then
	AC_DEFINE(NEED_INET_PTON)
	LIBOBJS="$LIBOBJS inet_pton.o"
fi
AC_CACHE_CHECK(for inet_ntop in <arpa/inet.h>, ucl_cv_inet_ntop_in_inet_h,
	AC_EGREP_HEADER(uint16_t,
			stdint.h,
			ucl_cv_inet_ntop_in_inet_h=yes,
			ucl_cv_inet_ntop_in_inet_h=no))
if test $ucl_cv_inet_ntop_in_inet_h = "no"
then
	AC_DEFINE(NEED_INET_NTOP)
	LIBOBJS="$LIBOBJS inet_ntop.o"
else
	AC_DEFINE(HAVE_INET_NTOP)
fi
AC_SUBST(LIBOBJS)

dnl AC_REPLACE_FUNCS(inet_pton inet_ntop)

AC_SEARCH_LIBS(socket, socket)
AC_SEARCH_LIBS(inet_addr, nsl)

###############################################################################
#  -profile
AC_ARG_ENABLE(profile,
	[  --enable-profile        enable profiling],
	[if test $enableval = yes
	then
		if test "$GCC" = yes
		then
			CFLAGS="$CFLAGS -pg"
		else
			AC_ERROR([Don't know how to enable profiling for $CC])
		fi
	fi])

#  -bounds
#       add -fbounds-checking to CFLAGS
#       add -lcheck to LIBS
AC_ARG_ENABLE(bounds,
	[  --enable-bounds         enable bounds checking],
	[if test $enableval = yes
	then
		if test "$GCC" = yes
		then
			CFLAGS="$CFLAGS -fbounds-checking"
			LIBS="$LIBS -lcheck"
		else
			AC_ERROR([Don't know how to enable profiling for $CC])
		fi
	fi])

# -DDEBUG
# -DDEBUG_MEM
# -DNDEBUG
AC_ARG_ENABLE(debug,
	[  --enable-debug          enable debug messages and code],
	[if test $enableval = yes
	then
		AC_DEFINE(DEBUG)
	fi])
AC_ARG_ENABLE(debug-mem,
	[  --enable-debug-mem      enable memory debugging code],
	[if test $enableval = yes
	then
		AC_DEFINE(DEBUG_MEM)
	fi])

###############################################################################
# IPv6 related configuration options (using tcpdump's configure.in)
AC_MSG_CHECKING([whether to enable IPv6])
AC_ARG_ENABLE(ipv6,
        [  --enable-ipv6           enable ipv6],
	[ case "$enableval" in
	  yes)	AC_MSG_RESULT(yes)
		AC_DEFINE(HAVE_IPv6)
		ipv6=yes
		;;
	  *)	AC_MSG_RESULT(no)
		ipv6=no
		;;
	  esac
	],
	[ AC_MSG_RESULT(no)
	  ipv6=no
	])
ipv6type=unknown
ipv6lib=none
ipv6trylibc=no

# IPv6 Stack Type Detector
if test "$ipv6" = "yes"; then
	AC_MSG_CHECKING([ipv6 stack type])
	for i in inria kame linux-glibc linux-libinet6 toshiba v6d zeta; do
		case $i in
		inria)
			dnl http://www.kame.net/
			AC_EGREP_CPP(yes, [dnl
#include <netinet/in.h>
#ifdef IPV6_INRIA_VERSION
yes
#endif],
				[ipv6type=$i;
				CFLAGS="-DINET6 $CFLAGS"])
			;;
		kame)
			dnl http://www.kame.net/
			AC_EGREP_CPP(yes, [dnl
#include <netinet/in.h>
#ifdef __KAME__
yes
#endif],
				[ipv6type=$i;
				ipv6lib=inet6;
				if test -x /usr/local/v6/lib; then
					ipv6libdir=/usr/local/v6/lib;
				else
					ipv6type=freebsd/netbsd/openbsd
					ipv6libdir=/usr/lib;
				fi;
				ipv6trylibc=yes;
				CFLAGS="-DINET6 $CFLAGS"])
			;;
		linux-glibc)
			dnl http://www.v6.linux.or.jp/
			AC_EGREP_CPP(yes, [dnl
#include <features.h>
#if defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1
yes
#endif],
				[ipv6type=$i;
				CFLAGS="-DINET6 $CFLAGS"])
			;;
		linux-libinet6)
			dnl http://www.v6.linux.or.jp/
			if test -d /usr/inet6 -o -f /usr/include/netinet/ip6.h; then
				ipv6type=$i
				ipv6lib=inet6
				ipv6libdir=/usr/inet6/lib
				ipv6trylibc=yes;
				CFLAGS="-DINET6 -I/usr/inet6/include $CFLAGS"
			fi
			;;
		toshiba)
			AC_EGREP_CPP(yes, [dnl
#include <sys/param.h>
#ifdef _TOSHIBA_INET6
yes
#endif],
				[ipv6type=$i;
				ipv6lib=inet6;
				ipv6libdir=/usr/local/v6/lib;
				CFLAGS="-DINET6 $CFLAGS"])
			;;
		v6d)
			AC_EGREP_CPP(yes, [dnl
#include </usr/local/v6/include/sys/v6config.h>
#ifdef __V6D__
yes
#endif],
				[ipv6type=$i;
				ipv6lib=v6;
				ipv6libdir=/usr/local/v6/lib;
				CFLAGS="-I/usr/local/v6/include $CFLAGS"])
			;;
		zeta)
			AC_EGREP_CPP(yes, [dnl
#include <sys/param.h>
#ifdef _ZETA_MINAMI_INET6
yes
#endif],
				[ipv6type=$i;
				ipv6lib=inet6;
				ipv6libdir=/usr/local/v6/lib;
				CFLAGS="-DINET6 $CFLAGS"])
			;;
		esac
		if test "$ipv6type" != "unknown"; then
			break
		fi
	done
	AC_MSG_RESULT($ipv6type)
fi

if test "$ipv6" = "yes" -a "$ipv6lib" != "none"; then
	if test -d $ipv6libdir -a -f $ipv6libdir/lib$ipv6lib.a; then
		LIBS="-L$ipv6libdir -l$ipv6lib $LIBS"
		echo "You have $ipv6lib library, using it"
	else
		if test "$ipv6trylibc" = "yes"; then
			echo "You do not have $ipv6lib library, using libc"
		else
			echo 'Fatal: no $ipv6lib library found.  cannot continue.'
			echo "You need to fetch lib$ipv6lib.a from appropriate"
			echo 'ipv6 kit and compile beforehand.'
			exit 1
		fi
	fi
fi

# Test below are IPv6 specific.  Their result has no bearing if HAVE_IPv6 is
# not defined.  They are kept outside IPv6 enable check to keep code readable.

AC_CACHE_CHECK(for getipnodebyname in <netdb.h>, ucl_cv_getipnodebyname_in_netdb_h,
	AC_EGREP_HEADER(getipnodebyname,
		netdb.h,
		ucl_cv_getipnodebyname_in_netdb_h=yes,	
		ucl_cv_getipnodebyname_in_netdb_h=no))
if test $ucl_cv_getipnodebyname_in_netdb_h
then
	AC_DEFINE(HAVE_GETIPNODEBYNAME) 
fi

AC_CACHE_CHECK(for struct addrinfo in <netdb.h>, ucl_cv_st_addrinfo_in_netdb_h,
	AC_EGREP_HEADER(addrinfo,
		netdb.h,
		ucl_cv_st_addrinfo_in_netdb_h=yes,	
		ucl_cv_st_addrinfo_in_netdb_h=no))
if test $ucl_cv_st_addrinfo_in_netdb_h
then
	AC_DEFINE(HAVE_ST_ADDRINFO) 
fi

AC_CACHE_CHECK(for sin6_len in struct sockaddr_in6, ucl_cv_sin6_len,
	[AC_TRY_COMPILE([
		#include <netinet/in.h>
	],[
		struct sockaddr_in6	s_in;
		s_in.sin6_len = 0;
	],
		ucl_cv_sin6_len=yes,	
		ucl_cv_sin6_len=no
	)])
if test $ucl_cv_sin6_len = yes
then
	AC_DEFINE(HAVE_SIN6_LEN) 
fi

AC_CACHE_CHECK(for msg_control in struct msghdr, ucl_cv_control,
        [AC_TRY_COMPILE([
                #include <sys/types.h>
		#include <sys/socket.h>
        ],[
                struct msghdr msg;
                msg.msg_control = 0;
        ],
                ucl_cv_control=yes,
                ucl_cv_control=no
        )])
if test $ucl_cv_control = yes
then
        AC_DEFINE(HAVE_MSGHDR_MSGCTRL)
fi

###############################################################################
# Check whether gtk-doc is installed

AC_CHECK_PROG(GTKDOC, gtkdoc-scan, yes, no, $PATH)
if test "$GTKDOC" = yes 
then
	OPTDOC=doc
else
	OPTDOC=
fi
AC_SUBST(OPTDOC)

###############################################################################
# GCC-specific warning flags
if test "$GCC" = yes
then
        CFLAGS="$CFLAGS -W -Wall -Wwrite-strings -Wbad-function-cast -Wmissing-prototypes -Wcast-qual -Wmissing-declarations"
fi

###############################################################################
# Done, create the output files....
AC_CONFIG_HEADER(src/uclconf.h:src/config.h.in)
AC_OUTPUT(Makefile doc/Makefile examples/Makefile examples/rtp/Makefile src/Makefile tests/Makefile)
