dnl Process this file with autoconf to produce a configure script.
AC_INIT(main.c)

AM_INIT_AUTOMAKE(links, 0.92)
AM_CONFIG_HEADER(config.h)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_AWK

AC_MSG_CHECKING([for EMX])
AC_TRY_COMPILE(, [#ifndef __EMX__
kill me!
#endif ], have_emx=yes, have_emx=no)
AC_MSG_RESULT($have_emx)
test "$have_emx" = yes && LDFLAGS=`echo "$LDFLAGS" | sed "s/-Zexe//g"`

AC_MSG_CHECKING([for typeof])
AC_TRY_COMPILE(, [int a;
typeof(a) b;], have_typeof=yes, have_typeof=no)
AC_MSG_RESULT($have_typeof)
test "$have_typeof" = yes && AC_DEFINE(HAVE_TYPEOF)

AC_MSG_CHECKING([for long long])
AC_TRY_COMPILE(, [unsigned long long a; ], have_long_long=yes, have_long_long=no)
AC_MSG_RESULT($have_long_long)
test "$have_long_long" = yes && AC_DEFINE(HAVE_LONG_LONG)

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(fcntl.h limits.h sys/ioctl.h sys/time.h time.h unistd.h)
AC_CHECK_HEADERS(sigaction.h)
AC_CHECK_HEADERS(netinet/in_systm.h)
AC_CHECK_HEADERS(netinet/in_system.h)
AC_CHECK_HEADERS(netinet/ip.h)
AC_CHECK_HEADERS(sys/select.h)
AC_CHECK_HEADERS(sys/un.h)
AC_CHECK_HEADERS(sys/fmutex.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM

dnl Checks for library functions.
AC_PROG_GCC_TRADITIONAL
AC_FUNC_MEMCMP
AC_TYPE_SIGNAL
AC_FUNC_STRFTIME
AC_FUNC_VPRINTF
AC_HAVE_FUNCS(gettimeofday mkdir select strcspn strerror strstr strtol strtoul alarm chmod)
AC_HAVE_FUNCS(getpid setpgid getpgid setpgrp getpgrp)

dnl Checks for libraries.
dnl Replace `main' with a function in -lgpm:
AC_CHECK_LIB(gpm, Gpm_Open)
dnl Replace `main' with a function in -lsocket:
AC_CHECK_FUNC(socket, cf_result=yes, cf_result=no)
if test "$cf_result" = no; then
	AC_CHECK_LIB(socket, socket)
fi

#AC_MSG_CHECKING([for gethostbyname])
#AC_TRY_LINK([#include <netdb.h>], [gethostbyname("")], cf_result=yes, cf_result=no)
#AC_MSG_RESULT($cf_result)
AC_CHECK_FUNC(gethostbyname, cf_result=yes, cf_result=no)
if test "$cf_result" = no; then
	AC_CHECK_LIB(socket, gethostbyname, cf_result=yes, cf_result=no)
	if test "$cf_result" = no; then
		AC_CHECK_LIB(nsl, gethostbyname)
	else
		test -z "`echo $LIBS | grep -- -lsocket`" && LIBS="$LIBS -lsocket"
	fi
fi

AC_CHECK_FUNC(herror, AC_DEFINE(HAVE_HERROR))
AC_CHECK_FUNC(cfmakeraw, AC_DEFINE(HAVE_CFMAKERAW))

AC_MSG_CHECKING([for OS/2 threads])
CFLAGS_X="$CFLAGS"
CFLAGS="$CFLAGS -Zmt"
AC_TRY_LINK([#include <stdlib.h>], [_beginthread(NULL, NULL, 0, NULL)], cf_result=yes, cf_result=no)
AC_MSG_RESULT($cf_result)
if test "$cf_result" = yes; then
	AC_DEFINE(HAVE_BEGINTHREAD)
	AC_CHECK_FUNC(MouOpen, AC_DEFINE(HAVE_MOUOPEN))
	AC_CHECK_FUNC(_read_kbd, AC_DEFINE(HAVE_READ_KBD))
else
	CFLAGS="$CFLAGS_X"
#	AC_CHECK_FUNC(pthread_create, cf_result=yes, cf_result=no)
#	if  test "$cf_result" = yes; then
#		AC_DEFINE(HAVE_PTHREADS)
#	else
#		AC_CHECK_LIB(pthread, pthread_create, AC_DEFINE(HAVE_PTHREADS)
#		LDFLAGS="$LDFLAGS -lpthread" )
#	fi
fi
#AC_CHECK_FUNC(clone, AC_DEFINE(HAVE_CLONE))

AC_MSG_CHECKING([for XFree for OS/2])
CFLAGS_X="$CFLAGS"
LIBS_X="$LIBS"
cf_result=no
if test -n "$X11ROOT"; then
	CFLAGS="$CFLAGS_X -I$X11ROOT/XFree86/include"
	LIBS="$LIBS_X -L$X11ROOT/XFree86/lib -lxf86_gcc"
	AC_TRY_LINK([#include <pty.h>], [struct winsize win;ptioctl(1, TIOCGWINSZ, &win)], cf_result=yes, cf_result=no)
	if test "$cf_result" = no; then
		LIBS="$LIBS_X -L$X11ROOT/XFree86/lib -lxf86"
		AC_TRY_LINK([#include <pty.h>], [struct winsize win;ptioctl(1, TIOCGWINSZ, &win)], cf_result=yes, cf_result=no)
	fi
fi
if test "$cf_result" != yes; then
	CFLAGS="$CFLAGS_X"
	LIBS="$LIBS_X"
else
	AC_DEFINE(X2)
fi
AC_MSG_RESULT($cf_result)

test "$have_emx" = yes && LDFLAGS="$LDFLAGS -Zexe"

AC_OUTPUT(Makefile)

#if test -z "$AWK"; then
#	AC_WARN([awk not found. You won't be able to rebuild code page table.]);
#fi
