dnl Process this file with autoconf to produce a configure script.
AC_INIT(The Hessling Editor, m4_esyscmd_s([grep VER_DOT ./the.ver | cut -f 2 -d '=']), mark@rexx.org, the)
AC_PREFIX_DEFAULT(/usr/local)

MH_PACKAGE="the"

. $srcdir/the.ver
VERSION_NODOT=\"$VER\"
VERSION=\"$VER_DOT\"
VERSION_UNQUOTED=$VER_DOT
VERSION_BASE_UNQUOTED=$VER_BASE
VER_DATE=\"$VER_DATE\"
AC_SUBST(VERSION)
AC_SUBST(VERSION_UNQUOTED)
AC_SUBST(VERSION_BASE_UNQUOTED)
AC_SUBST(VERSION_NODOT)
AC_SUBST(VER_DATE)
THE_PACKAGE_NAME="the"
AC_SUBST(THE_PACKAGE_NAME)
MH_SET_FULL_PACKAGE_NAME($THE_PACKAGE_NAME,$VER_DOT)
cp $srcdir/common/config.guess $srcdir
cp $srcdir/common/config.sub $srcdir

dnl ---------------------- check for C compiler -----------------
dnl Checks for one of the C compilers below - change if yours not there.

MH_PROG_CC

AC_CONFIG_HEADER(config.h)
AC_ISC_POSIX

dnl Checks for system first
AC_CANONICAL_SYSTEM

dnl
dnl include our 32/64bit tests
dnl
sinclude(common/inclibarch.m4)
dnl
dnl Test if using g++ and if its broken WRT string.h
if test "$ac_cv_prog_CC" = "g++"; then
   MH_HAVE_BROKEN_CXX_WITH_STRING_H
fi

BASE_BINARY="binarybase"
BASE_INSTALL="installbase"
on_qnx=no
on_cygwin="no"
on_macos="no"
case "$target" in
   *hp-hpux9*)
      SYS_DEFS="-D_HPUX_SOURCE"
      AC_DEFINE(SELECT_IN_TIME_H)
      ;;
   *hp-hpux*)
      SYS_DEFS="-D_HPUX_SOURCE"
      if test "$ac_cv_prog_CC" != "gcc"; then
         if test "$enable_hpux_portable" = yes; then
            SYS_DEFS="${SYS_DEFS} +DAportable"
         fi
      fi
      ;;
   *ibm-aix*)
      SYS_DEFS="-D_ALL_SOURCE"
      ;;
   powerpc-ibm-os400)
      ;;
   *ibm-OS390*)
      SYS_DEFS="-D_ALL_SOURCE -DOS390"
      ;;
   *dec-osf4*)
      SYS_DEFS="-D_XOPEN_SOURCE_EXTENDED"
      AC_DEFINE(HAVE_BROKEN_SYS_ACL_H)
      ;;
   *dec-osf*)
      ;;
   *sequent-dynix*)
      ;;
   *solaris*)
      ;;
   sparc*sunos*)
      ;;
   *linux*)
      ;;
   *cygwin*)
      BASE_BINARY="cygwinbinary"
      on_cygwin="yes"
      ;;
   *sgi-irix*)
      SYS_DEFS="-DSGI_MPROT_T_UNDEFINED"
      ;;
   *pc-sco*)
      SYS_DEFS="-UM_XENIX -b elf"
      ;;
   *nto-qnx*)
      ;;
   *qnx*)
      on_qnx=yes
      SYS_DEFS="-Q"
      EEXTRA="-mf -N0x20000 -Q"
      SIMPLE_CFLAGS=yes
      ;;
   *beos*)
      if test "$prefix" != "/usr"; then
         prefix=/boot/home/config
      fi
      ;;
   *apple-darwin*)
# sinclude(common/inclibarch.m4) MUST have been included before here
      SYS_DEFS="$valid_arch_flags"
      EEXTRA="$valid_arch_flags -flat_namespace -undefined suppress"
      on_macos="yes"
      ;;
   *darwin*)
      EEXTRA="-flat_namespace -undefined suppress"
      ;;
esac
AC_SUBST(EEXTRA)
AC_SUBST(BASE_BINARY)
AC_SUBST(BASE_INSTALL)

MH_GET_KERNEL_NAME

dnl ---------------------- check for '--with-dist' switch -----------------
AC_ARG_WITH(dist,
   [  --with-dist             specify this just to build a distribution],
   [with_dist=$withval],
   [with_dist=no],
)

dnl ---------------------- check for Curses argument -----------------
dnl allow --with-curses to link with various curses libraries
AC_ARG_WITH(curses,
   [  --with-curses=lib       where lib is one of: ncurses, ncursesw, pdcurses-x11, pdcurses-x11w, pdcurses-sdl1, pdcurses-sdl1w, pdcurses-sdl2, pdcurses-sdl2w, extcurses],
   [with_curses=$withval],
   [with_curses=no],
)

dnl ---------------------- check for '--with-docdir' switch -----------------
AC_ARG_WITH(docdir,
   [  --with-docdir           specify location of document files],
   [with_docdir=$withval],
   [with_docdir=no],
)
if test "$with_docdir" == no; then
   docdir="\$(prefix)/share/doc/the-doc"
else
   docdir="$with_docdir"
fi
AC_SUBST(docdir)


dnl --------------- allow --enable-utf8 to allow UTF-8 files --------
#AC_ARG_ENABLE(utf8,
#   [  --enable-utf8           enable UTF8 support],
#   [with_utf8=$enableval],
#   [with_utf8=no],
#)

dnl
dnl Check lots of things specific to AIX
dnl
dnl Check that the OS supports extended curses
dnl Only supporting it on AIX 3.x
dnl
if test "$with_curses" = "extcurses" ; then
   case "$target" in
   *aix3*)
      ;;
   *)
      AC_MSG_ERROR(Extended Curses support only available on AIX 3.x; THE cannot be configured)
      ;;
   esac
fi

if test "$with_curses" = no; then
   case "$target" in
   *ibm-aix4.3*)
      SYS_DEFS="${SYS_DEFS} -DHAVE_BROKEN_COLORS"
      ;;
   *)
      ;;
   esac
fi

dnl allow --with-cursesincdir to specify where Curses header file is
AC_ARG_WITH(cursesincdir,
   [  --with-cursesincdir=DIR specify where curses header file is],
   [with_cursesincdir=$withval],
   [with_cursesincdir=no],
)
dnl allow --with-curseslibdir to specify where Curses library file is
AC_ARG_WITH(curseslibdir,
   [  --with-curseslibdir=DIR specify where curses library file is],
   [with_curseslibdir=$withval],
   [with_curseslibdir=no],
)
if test $with_cursesincdir = no; then
   CURSES_INC_DIR_BUILD=""
else
   CURSES_INC_DIR_BUILD="--with-cursesincdir=$with_cursesincdir"
fi
if test $with_curseslibdir = no; then
   CURSES_LIB_DIR_BUILD=""
else
   CURSES_LIB_DIR_BUILD="--with-curseslibdir=$with_curseslibdir"
fi
AC_SUBST(CURSES_INC_DIR_BUILD)
AC_SUBST(CURSES_LIB_DIR_BUILD)
dnl _XOPEN_SOURCE_EXTENDED needed for ncursesw
dnl if test "$enable_utf8" = yes; then
dnl    SYS_DEFS="${SYS_DEFS} -D_XOPEN_SOURCE_EXTENDED"
dnl fi
AC_SUBST(SYS_DEFS)

dnl
dnl check for only 1 curses library specified
dnl

THEBIN=""
THETYPE="$with_curses"
THECURSESTYPE=""
WIDE_CHAR=""
case "$with_curses" in
   ncurses)
      THEBIN="the-con"
      THECURSESTYPE="con"
      ;;
   ncursesw)
      THEBIN="the-conw"
      WIDE_CHAR="-DUSE_UTF8 -DUSE_WIDE_CHAR"
      MH_XTRA_OBJS="$MH_XTRA_OBJS utf8.o"
      THECURSESTYPE="conw"
      ;;
   pdcurses-x11)
      THEBIN="the-x11"
      MH_FIND_XTERM
      AC_DEFINE(USE_XCURSES)
      THECURSESTYPE="x11"
      ;;
   pdcurses-x11w)
      THEBIN="the-x11w"
      MH_FIND_XTERM
      AC_DEFINE(USE_XCURSES)
      WIDE_CHAR="-DUSE_UTF8 -DUSE_WIDE_CHAR"
      MH_XTRA_OBJS="$MH_XTRA_OBJS utf8.o"
      THECURSESTYPE="x11w"
      ;;
   pdcurses-sdl1)
      THEBIN="the-sdl1"
      MH_FIND_XTERM
      AC_DEFINE(USE_SDLCURSES)
      THECURSESTYPE="sdl1"
      ;;
   pdcurses-sdl1w)
      THEBIN="the-sdl1w"
      MH_FIND_XTERM
      AC_DEFINE(USE_SDLCURSES)
      WIDE_CHAR="-DUSE_UTF8 -DUSE_WIDE_CHAR"
      MH_XTRA_OBJS="$MH_XTRA_OBJS utf8.o"
      THECURSESTYPE="sdl1w"
      ;;
   pdcurses-sdl2)
      THEBIN="the-sdl2"
      MH_FIND_XTERM
      AC_DEFINE(USE_SDLCURSES)
      THECURSESTYPE="sdl2"
      ;;
   pdcurses-sdl2w)
      THEBIN="the-sdl2w"
      MH_FIND_XTERM
      AC_DEFINE(USE_SDLCURSES)
      WIDE_CHAR="-DUSE_UTF8 -DUSE_WIDE_CHAR"
      MH_XTRA_OBJS="$MH_XTRA_OBJS utf8.o"
      THECURSESTYPE="sdl2w"
      ;;
   extcurs)
      THEBIN="the-con"
      THECURSESTYPE="extcurs"
      ;;
   *)
      AC_MSG_ERROR(--with-curses=$with_curses does not specify a valid curses platform; THE cannot be configured)
esac
AC_SUBST(THEBIN)
AC_SUBST(THETYPE)
AC_SUBST(THECURSESTYPE)
AC_SUBST(WIDE_CHAR)
checkcurses="yes"
checkcursesfuncs="yes"
#MH_CHECK_CURSES()


dnl
dnl if specifying the include dir for curses, #define LOCAL_CURSES
dnl and symbolically link curses.h in that directory to ./local_curses.h
dnl

#if test "$with_cursesincdir" = yes; then
#   AC_DEFINE(LOCAL_CURSES)
#   dummy=`ln -sf $with_cursesincdir/curses.h ./curses_local.h`
#fi

dnl
dnl include Rexx support
dnl
sinclude(common/increxx.m4)

dnl ---------------------- check for Xaw3d library -------------------
dnl allow --with-xaw3d to link with XCurses
AC_ARG_WITH(xaw3d,
   [  --with-xaw3d            link with Xaw3d],
   [with_xaw3d=$withval],
   [with_xaw3d=no],
)

dnl ---------------------- check for neXtaw library -------------------
dnl allow --with-nextaw to link with XCurses
AC_ARG_WITH(nextaw,
   [  --with-nextaw           link with neXtaw],
   [with_nextaw=$withval],
   [with_nextaw=no],
)

dnl ---------------------- check for Checker -------------------------
dnl allow --with-checker to use Checker
AC_ARG_WITH(checker,
   [  --with-checker          build THE with Checker],
   [with_checker=$withval],
   [with_checker=no],
)

dnl ---------------------- check packages -----------------------
dnl Check for general libraries. MUST be before MH_CHECK_CURSES
dnl
MH_LIBS_TO_CHECK="m s socket nls"
MH_CHECK_LIB(${MH_LIBS_TO_CHECK})
AC_SUBST(MH_EXTRA_LIBS)

dnl ---------------------- check Curses -------------------------
dnl don't check for Curses or Rexx arguments if only building for distribution
if test "$checkcurses" = "yes"; then
   MH_CHECK_CURSES()
fi
dnl ---------------------- check packages -----------------------
dnl Check for X libraries if using XCurses and we don't have xcurses-config
dnl MUST be after MH_CHECK_CURSES
dnl
dnl MH_XLIBS=""
dnl MH_XINC_DIR=""
dnl if test "$with_xcurses" = yes; then
dnl    if test "$ac_cv_prog_xcurses_config" = yes; then
dnl       MX_XINC_DIR=`xcurses-config --cflags`
dnl       MH_XLIBS=`xcurses-config --libs`
dnl    else
dnl       MH_CHECK_X_LIB
dnl    fi
dnl fi
dnl AC_SUBST(MH_XLIBS)
dnl AC_SUBST(MH_XINC_DIR)

dnl --------------------- Check for other programs. ---------------
AC_PROG_MAKE_SET
MH_CHECK_CHECKER

dnl -------------------- Checks for header files. -----------------
AC_HEADER_STDC
AC_CHECK_HEADERS( \
   errno.h \
   alloca.h \
   ctype.h \
   wctype.h \
   sys/types.h \
   sys/mode.h \
   sys/stat.h \
   sys/socket.h \
   sys/file.h \
   sys/wait.h \
   sys/acl.h \
   sys/time.h \
   sys/select.h \
   select.h \
   unistd.h \
   stdlib.h \
   memory.h \
   fcntl.h \
   locale.h \
   string.h \
   stdarg.h \
   stdint.h \
   inttypes.h \
   stat.h \
   dirent.h \
   time.h
   )

MH_HAVE_PROTO

dnl Checks for typedefs, structures, and compiler characteristics.
if test "$ac_cv_prog_CC" != "g++"; then
   AC_C_CONST
fi
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM
MH_C_LONG_LONG

dnl Checks for library functions.
AC_FUNC_MEMCMP
AC_FUNC_ALLOCA
AC_TYPE_SIGNAL
MH_CHECK__SIGHANDLER_T
MH_FUNC_ACL_GET
MH_CHECK_FUNCS(rename lstat readlink getacl chown strcasecmp strcmpi stricmp mkfifo select sigaction siginterrupt setlocale realpath fork)

MH_CHECK_CHTYPE
if test "$checkcursesfuncs" = "yes"; then
   MH_CHECK_BROKEN_SYSVR4_CURSES
   MH_CURSES_FUNCS
fi

dnl
dnl include our debugging switch checking
dnl
sinclude(common/incdebug.m4)

dnl --------------- allow --enable-bounds-checking to compile in debug mode --------
AC_ARG_ENABLE(bounds-checking,
   [  --enable-bounds-checking turn on bounds checking],
   [with_bounds_checking=$enableval],
   [with_bounds_checking=no],
)

if test "$with_bounds_checking" = yes; then
   CFLAGS="${CFLAGS} -fbounds-check -D__BOUNDS_CHECKING_ON"
fi

dnl --------------- allow --enable-trace to log trace information --------
AC_ARG_ENABLE(trace,
   [  --enable-trace          turn on tracing],
   [with_trace=$enableval;AC_DEFINE(THE_TRACE)],
   [with_trace=no]
)
if test "$with_trace" = yes; then
   MH_XTRA_OBJS="$MH_XTRA_OBJS trace.o"
fi

AC_SUBST(MH_XTRA_OBJS)

dnl --------------- allow --with-htmlext to specify file extension of HTML files
AC_ARG_WITH(htmlext,
   [  --with-htmlext          specify extension of HTML files],
   [HTML_EXT=.$withval],
   [HTML_EXT=.html],
)
AC_SUBST(HTML_EXT)

dnl --------------- allow --with-global-profile to specify a global profile file
AC_ARG_WITH(global-profile,
   [  --with-global-profile   specify name of global profile file],
   [THE_GLOBAL_PROFILE="$withval"],
   [THE_GLOBAL_PROFILE=""],
)
if test x"$THE_GLOBAL_PROFILE" != x; then
  AC_DEFINE_UNQUOTED(THE_GLOBAL_PROFILE,"$THE_GLOBAL_PROFILE")
fi

dnl
dnl include our check for RPM
dnl
MH_CHECK_PACKAGE_BUILDERS()
MH_GET_DISTRO_NAME()

dnl --------------- Force definition of pre-compiler macro UNIX ----------
AC_DEFINE(UNIX)

dnl --------------- Force definition of this build number ----------------
AC_DEFINE(BUILD3001)


dnl --------------- Get us an install package ----------------------------
AC_PROG_INSTALL

thisdir="`pwd`"
AC_SUBST(thisdir)

dnl ---- leave the setting of CC to last
if test "$mh_cv_checker" = yes; then
   CC="checkergcc"
   AC_DEFINE(CC)
fi

AC_OUTPUT(Makefile APKBUILD haiku/PackageInfo haiku/PackageInfo-variant haiku/PackageInfo-doc)

dnl
dnl include our status information
dnl
if test "$with_dist" = no; then
   MH_SHOW_STATUS( MH_SHOW_THE, "notrailer" )
   echo "To build the '$THEBIN' executable, text and normal HTML documentation; 'make'"
   echo "then to install THE in the above directories; 'make install'"
   echo
   echo "Individual portions of THE can be built as follows:"
   echo "- To build the 'the' executable; 'make the'"
   echo "- To build a text reference manual; 'make the.man'"
   echo "- To build the help file; THE_Help.txt; 'make THE_Help.txt'"
   echo "- To build the normal HTML reference manual; 'make html'"
   echo "- To build a HTML reference manual in one file; 'make combined'"
   echo "- To build a HTML reference manual in separate files; 'make helpviewer'"
   echo "- To build a PDF reference manual; 'make pdf' - this requires htmldoc"
   echo
else
   echo ""
   echo "Source distributions (zip and dist) can now be made for $MH_PACKAGE"
   echo ""
   rm -f config.h
fi
