dnl Copyright (c) 2015-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 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([suite3270],[4.5])
AC_PREREQ([2.69])

AC_CANONICAL_HOST

AC_ARG_ENABLE(x3270,[  --enable-x3270          build x3270])
AC_ARG_ENABLE(c3270,[  --enable-c3270          build c3270])
AC_ARG_ENABLE(s3270,[  --enable-s3270          build s3270])
AC_ARG_ENABLE(b3270,[  --enable-b3270          build b3270])
AC_ARG_ENABLE(tcl3270,[  --enable-tcl3270        build tcl3270])
AC_ARG_ENABLE(pr3287,[  --enable-pr3287         build pr3287])
AC_ARG_ENABLE(x3270if,[  --enable-x3270if        build x3270if])
AC_ARG_ENABLE(playback,[  --enable-playback       build playback])
AC_ARG_ENABLE(mitm,[  --enable-mitm           build mitm])
AC_ARG_ENABLE(wc3270,[  --enable-wc3270         build wc3270])

dnl Figure out if this is a Windows build or not.
. Common/dirnames
case "$host" in
$win64|x86_64-pc-msys)	dnl 64-bit Windows
    enable_windows=yes
    enable_windows32=no
    enable_windows64=yes
    enable_unix=no
    MODE=windows
    dirpfx="w"
    WIN32_FLAGS="-D_WIN32 -D_WIN64"
    if test $host = $win64; then toolpfx=$host-; else toolpfx=""; fi
    ;;
$win32|i686-pc-msys)	dnl 32-bit Windows
    enable_windows=yes
    enable_windows32=yes
    enable_windows64=no
    enable_unix=no
    MODE=windows
    WIN32_FLAGS="-D_WIN32"
    dirpfx="w"
    if test $host = $win32; then toolpfx=$host-; else toolpfx=""; fi
    ;;
*)			dnl unix
    enable_windows=no
    enable_windows32=no
    enable_windows64=no
    enable_unix=yes
    MODE=unix
    dirpfx=""
esac

dnl Look for Windows tools
if test $enable_windows = yes
then
    AC_PATH_PROGS(WINDOWS_CC,${toolpfx}gcc,no)
    if test $WINDOWS_CC = no
    then	AC_MSG_ERROR([Can't find Windows C compiler])
    fi
    AC_PATH_PROGS(WINDOWS_AR,${toolpfx}gcc-ar,no)
    if test $WINDOWS_AR = no
    then	AC_MSG_ERROR([Can't find Windows C library archiver])
    fi
    AC_PATH_PROGS(WINDRES,${toolpfx}windres windres.exe,no)
    if test $WINDRES = no
    then	AC_MSG_ERROR([Can't find windres])
    fi
fi

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 Figure out if our host is Windows.
if test $MODE = windows; then
    platform=`python3 -c 'import sys; print(sys.platform)'`
    case "$platform" in
    cygwin|win*) WINHOST=WINHOST=1;;
    *) unset WINHOST;;
    esac
    AC_SUBST(WINHOST)
fi

dnl Handle expansion of Unix targets.
if test $MODE = unix; then
    if test "x$enable_x3270" != xno; then enable_x3270=yes; fi
    if test "x$enable_c3270" != xno; then enable_c3270=yes; fi
    if test "x$enable_s3270" != xno; then enable_s3270=yes; fi
    if test "x$enable_b3270" != xno; then enable_b3270=yes; fi
    if test "x$enable_tcl3270" != xno; then enable_tcl3270=yes; fi
    if test "x$enable_pr3287" != xno; then enable_pr3287=yes; fi
    if test "x$enable_x3270if" != xno; then enable_x3270if=yes; fi
    if test "x$enable_playback" != xno; then enable_playback=yes; fi
    if test "x$enable_mitm" != xno; then enable_mitm=yes; fi
else
    enable_x3270=no
    enable_tcl3270=no
fi

dnl Set the 32-bit and 64-bit conditionals for Windows builds
if test "x$enable_windows" != xno; then
    dnl Default 32- and 64-bit if they are not already specified.
    if test "x$enable_windows32" = x -a "x$enable_windows64" = x; then
	enable_windows32=$mingw32_found
	enable_windows64=$mingw64_found
    fi
fi

dnl Handle expansion of Windows targets.
if test $MODE = windows; then
    if test "x$enable_wc3270" != xno; then enable_wc3270=yes; fi
    if test "x$enable_s3270" != xno; then enable_s3270=yes; fi
    if test "x$enable_b3270" != xno; then enable_b3270=yes; fi
    if test "x$enable_pr3287" != xno; then enable_pr3287=yes; fi
    if test "x$enable_x3270if" != xno; then enable_x3270if=yes; fi
    if test "x$enable_playback" != xno; then enable_playback=yes; fi
    if test "x$enable_mitm" != xno; then enable_mitm=yes; fi
else
    enable_wc3270=no
fi

dnl Handle all-default case.
if test "x$enable_x3270" = x -a \
        "x$enable_c3270" = x -a \
        "x$enable_s3270" = x -a \
        "x$enable_b3270" = x -a \
        "x$enable_tcl3270" = x -a \
        "x$enable_pr3287" = x -a \
        "x$enable_x3270if" = x -a \
        "x$enable_playback" = x -a \
        "x$enable_mitm" = x -a \
	"x$enable_wc3270" = x; then
    AC_MSG_NOTICE(configuring all emulators... use --enable or --disable options to pick a subset)
    if test $MODE = unix; then
	enable_x3270=yes
	enable_tcl3270=yes
    fi
    if test $MODE = windows; then
	enable_wc3270=yes
    fi
    enable_c3270=yes
    enable_s3270=yes
    enable_b3270=yes
    enable_pr3287=yes
    enable_x3270if=yes
    enable_playback=yes
    enable_mitm=yes
fi

dnl Handle the just-disable case.
lparen="("
rparen=")"
if test "x$enable_x3270" != xyes -a \
	"x$enable_c3270" != xyes -a \
	"x$enable_s3270" != xyes -a \
	"x$enable_b3270" != xyes -a \
	"x$enable_tcl3270" != xyes -a \
	"x$enable_pr3287" != xyes -a \
	"x$enable_x3270if" != xyes -a \
	"x$enable_playback" != xyes -a \
	"x$enable_mitm" != xyes -a \
	"x$enable_wc3270" != xyes -a \
	$lparen "x$enable_x3270" = xno -o \
	 "x$enable_c3270" = xno -o \
	 "x$enable_s3270" = xno -o \
	 "x$enable_b3270" = xno -o \
	 "x$enable_tcl3270" = xno -o \
	 "x$enable_pr3287" = xno -o \
	 "x$enable_x3270if" = xno -o \
	 "x$enable_playback" = xno -o \
	 "x$enable_wc3270" = xno $rparen; then
    dnl Enable everything that wasn't explicitly disabled.
    if test $MODE = unix; then
	if test "x$enable_x3270" != xno; then enable_x3270=yes; fi
	if test "x$enable_tcl3270" != xno; then enable_tcl3270=yes; fi
    fi
    if test $MODE = windows; then
	if test "x$enable_wc3270" != xno; then enable_wc3270=yes; fi
    fi
    if test "x$enable_c3270" != xno; then enable_c3270=yes; fi
    if test "x$enable_s3270" != xno; then enable_s3270=yes; fi
    if test "x$enable_b3270" != xno; then enable_b3270=yes; fi
    if test "x$enable_pr3287" != xno; then enable_pr3287=yes; fi
    if test "x$enable_x3270if" != xno; then enable_x3270if=yes; fi
    if test "x$enable_playback" != xno; then enable_playback=yes; fi
    if test "x$enable_mitm" != xno; then enable_mitm=yes; fi
fi

dnl Handle impossibilities.
if test $MODE = windows -a $lparen x$enable_x3270 = xyes -o x$enable_tcl3270 = xyes -o x$enable_c3270 = xyes $rparen; then
    AC_MSG_NOTICE([ignoring Unix-specific targets])
    enable_x3270=no
    enable_tcl3270=no
    enable_c3270=no
fi
if test $MODE = unix -a x$enable_wc3270 = xyes; then
    AC_MSG_NOTICE([ignoring Windows-specific targets])
    enable_wc3270=no
fi

dnl Handle dependencies.
if test "x$enable_tcl3270" = xyes; then
    enable_s3270=yes
fi
if test "x$enable_x3270" = xyes -o \
        "x$enable_c3270" = xyes -o \
        "x$enable_s3270" = xyes -o \
        "x$enable_b3270" = xyes -o \
	"x$enable_tcl3270" = xyes; then
    enable_x3270if=yes
fi
if test "x$enable_x3270" = xyes -o \
	"x$enable_c3270" = xyes -o \
	"x$enable_s3270" = xyes -o \
	"x$enable_b3270" = xyes -o \
	"x$enable_tcl3270" = xyes; then
    enable_pr3287=yes
fi
if test "x$enable_x3270" = xyes -o \
        "x$enable_c3270" = xyes -o \
        "x$enable_wc3270" = xyes -o \
        "x$enable_s3270" = xyes -o \
        "x$enable_b3270" = xyes -o \
	"x$enable_pr3287" = xyes; then
    enable_lib=yes
fi
unset do_ibm_hosts
if test "x$enable_x3270" = xyes -o "x$enable_c3270" = xyes; then do_ibm_hosts=yes; fi

dnl Start recursing.
if test "x$enable_lib" = xyes; then
    if test $MODE = unix; then
	AC_CONFIG_SUBDIRS(lib)
    fi
    if test $MODE = windows; then
	AC_CONFIG_SUBDIRS(lib/w32xx)
	AC_CONFIG_SUBDIRS(lib/w3270)
    fi
fi

if test "x$do_ibm_hosts" = xyes; then
    AC_CONFIG_SUBDIRS(ibm_hosts)
fi

T_ALL=""
T_CLEAN=""
T_CLOBBER=""
T_INSTALL=""
T_INSTALL_MAN=""
T_TEST=""
T_ALLTESTS=""
T_TEST_TARGETS=""
T_TEST_CLEAN=""
T_TEST_CLOBBER=""

for i in x3270 c3270 s3270 b3270 tcl3270 pr3287 x3270if playback mitm wc3270; do
    eval x=\$enable_$i
    if test "x$x" = xyes; then
	T_ALL="$T_ALL $i"
	T_CLEAN="$T_CLEAN $i-clean"
	T_CLOBBER="$T_CLOBBER $i-clobber"
	if test $MODE = unix; then
	    case $i in
	    playback|mitm)	;;
	    *)
		T_INSTALL="$T_INSTALL $i-install"
		T_INSTALL_MAN="$T_INSTALL_MAN $i-install.man"
	    esac
	fi
	T_TEST="$T_TEST $i"
	T_TEST_TARGETS="$T_TEST_TARGETS ${i}-test"
    fi
done
T_ALLTESTS=$T_TEST
T_TEST_TARGETS=`echo $T_TEST_TARGETS | sed -e 's/^ //'`

if test "x$enable_lib" = xyes; then
    T_CLEAN="$T_CLEAN lib-clean"
    T_CLOBBER="$T_CLOBBER lib-clobber"
    T_ALLTESTS="$T_ALLTESTS lib-test"
fi

if test "x$enable_x3270" = xyes; then AC_CONFIG_SUBDIRS(x3270)
fi
if test "x$enable_c3270" = xyes; then AC_CONFIG_SUBDIRS(c3270)
fi
if test "x$enable_s3270" = xyes -a $MODE = unix; then AC_CONFIG_SUBDIRS(s3270)
fi
if test "x$enable_s3270" = xyes -a $MODE = windows; then AC_CONFIG_SUBDIRS(ws3270)
fi
if test "x$enable_b3270" = xyes -a $MODE = unix; then AC_CONFIG_SUBDIRS(b3270)
fi
if test "x$enable_b3270" = xyes -a $MODE = windows; then AC_CONFIG_SUBDIRS(wb3270)
fi
if test "x$enable_tcl3270" = xyes; then AC_CONFIG_SUBDIRS(tcl3270)
fi
if test "x$enable_pr3287" = xyes -a $MODE = unix; then AC_CONFIG_SUBDIRS(pr3287)
fi
if test "x$enable_pr3287" = xyes -a $MODE = windows; then AC_CONFIG_SUBDIRS(wpr3287)
fi
if test "x$enable_x3270if" = xyes -a $MODE = unix; then AC_CONFIG_SUBDIRS(x3270if)
fi
if test "x$enable_x3270if" = xyes -a $MODE = windows; then AC_CONFIG_SUBDIRS(wx3270if)
fi
if test "x$enable_playback" = xyes -a $MODE = unix; then AC_CONFIG_SUBDIRS(playback)
fi
if test "x$enable_mitm" = xyes -a $MODE = unix; then AC_CONFIG_SUBDIRS(mitm)
fi
if test "x$enable_mitm" = xyes -a $MODE = windows; then AC_CONFIG_SUBDIRS(wmitm)
fi
if test "x$enable_wc3270" = xyes; then AC_CONFIG_SUBDIRS(wc3270)
fi

dnl Set the targets that don't work in each mode.
case $MODE in
    unix)	T_WRONG_MODE="wb3270 wc3270 ws3270 wpr3287 wx3270if wplayback wmitm"
    ;;
    windows)	T_WRONG_MODE="x3270 tcl3270 ibm_hosts"
    ;;
esac

AC_SUBST(host)
AC_SUBST(MODE)
AC_SUBST(T_ALL)
AC_SUBST(T_CLEAN)
AC_SUBST(T_CLOBBER)
AC_SUBST(T_INSTALL)
AC_SUBST(T_INSTALL_MAN)
AC_SUBST(T_TEST)
AC_SUBST(T_ALLTESTS)
AC_SUBST(T_TEST_TARGETS)
AC_SUBST(T_TEST_CLEAN)
AC_SUBST(T_TEST_CLOBBER)
AC_SUBST(T_WRONG_MODE)
AC_SUBST(WIN32_FLAGS)

dnl Generate to top-level Makefiles.
AC_CONFIG_FILES([Makefile Makefile.$MODE])
AC_OUTPUT

dnl Gather warnings.
find . -type f -name config.log | xargs ls -t | xargs grep WARNING; true
