dnl	This file is an input file used by the GNU "autoconf" program to
dnl	generate the file "configure", which is run during Tk installation
dnl	to configure the system for the local environment.
AC_INIT(neoXgeneral.c)
# $Id: configure.in,v 1.10 1999/07/08 17:20:37 damon Exp $

AC_CONFIG_HEADER(neoXconfig.h)

NEO_VERSION=8.1.1
NEO_MAJOR_VERSION=8
NEO_MINOR_VERSION=1
VERSION=${NEO_VERSION}

dnl	Change the DB_VERSION below if you go to a new version of db.
DB_VERSION=2.7

TCLX_DIR=tclX8.1.1
ITCL_DIR=itcl3.0.1

if test "${prefix}" = "NONE"; then
    prefix=/usr/local
fi
if test "${exec_prefix}" = "NONE"; then
    exec_prefix=$prefix
fi

AC_ARG_ENABLE(gcc, [  --enable-gcc            allow use of gcc if available],
    [neo_ok=$enableval], [neo_ok=no])
if test "$neo_ok" = "yes"; then
    AC_PROG_CC
else
    CC=${CC-cc}
    AC_SUBST(CC)
fi
AC_PROG_CPP

#------------------------------------------------------------------------------
#	Process optional package args into PKG_CFLAGS, PKG_LDFLAGS, etc -- Randy
#------------------------------------------------------------------------------

# Note: NEO_DB is on by default
AC_ARG_WITH(db, [  --with-db[=prefix of db lib,include]  enable Tcl db interface via neoXdb.c],
	neo_db=$withval, neo_db=yes)

dbbuild="no"
dbdir=../tools/db-2.7.5/build_unix

if test "x$neo_db" != "xno"
then
  
  pkg_cflags="$pkg_cflags -DNEO_DB"
  pkg_optobj="$pkg_optobj neoXdb.o"

# search for dbpath based on withval

  case $neo_db in
  yes)
	dbbuild=yes
	dbtarget='$dbdir/${dblibname}'
	dbinclude="-I$dbdir"
	;;
  system)
	AC_CHECK_LIB(db, db_open, [
		dblibflags="-ldb"
		dbinclude=""
	], [ AC_MSG_WARN([You do not have a system-supplied db_open, build ours])dnl
		dbbuild=yes
		dbtarget='$dbdir/${dblibname}'
		dbinclude="-I$dbdir"
	])dnl
	;;
  *)	dblibflags="-L$neo_db/lib -ldb"
	dbinclude="-I$neo_db/include"
	;;
  esac


fi

# Note: NEO_LDAP is off by default
AC_ARG_WITH(ldap, [  --with-ldap[=dir-of-ldap.a]  enable interface via neoXldap.c],
	[neo_ldap=$withval
	case $withval in
	    yes) ldapdir=/usr/local
		 ;;
	    no)  ;;
	    *)	 ldapdir=$withval
		 neo_ldap=yes
		 ;;
	esac
	], [
	    neo_ldap=yes
	    ldapdir=/usr/local
	])

ldapbuild="no"

if test "x$neo_ldap" != "xno"
then

  _CFLAGS="$CFLAGS"
  _LIBS="$LIBS"
  LIBS="$LIBS -L$ldapdir/lib -lldap -llber"
  CFLAGS="$CFLAGS -I$ldapdir/include"
  AC_CHECK_LIB(ldap, ldap_open, [
	ldaplibflags="-L$ldapdir/lib -lldap -llber"
	ldapinclude="-I$ldapdir/include"
	ldapbuild=yes
  ], [ AC_WARN([You do not appear to have the LDAP package installed])dnl
	    ldapbuild=no
  ])dnl
  CFLAGS="$_CFLAGS"
  LIBS="$_LIBS"
  
  if test "$ldapbuild" = yes
  then
    pkg_cflags="$pkg_cflags -DNEO_LDAP"
    pkg_optobj="$pkg_optobj neoXldap.o"
  fi
fi

AC_SUBST(ldaplibflags)
AC_SUBST(ldapinclude)
AC_SUBST(ldapbuild)
AC_SUBST(ldapdir)

# Note: ENCRYPTION on by default
AC_ARG_WITH(encryption,
	[  --without-encryption      disable NeoSoft Encrption])
if test "$withval" != "no"
then
	pkg_cflags="$pkg_cflags -DNEOSOFT_TCL_ENCRYPTION"
	AC_CHECK_LIB(crypt, crypt, [LIBS="$LIBS -lcrypt"])
fi
rm -f neoXcrypt.o

# Note: CUTE on by default
AC_ARG_WITH(cute,
	[  --without-cute            do not include CUTE], , with_cute=no)
if test "$with_cute" != "no"
then
	AC_CHECK_FUNC(cfsetspeed, , AC_CHECK_LIB(bsd, cfsetspeed,
		[BSDLIB="-lbsd"; echo "-lbsd resolves cfsetspeed"],
		[AC_MSG_WARN(disabling cute cmd: needs cfsetspeed() and I couldn't find it)]))
	if test "$ac_cv_func_cfsetspeed" = "yes"
	then
	    pkg_cflags="$pkg_cflags -DCUTE"
	    pkg_optobj="$pkg_optobj neoXcute.o"
	fi
fi
AC_SUBST(BSDLIB)

AC_ARG_ENABLE(gcc, [  --enable-gcc            allow use of gcc if available],
    [neo_ok=$enableval], [neo_ok=no])
if test "$neo_ok" = "yes"; then
    AC_PROG_CC
else
    CC=${CC-cc}
AC_SUBST(CC)
fi

AC_SUBST(pkg_cflags)
AC_SUBST(pkg_ldflags)
AC_SUBST(pkg_libs)
AC_SUBST(pkg_optobj)

AC_PROG_INSTALL(install-sh)
AC_PROG_RANLIB
AC_HAVE_HEADERS(unistd.h stdlib.h limits.h crypt.h sys/time.h)

#--------------------------------------------------------------------
#	Supply a substitute for stdlib.h if it doesn't define strtol,
#	strtoul, or strtod (which it doesn't in some versions of SunOS).
#--------------------------------------------------------------------

AC_CHECKING(stdlib.h)
AC_HEADER_EGREP(strtol, stdlib.h, tk_ok=yes, tk_ok=no)
AC_HEADER_EGREP(strtoul, stdlib.h, , tk_ok=no)
AC_HEADER_EGREP(strtod, stdlib.h, , tk_ok=no)
if test $tk_ok = no; then
    AC_DEFINE(NO_STDLIB_H)
fi
AC_MSG_RESULT($tk_ok)

#--------------------------------------------------------------------
#	Check for various typedefs and provide substitutes if
#	they don't exist.
#--------------------------------------------------------------------

AC_MODE_T
AC_PID_T
AC_SIZE_T
AC_UID_T


AC_HEADER_CHECK(termio.h, AC_DEFINE(HAVE_TERMIO_H), )
AC_HEADER_CHECK(termios.h, AC_DEFINE(HAVE_TERMIOS_H), )
AC_HEADER_CHECK(limits.h, AC_DEFINE(HAVE_LIMITS_H), )



#--------------------------------------------------------------------
#	If this system doesn't have a memmove procedure, use memcpy
#	instead.
#--------------------------------------------------------------------

AC_CHECK_FUNC(memmove, , [AC_DEFINE(memmove, memcpy)])

#--------------------------------------------------------------------
#	Figure out whether "char" is unsigned.  If so, set a
#	#define for __CHAR_UNSIGNED__.
#--------------------------------------------------------------------

AC_C_CHAR_UNSIGNED

#--------------------------------------------------------------------
#	See if there was a command-line option for where Tcl is;  if
#	not, assume that its top-level directory is a sibling of ours.
#--------------------------------------------------------------------

AC_ARG_WITH(tcl, [  --with-tcl=DIR          use Tcl 8.1.1 binaries from DIR],
	TCL_BIN_DIR=$withval, TCL_BIN_DIR=`cd ../../tcl8.1.1/unix; pwd`)
if test ! -d $TCL_BIN_DIR; then
    AC_MSG_ERROR(Tcl directory $TCL_BIN_DIR doesn't exist)
fi
TCL_SRC_DIR=`cd $TCL_BIN_DIR/..; pwd`

#--------------------------------------------------------------------
#	See if there was a command-line option for where Tk is;  if
#	not, assume that its top-level directory is a sibling of ours.
#--------------------------------------------------------------------

AC_ARG_WITH(tk, [  --with-tk=DIR          use Tk 8.1.1 binaries from DIR],
	, with_tk=yes)

case "$with_tk" in
    yes) TK_BIN_DIR=../../tk8.1.1/unix
	if test ! -d "$TK_BIN_DIR"
	then
	    AC_MSG_ERROR(Parallel Tk directory $TK_BIN_DIR does not exist)
	fi
	;;
    no) ;;
    *)  if test -d "$with_tk"
	then
	    TK_BIN_DIR=`cd $with_tk; pwd`
	else
	    AC_MSG_ERROR(Tk build directory $TK_BIN_DIR does not exist)
	fi
	;;
esac

AC_ARG_WITH(x, [   --without-x            do not build/install neowish])
if test "$with_x" = "no"
then
    with_tk=no
fi

if test "$with_tk" != "no"
then
    TK_SRC_DIR=`cd $TK_BIN_DIR/..; pwd`
    . $TK_SRC_DIR/unix/tkConfig.sh
    NEOWISH=neowish
fi
AC_SUBST(TK_LIBS)
AC_SUBST(TK_BUILD_LIB_SPEC)
AC_SUBST(TK_XINCLUDES)
AC_SUBST(TK_SRC_DIR)
AC_SUBST(TK_VERSION)
AC_SUBST(NEOWISH)

#--------------------------------------------------------------------
#	See if there was a command-line option for where TclX is;  if
#	not, assume that its top-level directory is a sibling of ours.
#--------------------------------------------------------------------

AC_ARG_WITH(tclx, [  --with-tclx=DIR          use Tclx binaries from DIR],
	TCLX_TCL_DIR=$withval, TCLX_TCL_DIR=`cd ../../$TCLX_DIR/tcl/unix; pwd`)
if test ! -d $TCLX_TCL_DIR; then
    AC_MSG_ERROR(TclX directory $TCLX_TCL_DIR doesn't exist)
fi
TCLX_TOP_DIR=`cd $TCLX_TCL_DIR/../..; pwd`
. $TCLX_TCL_DIR/tclxConfig.sh

if test "$with_tk" != "no"
then
    . $TCLX_TOP_DIR/tk/unix/tkxConfig.sh
fi
AC_SUBST(TKX_BUILD_LIB_SPEC)

#--------------------------------------------------------------------
#	See if there was a command-line option for where TclX is;  if
#	not, assume that its top-level directory is a sibling of ours.
#--------------------------------------------------------------------

AC_CHECKING(whether building neotcl along side Itcl)
AC_ARG_WITH(itcl, [  --with-itcl=DIR          use Itcl binaries from DIR],
	ITCL_UNIX_DIR=$withval, ITCL_UNIX_DIR=default)
if test "$ITCL_UNIX_DIR" = default
then
    if test -d ../../$ITCL_DIR/unix
    then
	ITCL_UNIX_DIR=yes
    else
	ITCL_UNIX_DIR=no
    fi
fi
if test "$ITCL_UNIX_DIR" = yes 
then
    ITCL_UNIX_DIR=`cd ../../$ITCL_DIR/unix; pwd`
fi
if test "$ITCL_UNIX_DIR" != no
    then
    if test ! -d "$ITCL_UNIX_DIR"; then
	AC_MSG_ERROR(Itcl directory $ITCL_UNIX_DIR doesn't exist)
    else
	. $ITCL_UNIX_DIR/itclConfig.sh
	CFLAGS="$CFLAGS -DUSE_ITCL"
	AC_MSG_RESULT([yes, $ITCL_VERSION])
    fi
else
    AC_MSG_RESULT(no)
fi

#--------------------------------------------------------------------
#	Read in configuration information generated by Tcl for shared
#	libraries, and arrange for it to be substituted into our
#	Makefile.
#--------------------------------------------------------------------

file=$TCL_BIN_DIR/tclConfig.sh
. $file
CC=$TCL_CC
SHLIB_CFLAGS=$TCL_SHLIB_CFLAGS
SHLIB_LD=$TCL_SHLIB_LD
SHLIB_LD_LIBS=$TCL_SHLIB_LD_LIBS
SHLIB_SUFFIX=$TCL_SHLIB_SUFFIX
SHLIB_VERSION=$TCL_SHLIB_VERSION
DL_LIBS=$TCL_DL_LIBS
LD_FLAGS=$TCL_LD_FLAGS
NEO_LD_SEARCH_FLAGS=$TCL_LD_SEARCH_FLAGS

eval "NEO_SHARED_LIB_FILE=libneo${TCL_SHARED_LIB_SUFFIX}"
eval "NEO_UNSHARED_LIB_FILE=libneo${TCL_UNSHARED_LIB_SUFFIX}"

#--------------------------------------------------------------------
#	The statements below define a collection of symbols related to
#	building libneo as a shared library instead of a static library.
#--------------------------------------------------------------------

# Warning: in order to use the following code for libneo and libdb versions,
# the VERSION shell variable is modified, and then is restored after.
VERSION=${NEO_VERSION}

AC_ARG_ENABLE(shared,
    [  --enable-shared         build libneo as a shared library],
    [ok=$enableval], [ok=no])
if test "$ok" = "yes" -a "${SHLIB_SUFFIX}" != ""; then
    NEO_SHLIB_CFLAGS="${SHLIB_CFLAGS}"
    eval "NEO_LIB_FILE=libneo${TCL_SHARED_LIB_SUFFIX}"
    MAKE_LIB="\${SHLIB_LD} $TCL_LIB_HNAME -o ${NEO_LIB_FILE} \${OBJS} \${LIBS}"
    VERSION=""; eval "dblibname=libdb${TCL_SHARED_LIB_SUFFIX}"
    RANLIB=":"

else
    NEO_SHLIB_CFLAGS=""
    eval "NEO_LIB_FILE=libneo${TCL_UNSHARED_LIB_SUFFIX}"
    MAKE_LIB="ar cr ${NEO_LIB_FILE} \${OBJS} \${LIBS}"
    VERSION=""; eval "dblibname=libdb${TCL_UNSHARED_LIB_SUFFIX}"
fi

eval "DBLINK=\"@ln -f \\\$(prefix)/BerkeleyDB/lib/$dblibname \\\$(prefix)/lib\""

AC_ARG_ENABLE(dump185,
    [  --enable-dump185         build db_dump185 with the db build],
    [dump185=$enableval], [dump185=no])


AC_SUBST(DBLINK)
if test "$dbbuild" = "yes"; then
    if test "${TCL_LIB_VERSIONS_OK}" = "ok"; then
	dblibflags="-L${dbdir} -ldb"
	DB_LIB_SPEC="-L${exec_prefix}/lib -ldb"
    else
	dblibflags="-L${dbdir} -ldb"
	DB_LIB_SPEC="-L${exec_prefix}/lib -ldb"
    fi
fi

eval "dbtarget=$dbtarget"
AC_SUBST(dblibname)
AC_SUBST(dblibflags)
AC_SUBST(dbinclude)
AC_SUBST(dbbuild)
AC_SUBST(dbtarget)
AC_SUBST(dbdir)
AC_SUBST(DB_LIB_SPEC)

VERSION=${NEO_VERSION}
# Note:  in the following variable, it's important to use the absolute
# path name of the Tcl directory rather than "..":  this is because
# AIX remembers this path and will attempt to use it at run-time to look
# up the Tcl library.

if test "${TCL_LIB_VERSIONS_OK}" = "ok"; then
    NEO_BUILD_LIB_SPEC="-L`pwd` -lneo${VERSION}"
    NEO_LIB_SPEC="-L${exec_prefix}/lib -lneo${VERSION}"
else
    NEO_BUILD_LIB_SPEC="-L`pwd` -lneo`echo ${VERSION} | tr -d .`"
    NEO_LIB_SPEC="-L${exec_prefix}/lib -lneo`echo ${VERSION} | tr -d .`"
fi

AC_SUBST(CC)
AC_SUBST(LIBS)
AC_SUBST(DL_LIBS)
AC_SUBST(LD_FLAGS)
AC_SUBST(MATH_LIBS)
AC_SUBST(MAKE_LIB)
AC_SUBST(SHLIB_CFLAGS)
AC_SUBST(SHLIB_LD)
AC_SUBST(SHLIB_LD_LIBS)
AC_SUBST(SHLIB_SUFFIX)
AC_SUBST(SHLIB_VERSION)
AC_SUBST(TCLX_TOP_DIR)
AC_SUBST(TCLX_TCL_DIR)
AC_SUBST(TCLX_BUILD_LIB_SPEC)
AC_SUBST(ITCL_BUILD_LIB_SPEC)
AC_SUBST(TCL_LIBS)
AC_SUBST(TCL_SRC_DIR)
AC_SUBST(TCL_BIN_DIR)
AC_SUBST(TCL_BUILD_LIB_SPEC)
AC_SUBST(TCL_LD_SEARCH_FLAGS)
AC_SUBST(TCL_LIB_HNAME)
AC_SUBST(TCL_SRC_DIR)
AC_SUBST(TCL_VERSION)
AC_SUBST(NEO_BUILD_LIB_SPEC)
AC_SUBST(NEO_LD_SEARCH_FLAGS)
AC_SUBST(NEO_SHARED_LIB_FILE)
AC_SUBST(NEO_UNSHARED_LIB_FILE)
AC_SUBST(NEO_LIB_FILE)
AC_SUBST(NEO_LIB_SPEC)
AC_SUBST(NEO_MAJOR_VERSION)
AC_SUBST(NEO_MINOR_VERSION)
AC_SUBST(NEO_SHLIB_CFLAGS)
AC_SUBST(NEO_VERSION)
dnl AC_SUBST(XINCLUDES)
dnl AC_SUBST(XLIBSW)

AC_OUTPUT(Makefile neoConfig.sh pkgIndex.tcl)

if test "$dbbuild" = "yes"
then
  echo "Running configure in $dbdir"
  echo "cd $dbdir; ../dist/configure --prefix=$prefix --enable-shared=$enable_shared --enable-dump185 --enable-tclconfig=$TCL_BIN_DIR/tclConfig.sh"
  cd $dbdir; ../dist/configure --prefix=$prefix \
	--enable-shared=$enable_shared \
	--enable-dump185=$dump185 \
	--enable-tclconfig=$TCL_BIN_DIR/tclConfig.sh
fi
