dnl Copyright 2000, 2001, 2002 by Paul Mattes.
dnl  Permission to use, copy, modify, and distribute this software and its
dnl  documentation for any purpose and without fee is hereby granted,
dnl  provided that the above copyright notice appear in all copies and that
dnl  both that copyright notice and this permission notice appear in
dnl  supporting documentation.
dnl 
dnl x3270 is distributed in the hope that it will be useful, but WITHOUT ANY
dnl WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
dnl FOR A PARTICULAR PURPOSE.  See the file LICENSE for more details.

dnl Process this file with autoconf to produce a configure script.
AC_INIT(main.c)
AC_CONFIG_HEADER(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 solaris, then pass the -D__EXTENSIONS__ flag to cc, so that
dnl all of usual Unix functions are visible to strict ANSI compilers.
AC_CANONICAL_HOST
case "$host_os" in
solaris2*)	XANSI=-D__EXTENSIONS__
		if test "$GCC" = yes
		then	NIXCCOPTIONS="CCOPTIONS ="
		fi
		;;
darwin*)	XPRECOMP=-no-cpp-precomp
		;;
esac
AC_SUBST(XANSI)
AC_SUBST(XPRECOMP)
AC_SUBST(NIXCCOPTIONS)

dnl Check for X
AC_PATH_X
if test "$no_x" = "yes"; then AC_MSG_ERROR('Cannot find X utilities or libraries'); fi

dnl Check for --with-all-xinstall
AC_ARG_WITH(all_xinstall,[  --with-all-xinstall     install all components in system X11 directory])
case "$with_all_xinstall" in
yes)	prefix='$(PROJECTROOT)'
	;;
esac

dnl Check for --enable-app-defaults
AC_ARG_ENABLE(app-defaults,
    [  --enable-app-defaults   use a separate app-defaults file],
    [DEFINE_UAD="#define USE_APP_DEFAULTS 1"; if test "$enable_app_defaults" = "yes"; then UAD_FLAGS="-DCOLOR"; else UAD_FLAGS="$enable_app_defaults"; fi],
    [DEFINE_UAD=""; UAD_FLAGS="-DCOLOR"])
AC_SUBST(UAD_FLAGS)
AC_SUBST(DEFINE_UAD)

dnl Check for --without-pr3287
AC_ARG_WITH(pr3287,[  --without-pr3287        don't build pr3287])
if test -d pr3287 -a "$with_pr3287" != "no"
then	DEFINE_PR3287="#define PR3287 1"
	pr3mf=pr3287/Makefile
else	DEFINE_PR3287="#undef PR3287"
	pr3mf=""
fi
AC_SUBST(DEFINE_PR3287)

dnl Check for --with-fontdir=xxx
AC_ARG_WITH(fontdir,[  --with-fontdir=xxx      install fonts in directory xxx [ARG=misc]])
case "$with_fontdir" in
""|yes|no)
	IFONTDIR='$'"(FONTDIR)/misc"
	CIFONTDIR='$'"(exec_prefix)/fonts/misc"
	;;
/*)
	IFONTDIR="$with_fontdir"
	CIFONTDIR="$with_fontdir"
	;;
*)
	IFONTDIR='$'"(FONTDIR)/$with_fontdir"
	CIFONTDIR='$'"(exec_prefix)/fonts/$with_fontdir"
	;;
esac
AC_SUBST(IFONTDIR)
AC_SUBST(CIFONTDIR)

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

dnl Check for unwanted parts.
AC_ARG_ENABLE(ansi,[  --disable-ansi          leave out NVT (ANSI) support])
case "$enable_ansi" in
""|yes)	AC_DEFINE(X3270_ANSI,1)
	;;
esac
AC_ARG_ENABLE(apl,[  --disable-apl           leave out APL character support])
case "$enable_apl" in
""|yes)	AC_DEFINE(X3270_APL,1)
	;;
esac
AC_ARG_ENABLE(ft,[  --disable-ft            leave out file transfer support])
case "$enable_ft" in
""|yes)	AC_DEFINE(X3270_FT,1)
	;;
esac
AC_ARG_ENABLE(keypad,[  --disable-keypad        leave out pop-up keypad support])
case "$enable_keypad" in
""|yes)	AC_DEFINE(X3270_KEYPAD,1)
	;;
esac
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
AC_ARG_ENABLE(menus,[  --disable-menus         leave out menu support])
case "$enable_menus" in
""|yes)	AC_DEFINE(X3270_MENUS,1)
	;;
esac
AC_ARG_ENABLE(printer,[  --disable-printer       leave out printer session support])
case "$enable_printer" in
""|yes)	AC_DEFINE(X3270_PRINTER,1)
	;;
esac
AC_ARG_ENABLE(script,[  --disable-script        leave out scripting support])
case "$enable_script" in
""|yes)	AC_DEFINE(X3270_SCRIPT,1)
	;;
*)	X3270_SCRIPT="#undef X3270_SCRIPT" ;;
esac
AC_ARG_ENABLE(tn3270e,[  --disable-tn3270e       leave out TN3270E support])
case "$enable_tn3270e" in
""|yes)	AC_DEFINE(X3270_TN3270E,1)
	;;
esac
AC_ARG_ENABLE(trace,[  --disable-trace         leave out tracing support])
case "$enable_trace" in
""|yes)	AC_DEFINE(X3270_TRACE,1)
	;;
esac

dnl Check for other 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_CHECK_LIB(util, forkpty)
AC_CHECK_LIB(nsl_s, gethostbyname)
AC_CHECK_LIB(nsl, gethostbyname)
AC_CHECK_LIB(socket, socket)

dnl Checks for header files.
dnl AC_HEADER_STDC
dnl AC_HEADER_SYS_WAIT
dnl AC_CHECK_HEADERS(fcntl.h sys/ioctl.h sys/time.h unistd.h)
AC_CHECK_HEADERS(sys/select.h)
AC_CHECK_HEADERS(pty.h)
AC_CHECK_HEADERS(libutil.h)
AC_CHECK_HEADERS(util.h)
AC_CHECK_HEADERS(getopt.h)

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

dnl Checks for library functions.
dnl AC_PROG_GCC_TRADITIONAL
dnl AC_FUNC_MEMCMP
dnl AC_TYPE_SIGNAL
dnl AC_FUNC_VPRINTF
dnl AC_CHECK_FUNCS(gettimeofday putenv select socket strerror strstr strtol strtoul)
AC_CHECK_FUNCS(vasprintf)
AC_CHECK_FUNC(strtok_r,,AC_LIBOBJ(strtok_r)
AC_DEFINE(NEED_STRTOK_R))
AC_FUNC_FSEEKO

dnl Check for xmkmkf
AC_CHECK_PROG(xmkmf_found,xmkmf,found,notfound)
if test "$xmkmf_found" = "notfound"
then	AC_MSG_ERROR('Cannot find xmkmf')
fi

dnl Save a copy of Imakefile
if test -f Imakefile
then	cp Imakefile Imakefile.old
fi

dnl Generate the Imakefile and the pr3287 Makefile.
AC_OUTPUT(Imakefile $pr3mf)

dnl Generate the Makefile
gen_Makefile=""
if test ! -f Makefile; then gen_Makefile=1; fi
if cmp Imakefile.old Imakefile 1>/dev/null 2>/dev/null; then :; else gen_Makefile=1; fi
if test "$gen_Makefile"
then	echo "generating Makefile"
	xmkmf
else	echo "Makefile is unchanged"
fi
rm -f Imakefile.old
