dnl Copyright (c) 2000-2025 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
dnl 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 name of Paul Mattes nor his contributors may be used
dnl       to endorse or promote products derived from this software without
dnl       specific prior written permission.
dnl 
dnl THIS SOFTWARE IS PROVIDED BY PAUL MATTES "AS IS" AND ANY EXPRESS
dnl OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
dnl WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
dnl DISCLAIMED. IN NO EVENT SHALL PAUL MATTES BE LIABLE FOR ANY DIRECT,
dnl INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
dnl (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
dnl SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
dnl HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
dnl STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
dnl IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
dnl POSSIBILITY OF SUCH DAMAGE.

dnl Process this file with autoconf to produce a configure script.
AC_INIT([x3270if],[4.4])
AC_PREREQ([2.69])

dnl Checks for programs.
AC_PROG_INSTALL
AC_PROG_CC

dnl Figure out what sort of host and compiler this is.
AC_CANONICAL_HOST
ansi="-std=c99 -pedantic"
case "$host_os" in
*cygwin*)	ansi=""
		;;
solaris2*)	CPPFLAGS="$CPPFLAGS -D__EXTENSIONS__"
		;;
darwin*)	CCOPTIONS="$CCOPTIONS -no-cpp-precomp -Wno-deprecated-declarations"
		case "$build" in
                a*) if test -d /opt/homebrew/opt; then HOMEBREW=/opt/homebrew/opt; fi ;;
                x*) if test -d /usr/local/opt; then HOMEBREW=/usr/local/opt; fi ;;
                esac
		;;
linux*)		CPPFLAGS="$CPPFLAGS -D_POSIX_SOURCE"
		;;
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(CCOPTIONS)

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(gethostbyname, nsl)
AC_SEARCH_LIBS(socket, socket)

dnl Checks for header files.
AC_CHECK_HEADERS(sys/select.h)
AC_CHECK_HEADERS(getopt.h)
if test "$with_readline" != no; then
 if test -n "$with_readline" -a "$with_readline" != yes; then
  dnl Use the specified readline install path.
  CPPFLAGS="$CPPFLAGS -I${with_readline}/include"
 elif test -n "$HOMEBREW" -a -d "$HOMEBREW/readline/include"; then
  dnl Use the Homebrew readline install path.
  CPPFLAGS="$CPPFLAGS -I$HOMEBREW/readline/include"
 fi
 AC_CHECK_HEADERS(readline/history.h)
fi
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_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 Checks for library functions.
AC_CHECK_FUNCS(vasprintf)
AC_FUNC_FSEEKO
AC_SEARCH_LIBS(newterm, ncursesw ncurses curses)
AC_SEARCH_LIBS(setupterm, tinfow tinfo)
AC_CHECK_FUNCS(tiparm)
if test "$with_readline" != no; then
 if test -n "$with_readline" -a "$with_readline" != yes; then
  dnl Use the speficied readline install path.
  LDFLAGS="$LDFLAGS -L${with_readline}/lib"
 elif test -n "$HOMEBREW" -a -d $HOMEBREW/readline/lib; then
  dnl Use the Homebrew readline install path.
  LDFLAGS="$LDFLAGS -L$HOMEBREW/readline/lib"
 fi
 AC_CHECK_LIB(readline, rl_initialize)
fi

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

dnl Find Python
AC_ARG_WITH(python,[  --with-python=path      specify path to Python interpreter])
if test "x$with_python" != x; then
    python=$with_python
elif test "x$PYTHON" != x; then
    python=$PYTHON
else
    python=python3
fi
AC_PATH_PROG(PYTHON,$python,not found)
if test "$PYTHON" = "not found"; then
    AC_MSG_ERROR([Can't find Python using '$python', try --with-python=])
fi
AC_SUBST(PYTHON)

dnl Generate the files.
AC_CONFIG_HEADERS([conf.h])
AC_CONFIG_FILES(Makefile Makefile.obj)
AC_OUTPUT
