dnl Copyright (c) 2015-2016, 2018, 2020 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.0)
AC_PREREQ(2.50)

AC_ARG_ENABLE(unix,[  --enable-unix           build all Unix emulators])
AC_ARG_ENABLE(windows,[  --enable-windows        build all Windows emulators])

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(unix-lib,[  --enable-lib            build Unix libraries])

AC_ARG_ENABLE(wc3270,[  --enable-wc3270         build wc3270])
AC_ARG_ENABLE(ws3270,[  --enable-ws3270         build Windows s3270])
AC_ARG_ENABLE(wb3270,[  --enable-wb3270         build Windows b3270])
AC_ARG_ENABLE(wpr3287,[  --enable-wpr3287        build Windows pr3287])
AC_ARG_ENABLE(wx3270if,[  --enable-wx3270if       build Windows x3270if])
AC_ARG_ENABLE(windows-lib,[  --enable-windows-lib    build Windows libraries])

AC_CHECK_PROG(mingw64_found,x86_64-w64-mingw32-gcc,yes,no)
AC_CHECK_PROG(mingw32_found,i686-w64-mingw32-gcc,yes,no)

dnl Handle expansion of enable-unix.
if test "x$enable_unix" = xyes; 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
fi
if test "x$enable_unix" = xno; then
    enable_x3270=no
    enable_c3270=no
    enable_s3270=no
    enable_b3270=no
    enable_tcl3270=no
    enable_pr3287=no
    enable_x3270if=no
fi

dnl Handle missing Windows tools.
if test "$mingw64_found" = "no" -o "$mingw32_found" = "no"
then	AC_MSG_WARN(Disabling Windows cross-compile)
	enable_windows=no
fi

dnl Handle expansion of enable-windows.
if test "x$enable_windows" = xyes; then
    if test "x$enable_wc3270" != xno; then enable_wc3270=yes; fi
    if test "x$enable_ws3270" != xno; then enable_ws3270=yes; fi
    if test "x$enable_wb3270" != xno; then enable_wb3270=yes; fi
    if test "x$enable_wpr3287" != xno; then enable_wpr3287=yes; fi
    if test "x$enable_wx3270if" != xno; then enable_wx3270if=yes; fi
fi
if test "x$enable_windows" = xno; then
    enable_wc3270=no
    enable_ws3270=no
    enable_wb3270=no
    enable_wpr3287=no
    enable_wx3270if=no
    enable_windows_lib=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_unix_lib" = x -a \
        "x$enable_wc3270" = x -a \
        "x$enable_ws3270" = x -a \
        "x$enable_wb3270" = x -a \
        "x$enable_wpr3287" = x -a \
        "x$enable_wx3270if" = x -a \
	"x$enable_windows_lib" = x; then
    AC_MSG_NOTICE(configuring all emulators... use --enable or --disable options to pick a subset)
    enable_x3270=yes
    enable_c3270=yes
    enable_s3270=yes
    enable_b3270=yes
    enable_tcl3270=yes
    enable_pr3287=yes
    enable_x3270if=yes
    enable_wc3270=yes
    enable_ws3270=yes
    enable_wb3270=yes
    enable_wpr3287=yes
    enable_wx3270if=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_unix_lib" != xyes -a \
	"x$enable_wc3270" != xyes -a \
	"x$enable_ws3270" != xyes -a \
	"x$enable_wb3270" != xyes -a \
	"x$enable_wpr3287" != xyes -a \
	"x$enable_wx3270if" != xyes -a \
	"x$enable_windows_lib" != 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_unix_lib" = xno -o \
	 "x$enable_wc3270" = xno -o \
	 "x$enable_ws3270" = xno -o \
	 "x$enable_wb3270" = xno -o \
	 "x$enable_wpr3287" = xno -o \
	 "x$enable_wx3270if" = xno -o \
	 "x$enable_windows_lib" = xno $rparen; then
    dnl Enable everything that wasn't explicitly disabled.
    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_wc3270" != xno; then enable_wc3270=yes; fi
    if test "x$enable_ws3270" != xno; then enable_ws3270=yes; fi
    if test "x$enable_wb3270" != xno; then enable_wb3270=yes; fi
    if test "x$enable_wpr3287" != xno; then enable_wpr3287=yes; fi
    if test "x$enable_wx3270if" != xno; then enable_wx3270if=yes; fi
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_wc3270" = xyes -o \
        "x$enable_ws3270" = xyes -o \
	"x$enable_wb3270" = xyes; then
    enable_wx3270if=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_pr3287" = xyes; then
    enable_unix_lib=yes
fi
if test "x$enable_wc3270" = xyes -o \
        "x$enable_ws3270" = xyes -o \
        "x$enable_wb3270" = xyes -o \
	"x$enable_wpr3287" = xyes; then
    enable_windows_lib=yes
fi

dnl Start recursing.
if test "x$enable_unix_lib" = xyes; then
    AC_CONFIG_SUBDIRS(lib)
fi

if test "x$enable_windows_lib" = xyes; then
    AC_CONFIG_SUBDIRS(lib/w32xx)
fi

T_ALL=""
T_CLEAN=""
T_CLOBBER=""
T_INSTALL=""
T_INSTALL_MAN=""
T_LIB=""
T_LIB_CLEAN=""
T_LIB_CLOBBER=""
T_UNIX_CLEAN=""
T_UNIX_CLOBBER=""
T_WINDOWS_CLEAN=""
T_WINDOWS_CLOBBER=""

for i in x3270 c3270 s3270 b3270 tcl3270 pr3287 x3270if wc3270 ws3270 wb3270 wc3270 wpr3287 wx3270if; 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"
	case "$i" in
	w*)	;;
	*)	T_INSTALL="$T_INSTALL $i-install"
		T_INSTALL_MAN="$T_INSTALL_MAN $i-install.man"
	esac
    fi
done

if test "x$enable_unix_lib" = xyes; then
    T_CLEAN="$T_CLEAN unix-lib-clean"
    T_CLOBBER="$T_CLOBBER unix-lib-clobber"
    T_LIB="$T_LIB unix-lib"
    T_LIB_CLEAN="$T_LIB_CLEAN unix-lib-clean"
    T_LIB_CLOBBER="$T_LIB_CLOBBER unix-lib-clobber"
    T_UNIX_CLEAN="$T_UNIX_CLEAN unix-lib-clean"
    T_UNIX_CLOBBER="$T_UNIX_CLOBBER unix-lib-clobber"
fi

if test "x$enable_windows_lib" = xyes; then
    T_CLEAN="$T_CLEAN windows-lib-clean"
    T_CLOBBER="$T_CLOBBER windows-lib-clobber"
    T_LIB="$T_LIB windows-lib"
    T_LIB_CLEAN="$T_LIB_CLEAN windows-lib-clean"
    T_LIB_CLOBBER="$T_LIB_CLOBBER windows-lib-clobber"
    T_WINDOWS_CLEAN="$T_WINDOWS_CLEAN windows-lib-clean"
    T_WINDOWS_CLOBBER="$T_WINDOWS_CLOBBER windows-lib-clobber"
fi

if test "x$enable_x3270" = xyes; then
    AC_CONFIG_SUBDIRS(x3270)
    T_UNIX_CLEAN="$T_UNIX_CLEAN x3270-clean"
    T_UNIX_CLOBBER="$T_UNIX_CLOBBER x3270-clobber"
fi
if test "x$enable_c3270" = xyes; then
    AC_CONFIG_SUBDIRS(c3270)
    T_UNIX_CLEAN="$T_UNIX_CLEAN c3270-clean"
    T_UNIX_CLOBBER="$T_UNIX_CLOBBER c3270-clobber"
fi
if test "x$enable_s3270" = xyes; then
    AC_CONFIG_SUBDIRS(s3270)
    T_UNIX_CLEAN="$T_UNIX_CLEAN s3270-clean"
    T_UNIX_CLOBBER="$T_UNIX_CLOBBER s3270-clobber"
fi
if test "x$enable_b3270" = xyes; then
    AC_CONFIG_SUBDIRS(b3270)
    T_UNIX_CLEAN="$T_UNIX_CLEAN b3270-clean"
    T_UNIX_CLOBBER="$T_UNIX_CLOBBER b3270-clobber"
fi
if test "x$enable_tcl3270" = xyes; then
    AC_CONFIG_SUBDIRS(tcl3270)
    T_UNIX_CLEAN="$T_UNIX_CLEAN tcl3270-clean"
    T_UNIX_CLOBBER="$T_UNIX_CLOBBER tcl3270-clobber"
fi
if test "x$enable_pr3287" = xyes; then
    AC_CONFIG_SUBDIRS(pr3287)
    T_UNIX_CLEAN="$T_UNIX_CLEAN pr3287-clean"
    T_UNIX_CLOBBER="$T_UNIX_CLOBBER pr3287-clobber"
fi
if test "x$enable_x3270if" = xyes; then
    AC_CONFIG_SUBDIRS(x3270if)
    T_UNIX_CLEAN="$T_UNIX_CLEAN x3270if-clean"
    T_UNIX_CLOBBER="$T_UNIX_CLOBBER x3270if-clobber"
fi

if test "x$enable_wc3270" = xyes; then
    T_WINDOWS_CLEAN="$T_WINDOWS_CLEAN wc3270-clean"
    T_WINDOWS_CLOBBER="$T_WINDOWS_CLOBBER wc3270-clobber"
fi
if test "x$enable_ws3270" = xyes; then
    T_WINDOWS_CLEAN="$T_WINDOWS_CLEAN ws3270-clean"
    T_WINDOWS_CLOBBER="$T_WINDOWS_CLOBBER ws3270-clobber"
fi
if test "x$enable_wb3270" = xyes; then
    T_WINDOWS_CLEAN="$T_WINDOWS_CLEAN wb3270-clean"
    T_WINDOWS_CLOBBER="$T_WINDOWS_CLOBBER wb3270-clobber"
fi
if test "x$enable_wpr3287" = xyes; then
    T_WINDOWS_CLEAN="$T_WINDOWS_CLEAN wpr3287-clean"
    T_WINDOWS_CLOBBER="$T_WINDOWS_CLOBBER wpr3287-clobber"
fi
if test "x$enable_wx3270if" = xyes; then
    T_WINDOWS_CLEAN="$T_WINDOWS_CLEAN wwx3270if-clean"
    T_WINDOWS_CLOBBER="$T_WINDOWS_CLOBBER wwx3270if-clobber"
fi

AC_SUBST(T_ALL)
AC_SUBST(T_CLEAN)
AC_SUBST(T_CLOBBER)
AC_SUBST(T_INSTALL)
AC_SUBST(T_INSTALL_MAN)
AC_SUBST(T_LIB)
AC_SUBST(T_LIB_CLEAN)
AC_SUBST(T_LIB_CLOBBER)
AC_SUBST(T_UNIX_CLEAN)
AC_SUBST(T_UNIX_CLOBBER)
AC_SUBST(T_WINDOWS_CLEAN)
AC_SUBST(T_WINDOWS_CLOBBER)

dnl Generate to top-level Makefile.
AC_OUTPUT(Makefile)
