dnl Process this file with autoconf to produce a configure script.
AC_INIT(editor/cooledit.c)

dnl Identify target.
AC_CANONICAL_SYSTEM
AC_DEFINE_UNQUOTED(TARGET_MACHINE, "${target_cpu}-${target_vendor}-${target_os}")

AM_INIT_AUTOMAKE(cooledit, 3.12.0)
AM_CONFIG_HEADER(config.h)

dnl Define package and version.
dnl PACKAGE=cooledit
dnl AC_SUBST(PACKAGE)
dnl AC_DEFINE_UNQUOTED(PACKAGE, "${PACKAGE}")
dnl VERSION=3.12.0
dnl AC_SUBST(VERSION)
dnl AC_DEFINE_UNQUOTED(VERSION, "${VERSION}")
echo
echo "Now configuring $PACKAGE-$VERSION"
echo


if test -n "`echo ${target_os} | grep sunos`" ; then
AC_DEFINE(CRASHES_ON_STARTUP,1)
fi

if test -n "`echo ${target_os} | grep 'powerpc.*linux'`" ; then
AC_DEFINE(CRASHES_ON_STARTUP,1)
fi

dnl For shared libraries
AM_PROG_LIBTOOL

dnl Checks for programs.
AC_PROG_MAKE_SET
AC_PROG_CC
AC_PROG_RANLIB
AC_ISC_POSIX

dnl Does man accept -a?
echo $ac_n "checking whether man accepts -a... "
man -a man 1>/dev/null 2>conftestman
if test -n "`cat conftestman | grep '[[Uu]]sage'`" -o \
		-n "`cat conftestman | grep '[[Ee]]rror'`" -o \
		-n "`cat conftestman | grep '[[Ii]]llegal'`" ; then 
	MAN_CMD="man %m" ;
	echo "no" ;
else
	MAN_CMD="man -a %m" ;
	echo "yes" ;
fi
rm -f conftestman
AC_DEFINE_UNQUOTED(MAN_CMD, "${MAN_CMD}")

dnl Do you have rxvt?
echo $ac_n "checking for rxvt... "
rxvt -help 1>/dev/null 2>conftestrxvt
if test -n "`cat conftestrxvt | grep 'geom'`" ; then
	XTERM_CMD="rxvt -fn %F -bg Navy -fg White " ;
	echo "yes" ;
else
	XTERM_CMD="xterm" ;
	echo "no" ;
fi
rm -f conftestrxvt
AC_DEFINE_UNQUOTED(XTERM_CMD, "${XTERM_CMD}")

dnl Send target name.
AC_ARG_ENABLE(mail-author,
	[--disable-mail-author     Don't automatically mail me on first run],[
	AC_DEFINE(DISABLE_AUTO_MAIL,1)
])

dnl Set of available languages.
ALL_LINGUAS="cs da de es fi fr it ja ko nl no pl pt ru sl sv"
AC_LINK_FILES($nls_cv_header_libgt, $nls_cv_header_intl)

dnl Checks for C compiler flags that X needs and the X linker flags
AC_PATH_XTRA
CFLAGS="$CFLAGS $X_CFLAGS"
LDFLAGS="$LDFLAGS $X_LIBS"
AC_SUBST(LDFLAGS)


dnl python stuff
PYLIB=python1.5
LIBPY=python1.5

have_python_config=no
have_python_include=no

AC_MSG_CHECKING(for python version 1.5)
EPATH=`echo $PATH | sed -e 's+/bin:+ +g' -e 's+:+ +g'`

dnl this should about cover everything  :-)  :-(

for i in / /opt /usr /usr/local /usr/ccs /usr/ucb /home $EPATH ; do
    for j in . gnu local python ; do
	for k in . gnu local python ; do
	    for l in lib . ; do
		if test -f $i/$j/$k/$l/$PYLIB/config/lib${LIBPY}.a ; then
		    PYTHON_LIBDIR=$i/$j/$k/$l/$PYLIB/config/
		    have_python_config=yes
		fi
	    done
	    if test -f $i/$j/$k/include/$PYLIB/Python.h ; then
		PYTHON_INCLUDEDIR=$i/$j/$k/include/$PYLIB/
		have_python_include=yes
	    fi
	done
    done
done

if test x$have_python_config = xyes && test x$have_python_include = xyes ; then
    have_python=yes
    AC_MSG_RESULT(yes)
    AC_DEFINE(HAVE_PYTHON,1)
    AC_CHECK_LIB(dl, dlopen,
EXTRA_PYTHON_LIBS=-ldl
    )
    AC_CHECK_LIB(dld, shl_load,
EXTRA_PYTHON_LIBS=-ldld
    )
    AC_MSG_CHECKING(python threads)
    PT=""
    if $NM ${PYTHON_LIBDIR}/lib${LIBPY}.a | grep PyThread_init_thread 2>&5 > /dev/null ; then
	AC_MSG_RESULT(yes)
	AC_CHECK_HEADER(mach/cthreads.h,, [
	    AC_CHECK_LIB(pthreads, pthread_create, [
		PT="-lpthreads"
	    ], [
		AC_CHECK_LIB(pthread, pthread_create, [
		    PT="-lpthread"
		], [
		    AC_CHECK_LIB(c_r, pthread_create, [
			PT="-lc_r"
		    ], [
			AC_CHECK_LIB(thread, __d6_pthread_create, [
			    PT="-lthread"
			])
		    ])])])])
	AC_CHECK_LIB(mpc, usconfig, [
	    PT="$PT -lmpc"
	])
	AC_CHECK_LIB(thread, thr_create, [
	    PT="$PT -lthread"
	])
    else
	AC_MSG_RESULT(no)
    fi
    PYTHON_LIBS="-L$PYTHON_LIBDIR -lm $PT $EXTRA_PYTHON_LIBS -l$LIBPY"
    PYTHON_INCLUDES="-I$PYTHON_INCLUDEDIR"
else
    AC_MSG_RESULT(no)
    PYTHON_LIBS=""
    PYTHON_INCLUDES=""
fi
AC_SUBST(PYTHON_LIBS)
AC_SUBST(PYTHON_INCLUDES)

dnl python stuff done

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
SET_CC_WARNINGS
AC_PROG_INSTALL
AC_CHECK_HEADERS(fcntl.h sys/fcntl.h sys/ioctl.h sys/time.h unistd.h sys/select.h sys/errno.h alloca.h grp.h pwd.h)

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

AC_CHECK_LIB(intl, tolower)

dnl Checks for library functions.
AC_FUNC_ALLOCA
AC_TYPE_SIGNAL
AC_FUNC_VPRINTF
dnl
dnl autoscan wants to check for a select function. How can the piping work without select?
dnl
AC_CHECK_FUNCS(strerror getcwd re_comp regcomp strdup strspn strstr strcspn)
AC_CHECK_FUNCS(strchr strrchr memmove memcpy memset memchr memcmp)
AC_CHECK_FUNCS(strcasecmp strncasecmp strftime alloca)

AC_MSG_CHECKING(for --with-libs)
AC_ARG_WITH(libs, [  --with-libs='lib1 ...'  link against additional libs], [
AC_MSG_RESULT($withval)
LIBS="$withval $LIBS"
], AC_MSG_RESULT(no))

AC_ARG_PROGRAM

dnl internationalization macros
PACKAGE_FORCE_USE_INCLUDED_GETTEXT=yes
AM_GNU_GETTEXT
AC_OUTPUT([Makefile widget/Makefile editor/Makefile icon/Makefile intl/Makefile \
	po/Makefile.in pixmap/Makefile lib/Makefile syntax/Makefile man/Makefile \
	intl/intlh.inst], \
	[sed -e "/POTFILES =/r po/POTFILES" po/Makefile.in > po/Makefile])

if test x$have_python != xyes ; then
    echo ""
    echo ""
    echo "The Python.h header file or lib$LIBPY.a library file could not be found."
    echo "This could mean that Python is not installed on your system."
    echo "The complete Python sources can be downloaded from http://www.python.org/"
    echo "Cooledit will work without Python, but will not be custom programmable."
    echo "and will lack any features coded in Python."
    echo ""
fi
echo ""
echo "Configuration done."
echo "Type 'make' to compile $PACKAGE-$VERSION"
echo ""

