AC_INIT()

AC_CONFIG_HEADER(src/kik_config.h)
AC_CONFIG_AUX_DIR(script)

AC_CANONICAL_HOST
AC_CANONICAL_BUILD

# for CFLAGS="..." ./configure ...
AC_SUBST(CFLAGS)

AC_PROG_CC
#  if --with-libtool is specified, cpp isn't detected.
AC_PROG_CPP
AC_PROG_INSTALL

#detect glibc
AC_TRY_COMPILE([#include <features.h>],
[
#ifdef __GLIBC__
#else
#error
int boil\[-1\];
#endif
], AC_DEFINE(HAVE_GNU_SOURCE))

#
# --- libtool ---
#
AC_LIBTOOL_WIN32_DLL
AC_LIBTOOL_DLOPEN
AC_ARG_WITH(libtool,
	[  --with-libtool@<:@=ARG@:>@    libtool path @<:@default=without@:>@],
	libtool=$with_libtool)
if test "${libtool}" != "" ; then
	LIBTOOL=${libtool}
else
	AM_PROG_LIBTOOL
	LIBTOOL='${top_builddir}/libtool'
fi
AC_SUBST(LIBTOOL)

#
# --- Checks for header files ---
#
# NOTE: if --with-libtool is specified, stdc header isn't detected.
AC_HEADER_STDC
AC_CHECK_HEADERS(langinfo.h dlfcn.h dl.h stropts.h sys/stropts.h stdint.h windows.h errno.h)

#
# --- Checks for library functions ---
#
AC_CHECK_FUNCS(strsep fgetln basename isastream seteuid setegid geteuid getegid setsid snprintf usleep setenv unsetenv flock getuid getgid recvmsg setpgid socketpair killpg gettimeofday)
AC_FUNC_ALLOCA

AC_C_INLINE
AC_C_CONST
AC_C_BIGENDIAN

AC_CHECK_TYPE(u_char,unsigned char)
AC_CHECK_TYPE(u_short,unsigned short)
AC_CHECK_TYPE(u_int,unsigned int)
AC_CHECK_TYPE(u_long,unsigned long)
AC_CHECK_TYPE(u_int8_t,unsigned char)
AC_CHECK_TYPE(u_int16_t,unsigned short)
AC_CHECK_TYPE(u_int32_t,unsigned int)
AC_CHECK_TYPE(u_int64_t,unsigned long)
AC_CHECK_TYPE(int8_t,char)
AC_CHECK_TYPE(int16_t,short)
AC_CHECK_TYPE(int32_t,int)
AC_CHECK_TYPE(int64_t,long)
AC_CHECK_TYPE(ssize_t,int)

AC_TYPE_MODE_T
AC_TYPE_PID_T
AC_TYPE_UID_T
AC_TYPE_OFF_T
AC_TYPE_SIZE_T

#
# --- Platform dependent stuff ---
#
kik_cv_cygwin=no
kik_cv_mingw32=no
kik_cv_win32=no
case "${host_os}" in
cygwin*)
	kik_cv_cygwin=yes
	case "${CC} ${CFLAGS}" in
	*mno-cygwin* | *-mingw*)
		kik_cv_win32=yes
		AC_DEFINE(USE_WIN32API,,"USE_WIN32API")
		;;
	*)
		;;
	esac
	;;
mingw32*)
	kik_cv_mingw32=yes
	if test ! -f "/lib/libmsys-1.0.dll.a" ; then
		kik_cv_win32=yes
		AC_DEFINE(USE_WIN32API,,"USE_WIN32API")
	fi
	;;
*)
	;;
esac

#
# --- socklen_t ---
#
# NOTE: #define _BSDTYPES_DEFINED is necessary because AC_TRY_COMPILE defines
#       u_char, u_short and so on before #include <ws2tcpip.h> .
#
AC_CACHE_CHECK(for socklen_t, kik_cv_socklen_ident,
[AC_TRY_COMPILE(
[
#include  <stddef.h>
#include  <sys/types.h>
#include  <sys/socket.h>
],
[
socklen_t  len ;
], kik_cv_socklen_ident=yes,
[AC_TRY_COMPILE(
[
#define  _BSDTYPES_DEFINED
#include  <ws2tcpip.h>
],
[
socklen_t  len ;
], kik_cv_socklen_ident=yes, kik_cv_socklen_ident=no)])])
if test "${kik_cv_socklen_ident}" = "no" ; then
	AC_DEFINE(socklen_t, unsigned int, "socklen_t")
fi

#
# --- Check if concatenation of string literals with __FUNCTION__ is supported. ---
#
AC_CACHE_CHECK(for __FUNCTION__, kik_cv_func_ident,[
AC_TRY_COMPILE(,
[
char * p = "[" __FUNCTION__ "]" ;
] , kik_cv_func_ident=yes,kik_cv_func_ident=no)])
if test "${kik_cv_func_ident}" = "yes" ; then
	AC_DEFINE(CONCATABLE_FUNCTION,,"CONCATABLE_FUNCTION")
fi


#
# --- Check for libxpg4 (for FreeBSD) ---
#
AC_CHECK_FUNCS(setlocale)
if test "x$ac_cv_func_setlocale" = xno ; then
	AC_CHECK_LIB(xpg4, setlocale, XPG4_LIBS=-lxpg4)
	AC_SUBST(XPG4_LIBS)
fi

#
# --- Checks for dynamic linking loader ---
#
DL_LOADER=none
DL_LIBS=
DL_CFLAGS=
# lt_dlopenext in libltdl
AC_ARG_WITH(libltdl,
	[  --with-libltdl@<:@=PREFIX@:>@ load modules with libltdl @<:@default=without@:>@],,
	[with_libltdl=no])
if test "x$with_libltdl" != "xno" ; then
	if test "x$with_libltdl" != "xyes"; then
		DL_CFLAGS="-I$with_libltdl/include"
		kik_libltdl_libdir="-L$with_libltdl/lib"
	fi
	kik_ldflags_save="$LDFLAGS"
	LDFLAGS="$LDFLAGS $kik_libltdl_libdir"
	AC_CHECK_LIB(ltdl, lt_dlopenext,
			[
			DL_LOADER=ltdl
			DL_LIBS="$kik_libltdl_libdir -lltdl"
			],[
			echo ""
			echo "Could not find libltdl"
			echo ""
			exit 1])
	LDFLAGS="$kik_ldflags_save"
fi
# lt_cv_dlopen is set by AC_LIBTOOL_DLOPEN
if test "$DL_LOADER" = none ; then
	case "${lt_cv_dlopen}" in
	# LoadLibrary  (Windows)
	LoadLibrary)
		DL_LOADER=win32
		;;
	# shl_load (HP-UX)
	shl_load)
		DL_LOADER=dld
		;;
	# dlopen (UNIX98)
	dlopen)
		DL_LOADER=dl
		;;
	# What is dld_link? Does anybody know?
	dld_link)
		DL_LOADER=none
		;;
	*)
		DL_LOADER=none
		;;
	esac
	DL_LIBS="${DL_LIBS} ${lt_cv_dlopen_libs}"
	if test "$DL_LOADER" = none ; then
		# NSLinkModule (darwin)
		AC_CHECK_FUNC(NSLinkModule,
			[
			DL_LOADER=dyld
			DL_LIBS=
			], [])
	fi

	# cygwin hack
	# (AC_LIBTOOL_DLOPEN tell lt_cv_dlopen == dlopen but use kik_dlcfn_win32.c.)
	if test "$kik_cv_cygwin" = "yes" ; then
		DL_LOADER=win32
	fi
fi
AC_SUBST(DL_LOADER)
AC_SUBST(DL_LIBS)
AC_SUBST(DL_CFLAGS)

if test "$DL_LOADER" = none ; then
	AC_DEFINE(DLFCN_NONE,,"DLFCN_NONE")
fi


#
# --- check for undefined symbol ---
#
AC_MSG_CHECKING([for undefined symbol])
if test "x$allow_undefined_flag" = "xunsupported" ; then
	AC_MSG_RESULT([not supported])
	NO_UNDEFINED_FLAG="-no-undefined"
else
	AC_MSG_RESULT(supported)
	NO_UNDEFINED_FLAG=""
fi
AC_SUBST(NO_UNDEFINED_FLAG)

#
# --- pty check ---
#
AC_MSG_CHECKING(for pty/tty type)
AC_CACHE_VAL(kik_cv_pty,
	[
	if test "$kik_cv_win32" = "yes" ; then
		kik_cv_pty=none
	elif test "$kik_cv_mingw32" = "yes" ; then
		kik_cv_pty=streams
	else
		AC_CHECK_FUNC( posix_openpt,
			[AC_RUN_IFELSE([AC_LANG_PROGRAM([#include <fcntl.h>],
				[return posix_openpt( O_RDWR | O_NOCTTY) == -1;])],
				[
				AC_DEFINE(HAVE_POSIX_OPENPT,,"HAVE_POSIX_OPENPT")
				kik_cv_pty=streams
				],
				[kik_cv_pty=bsd])],
			[kik_cv_pty=bsd])
		if test "$kik_cv_pty" = "bsd" ; then
			AC_RUN_IFELSE([AC_LANG_PROGRAM([#include <fcntl.h>],
				[return open( "/dev/ptmx", O_RDWR | O_NOCTTY, 0) == -1;])],
				[kik_cv_pty=streams])
		fi
	fi
	])
AC_ARG_ENABLE(pty_helper,
	[  --enable-pty-helper     use pty helper @<:@default=disabled@:>@],
	pty_helper=$enable_pty_helper)
if test "$pty_helper" = yes ; then
	if test "$kik_cv_pty" = streams ; then
		kik_cv_pty=helper
	else
		echo ""
		echo "** WARNING **"
		echo " pty helper is not supported in bsd-style pty system."
		echo ""
	fi
fi
AC_MSG_RESULT($kik_cv_pty)
PTY_NAME="${kik_cv_pty}"
AC_SUBST(PTY_NAME)


#
# --- checks for utmp ---
#
UTMP_NAME=
UTMP_LIBS=
if test "$kik_cv_pty" = helper -o "$kik_cv_mingw32" = yes -o "$kik_cv_cygwin" = yes ; then
	UTMP_NAME=none
	UTMP_LIBS=
fi
#  libutempter
if test -z "$UTMP_NAME" ; then
	AC_CHECK_LIB(utempter, addToUtmp,
		[
		UTMP_NAME=utmper
		UTMP_LIBS="-lutempter -lutil"
		], [])
fi
#  setutxent()  (SysV)
if test -z "$UTMP_NAME" ; then
	AC_CHECK_FUNC(setutxent,
		[
		UTMP_NAME=sysv
		UTMP_LIBS=
		UTMP_CFLAGS=-DUSE_UTMPX
		], [])
	AC_SUBST(UTMP_CFLAGS)
fi
#  setutent()  (SysV)
if test -z "$UTMP_NAME" ; then
	AC_CHECK_FUNC(setutent,
		[
		UTMP_NAME=sysv
		UTMP_LIBS=
		], [])
fi
#  libutil
if test -z "$UTMP_NAME" ; then
	AC_CHECK_LIB(util, logout,
		[
		UTMP_NAME=login
		UTMP_LIBS=-lutil
		], [])
fi
#  other (BSD)
if test -z "$UTMP_NAME" ; then
	UTMP_NAME=bsd
	UTMP_LIBS=
fi
AC_SUBST(UTMP_NAME)
AC_SUBST(UTMP_LIBS)


#
# --- debug ---
#
AC_ARG_ENABLE(debug,
	[  --enable-debug          debug @<:@default=disabled@:>@],
	debug=$enable_debug)
if test "$debug" = yes ; then
	DEB_CFLAGS="-DDEBUG -DKIK_DEBUG"
fi
AC_SUBST(DEB_CFLAGS)

#
# --- remove functions mlterm doesn't use ---
#
AC_ARG_WITH(funcs-mlterm-unuse,
	[  --without-funcs-mlterm-unuse remove functions mlterm doesn't use @<:@default=with@:>@],
	funcs_mlterm_unuse=$with_funcs_mlterm_unuse,funcs_mlterm_unuse="yes")
if test "$funcs_mlterm_unuse" = "no" ; then
	AC_DEFINE(REMOVE_FUNCS_MLTERM_UNUSE,,"REMOVE_FUNCS_MLTERM_UNUSE")
fi

#
# --- check for malloc ---
#
AC_TRY_RUN(
[
#include <stdio.h>
int
main()
{
return  calloc(8 , ((1 << (sizeof(size_t) * 8 - 1)) + 1)) ? 1 : 0 ;
}
],
AC_DEFINE(CALLOC_CHECK_OVERFLOW))


AC_OUTPUT(Makefile src/Makefile)
