dnl Copyright (c) 2000-2023 Paul Mattes.
dnl All rights reserved.
dnl
dnl Redistribution and use in source and binary forms, with or without
dnl modification, are permitted provided that the following conditions are met:
dnl     * Redistributions of source code must retain the above copyright
dnl       notice, this list of conditions and the following disclaimer.
dnl     * Redistributions in binary form must reproduce the above copyright
dnl       notice, this list of conditions and the following disclaimer in the
dnl       documentation and/or other materials provided with the distribution.
dnl     * Neither the names of Paul Mattes nor the names of his contributors
dnl       may be used to endorse or promote products derived from this software
dnl       without specific prior written permission.
dnl
dnl THIS SOFTWARE IS PROVIDED BY PAUL MATTES "AS IS" AND ANY EXPRESS OR IMPLIED
dnl WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
dnl MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
dnl EVENT SHALL PAUL MATTES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
dnl SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
dnl TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
dnl PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
dnl LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
dnl NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
dnl SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

dnl Process this file with autoconf to produce a configure script.
AC_INIT([c3270],[4.3])
AC_PREREQ([2.69])
AC_CONFIG_HEADERS([conf.h])

dnl Checks for programs.
AC_PROG_INSTALL
AC_PROG_CC

dnl Figure out what sort of host this is.
dnl If it's hpux, then pass the -D_XOPEN_SOURCE_EXTENDED flag to cc, so that
dnl all of the curses KEY_XXX definitions are visible.
dnl If it's solaris2, then pass the -D__EXTENSIONS__ flas to cc, so that all
dnl of the usual Unix functions are visible.
ansi="-std=c99 -pedantic"
AC_CANONICAL_HOST
case "$host_os" in
*cygwin*)	CPPFLAGS="$CPPFLAGS -D_XOPEN_SOURCE_EXTENDED"
    		dnl On Cygwin, gcc -ansi or -std=xxx hides POSIX declarations,
		dnl and there is no macro you can set to get them back.
    		ansi=""
		;;
hpux)		CPPFLAGS="$CPPFLAGS -D_XOPEN_SOURCE_EXTENDED"
		;;
solaris2*)	CPPFLAGS="$CPPFLAGS -D__EXTENSIONS__ -D_XOPEN_SOURCE_EXTENDED"
		;;
darwin*)	CCOPTIONS="$CCOPTIONS -no-cpp-precomp -Wno-deprecated-declarations"
		CPPFLAGS="$CPPFLAGS -D_XOPEN_SOURCE_EXTENDED"
		;;
linux*)		CPPFLAGS="$CPPFLAGS -D_POSIX_SOURCE -D_BSD_SOURCE -D_DEFAULT_SOURCE -D_XOPEN_SOURCE"
		;;
aix*)		BROKEN_NEWTERM=1
		;;
*bsd*)		CPPFLAGS="$CPPFLAGS -D_XOPEN_SOURCE_EXTENDED"
		;;
esac
if test "$GCC" = yes
then	# Add common gcc options, plus flags for dependency generation.
    	CCOPTIONS="$CCOPTIONS -Wall -Wsign-compare $ansi -Wno-variadic-macros -MMD -MP"
fi
AC_SUBST(host)
AC_SUBST(BROKEN_NEWTERM)
AC_SUBST(CCOPTIONS)
AC_SUBST(BUILDCC)

dnl Checks for header files.
AC_CHECK_HEADERS(sys/select.h)
if test "$with_readline" != no; then
AC_CHECK_HEADERS(readline/history.h)
fi
AC_CHECK_HEADERS(pty.h)
AC_CHECK_HEADERS(libutil.h)
AC_CHECK_HEADERS(util.h)
AC_CHECK_HEADERS(getopt.h)
dnl Find the best curses header file and hope it's consistent with the library
dnl we found.
AC_CHECK_HEADERS(ncursesw/ncurses.h, ,
 [AC_CHECK_HEADERS(ncurses/ncurses.h, ,
  [AC_CHECK_HEADERS(ncurses.h, ,
   [AC_CHECK_HEADERS(curses.h, , [AC_MSG_ERROR(No curses header file)])])])])
AC_CHECK_HEADERS(ncursesw/term.h, ,
 [AC_CHECK_HEADERS(ncurses/term.h, ,
  [AC_CHECK_HEADERS(term.h)])])

dnl Check for --without-readline
AC_ARG_WITH(readline, [  --without-readline      Don't use the readline library])

dnl Check for libraries.
dnl Note that the order here is important.  The last libraries should appear
dnl first, so that objects in them can be used by subsequent libraries.
AC_SEARCH_LIBS(forkpty, util)
AC_CHECK_FUNCS(forkpty)
AC_SEARCH_LIBS(newterm, ncursesw ncurses curses, , [AC_MSG_ERROR(Can't find libncurses or new-enough libcurses)])
AC_SEARCH_LIBS(setupterm, tinfow tinfo, , [AC_MSG_ERROR(Can't find terminfo library)])
if test "$with_readline" != no; then
AC_CHECK_LIB(readline, rl_initialize)
fi
AC_SEARCH_LIBS(gethostbyname, nsl)
AC_SEARCH_LIBS(socket, socket)

dnl If we're on AIX and have ncurses, cancel BROKEN_NEWTERM.
if test -n "$BROKEN_NEWTERM"
then	if test "$ac_cv_lib_ncurses_newterm" = yes
	then	:
	else	AC_DEFINE(BROKEN_NEWTERM,1)
	fi
fi

dnl Check for the ncurses default-color extensions.
AC_CHECK_FUNCS(use_default_colors)

dnl Checks for library functions.
AC_CHECK_FUNCS(vasprintf)
AC_FUNC_FSEEKO

dnl Check for curses wide character support.
AC_SEARCH_LIBS(wadd_wch, ncursesw ncurses curses, [AC_DEFINE(CURSES_WIDE,1)
Cw=1], [AC_MSG_WARN(Wide curses not found -- c3270 will not be able to support multi-byte character encodings)])
AC_SUBST(CURSES_WIDE)
AC_CHECK_FUNCS(tiparm)

dnl Check for default pager
AC_PATH_PROG(LESSPATH, less)
AC_DEFINE_UNQUOTED(LESSPATH,"$LESSPATH")
AC_PATH_PROG(MOREPATH, more)
AC_DEFINE_UNQUOTED(MOREPATH,"$MOREPATH")

dnl Set up the configuration directory.
LIBX3270DIR='${sysconfdir}/x3270'
AC_SUBST(LIBX3270DIR)

dnl Check for unwanted parts.
AC_ARG_ENABLE(dbcs,[  --disable-dbcs          leave out DBCS support])
case "$enable_dbcs" in
no)	;;
*)	AC_DEFINE(X3270_DBCS,1)
	DBCS=-DX3270_DBCS=1
	;;
esac
AC_SUBST(DBCS)
AC_ARG_ENABLE(local_process,[  --disable-local-process leave out local process support])
case "$enable_local_process" in
""|yes)	AC_DEFINE(X3270_LOCAL_PROCESS,1)
	;;
esac

dnl Generate the Makefile.
AC_CONFIG_FILES(Makefile Makefile.obj)
AC_OUTPUT
