
# This is free software, licensed under the GNU Public License V2.
# See the file COPYING for details.

dnl Process this file with autoconf to produce a configure script.
AC_INIT(include/pi-cmp.h)
AC_CONFIG_AUX_DIR(scripts)


dnl Check host type
AC_CANONICAL_HOST


dnl Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AM_PROG_LIBTOOL
AC_PROG_RANLIB
AC_PROG_YACC

WITHTCL="WITHOUTTCL"
WITHCXX="WITHOUTCXX"
WITHTK="WITHOUTTK"
WITHPERL5="WITHOUTPERL5"
WITHPYTHON="WITHOUTPYTHON"
WITHJAVA="WITHOUTJAVA"

AC_ARG_WITH(cpp,      [  --with-cpp[=c++ compiler]  use C++     [default=yes]])

if test "x$with_cpp" = "xno"; then
	CXX=
	ac_cv_prog_cxx_works=no
else

if test "x$with_cpp" = "xyes"; then
	with_cpp=""
fi

if test "x$with_cpp" = "x"; then
	AC_CHECK_PROGS(CXX,$CCC c++ g++ gcc CC cxx cc++, gcc)
else
	CXX=$with_cpp
fi

AC_LANG_SAVE
AC_LANG_CPLUSPLUS
AC_CACHE_CHECK([whether C++ compiler ($CXX $CXFLAGS $LDFLAGS) works],
  ac_cv_prog_cxx_works,
[AC_TRY_COMPILE([#include <stdio.h>], [class a { public: int b; a(void); };
a b; ],
  ac_cv_prog_cxx_works=yes,ac_cv_prog_cxx_works=no)])
AC_LANG_RESTORE

AC_PROG_CXX_GNU

if test $ac_cv_prog_gxx = yes; then
  GXX=yes
dnl Check whether -g works, even if CXXFLAGS is set, in case the package
dnl plays around with CXXFLAGS (such as to build both debugging and
dnl normal versions of a library), tasteless as that idea is.
  ac_test_CXXFLAGS="${CXXFLAGS+set}"
  ac_save_CXXFLAGS="$CXXFLAGS"
  CXXFLAGS=
  AC_PROG_CXX_G
  if test "$ac_test_CXXFLAGS" = set; then
    CXXFLAGS="$ac_save_CXXFLAGS"
  elif test $ac_cv_prog_cxx_g = yes; then
    CXXFLAGS="-g -O2"
  else
    CXXFLAGS="-O2"
  fi
else
  GXX=
  test "${CXXFLAGS+set}" = set || CXXFLAGS="-g"
fi

fi

ccexecs=''
cclib=''
libcclib=''
SUBMAKE_COMM='cd $(top_builddir)/libsock ; $(MAKE) '
SUBMAKE_COMM_DUBIOUS='cd $(top_builddir)/dubious ; $(MAKE) '
SUBMAKE_COMM_CC='-true'
EXT=''
CXXLIBS=''
if test $ac_cv_prog_cxx_works = yes; then
  ccexecs='$(CCEXECS)'
  cclib='libpicc.a'
  libcclib='libcc/libpicc.a'
  SUBMAKE_COMM_CC='cd $(top_builddir)/libcc ; $(MAKE) '
  WITHCXX='WITHCXX'
else
  CXX=''
  CXXFLAGS=''
fi
AC_SUBST(ccexecs)
AC_SUBST(cclib)
AC_SUBST(libcclib)
AC_SUBST(SUBMAKE_COMM)
AC_SUBST(SUBMAKE_COMM_DUBIOUS)
AC_SUBST(SUBMAKE_COMM_CC)
AC_SUBST(EXT)
AC_SUBST(CXXLIBS)


dnl Adjust linker

case "$host" in
	*sunos*)
		case "$LD" in 
			*/ucb/*) LIBS="-L/usr/ucblib -R/usr/ucblib $LIBS" ;;
		esac ;;
esac		

dnl Checks for libraries.


if test $ac_cv_prog_cxx_works = yes; then
  AC_LANG_SAVE
  AC_LANG_CPLUSPLUS
  if test $ac_cv_prog_gxx = yes; then
    AC_CHECK_LIB(g++, main, CXXLIBS="$CXXLIBS -lg++")
  fi
  AC_LANG_RESTORE
fi
AC_CHECK_LIB(m, ldexp, LIBS="$LIBS -lm")
AC_CHECK_LIB(PW, alloca, LIBS="$LIBS -lPW")

AC_ARG_WITH(readline21,      [  --with-readline21    use readline 2.1 [default=yes]])
AC_ARG_WITH(readline20,      [  --with-readline20    use readline 2.0 [default=yes]])

ac_save_LIBS="$LIBS"
LIBS="$LIBS -lreadline"
AC_TRY_LINK(,[(void)readline(0);], RLLIBS="-lreadline",
LIBS="$LIBS -ltermcap"
AC_TRY_LINK(,[(void)readline(0);], RLLIBS="-lreadline -ltermcap"))
LIBS="$ac_save_LIBS"

AC_MSG_CHECKING(for readline)
if test "x$with_readline21" = "x" || test "x$with_readline21" = "xyes"; then
	ac_save_LIBS="$LIBS"
	LIBS="$LIBS $RLLIBS"
	AC_TRY_LINK([#include <stdio.h>
	#include <readline/readline.h>], [rl_callback_read_char();], [RLDEFS="-DREADLINE_2_1"
		with_readline20='no'
		AC_MSG_RESULT(2.1)])
	LIBS="$ac_save_LIBS"
fi
if test "x$with_readline20" = "x" || test "x$with_readline20" = "xyes"; then
	ac_save_LIBS="$LIBS"
	LIBS="$LIBS $RLLIBS"
	AC_TRY_LINK([#include <stdio.h>
	#include <readline/readline.h>], [
extern void rl_deprep_terminal(void);
extern int rl_getc(FILE * stream);
extern void rl_gather_tyi(void);

rl_deprep_terminal();
(void)rl_getc(stdin);
rl_gather_tyi();], RLDEFS="-DREADLINE_2_0"; AC_MSG_RESULT(2.0))
	LIBS="$ac_save_LIBS"
fi

if test "x$RLDEFS" = "x"; then
	AC_MSG_RESULT(none)
fi

AC_SUBST(RLLIBS)
AC_SUBST(RLDEFS)


dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h malloc.h sys/ioctl.h sys/time.h sys/ioctl_compat.h memory.h string.h strings.h unistd.h stdlib.h netinet/in.h dirent.h sys/ndir.h sys/dir.h ndir.h sys/select.h sockio.h netdb.h sys/utsname.h)

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

dnl Find optional libraries (borrowed from Tcl)
tcl_checkBoth=0
AC_CHECK_FUNC(connect, tcl_checkSocket=0, tcl_checkSocket=1)
if test "$tcl_checkSocket" = 1; then
    AC_CHECK_LIB(socket, main, LIBS="$LIBS -lsocket", tcl_checkBoth=1)
fi
if test "$tcl_checkBoth" = 1; then
    tk_oldLibs=$LIBS
    LIBS="$LIBS -lsocket -lnsl"
    AC_CHECK_FUNC(accept, tcl_checkNsl=0, [LIBS=$tk_oldLibs])
fi
AC_CHECK_FUNC(gethostbyname, , AC_CHECK_LIB(nsl, main, [LIBS="$LIBS -lnsl"]))
AC_CHECK_LIB(inet, main, [LIBS="$LIBS -linet"])

dnl Checks for library functions.
AC_PROG_GCC_TRADITIONAL
AC_TYPE_SIGNAL
AC_CHECK_FUNCS(atexit strchr strdup memcpy memmove strtoul cfmakeraw cfsetspeed cfsetispeed cfsetospeed sigaction dup2 inet_aton gethostname uname putenv)

AC_CACHE_CHECK([for cispeed and cospeed members of struct termios],
  ac_cv_termios_cspeed,
[AC_TRY_COMPILE([#include <termios.h>], [int main(void) {
 struct termios t;t.c_ispeed=t.c_ospeed=0;}],
  ac_cv_termios_cspeed=yes,ac_cv_termios_cspeed=no)])
if test $ac_cv_termios_cspeed = yes; then
  AC_DEFINE(TERMIOS_CSPEED)
fi

AC_CACHE_CHECK([for sa_len],
  ac_cv_sa_len,
[AC_TRY_COMPILE([#include <sys/types.h>
#include <sys/socket.h>], [int main(void) {
 struct sockaddr t;t.sa_len = 0;}],
  ac_cv_sa_len=yes,ac_cv_sa_len=no)])
if test $ac_cv_sa_len = yes; then
  AC_DEFINE(HAVE_SA_LEN)
fi

dnl Check packages
AC_ARG_WITH(tcl,      [  --with-tcl=tclbasedir   use Tcl     [default=yes]])
AC_ARG_WITH(itcl,     [  --with-itcl=iclbasedir  use Itcl    [default=yes]])
AC_ARG_WITH(tk,       [  --with-tk=tkbasedir     use Tk      [default=yes]])
AC_ARG_WITH(python,   [  --with-python=pylibbase use Python  [default=yes]])
AC_ARG_WITH(java,     [  --with-java=jdkbase     use Java    [default=yes]])
AC_ARG_WITH(perl5,    [  --with-perl5=perl5exec  use Perl5   [default=yes]])

if test "x$with_perl5" = "xno"; then
	PERL5=''
else
	if test "x$with_perl5" != "xyes" && test "x$with_perl5" != "x"; then
		PERL5="$with_perl5"
	else
		AC_PATH_PROG(PERL5, perl)
	fi
fi

changequote(, )dnl
if test "x$PERL5" != "x"; then
	case `$PERL5 -e 'print $]'` in
		*5.*) WITHPERL5='WITHPERL5' ;;
		*)	PERL5='' ;;
	esac
fi
changequote([, ])dnl

AC_MSG_CHECKING(for Java Development Kit)
if test "x$with_java" = "xyes" || test "x$with_java" = "x" ; then
	for java_path in /usr/local/java /usr/local /usr/java /usr; do
		if test -f $java_path/bin/javac; then
			with_java=$java_path; break
		fi
	done
	if test "x$with_java" = "xyes"; then
		echo "Unable to find Java base installation directory."
		echo "Please give directory (such as /usr/local) as argument to --with-java=dir."
		with_java=no
	fi
fi 
if test "x$with_java" = "x"; then
	with_java=no
fi
JAVABASE=''
if test "x$with_java" != "xno"; then
	# I'd like to check Java for version, but it doesn't seem to know how to do that. Odd.
changequote(, )dnl
	cat >test.java <<EOF
	public class test { public static void main(String[] args) { 
		System.out.println("present");
	} }
EOF
changequote([, ])dnl
	$with_java/bin/javac test.java
	case `$with_java/bin/java test` in
		*present*) AC_MSG_RESULT(found in $with_java) 
			WITHJAVA='WITHJAVA'
			JAVABASE="$with_java"
			javaos="$host_os"
			case "$javaos" in
			  linux-gnu) javaos="$javaos linux" ;;
			  *solaris*) javaos="$javaos solaris sun" ;;
			  sunos) javaos="$javaos sunos sun" ;;
			esac
			JAVAARCH="genunix"
			for i in $javaos genunix; do
			  if test -f "$JAVABASE/include/$i/byteorder_md.h"; then
			    JAVAARCH="$i"
			    break
			  fi
			done
			JAVAMAJOR=1
			if test -f "$JAVABASE/include/$JAVAARCH/jni_md.h"; then
			  JAVAMINOR=1
			else
			  JAVAMINOR=0
			fi
			;;
		*)	AC_MSG_RESULT(not found)
			with_java=no ;;
	esac
	rm -f test.java test.class
else
	AC_MSG_RESULT(not found)
fi
AC_SUBST(JAVABASE)
AC_SUBST(JAVAARCH)
AC_SUBST(JAVAMAJOR)
AC_SUBST(JAVAMINOR)

if test "x$PERL5" = "x"; then
	PERL5="/usr/bin/perl"
fi

itcl=''
if test "x$with_tcl" = "xyes" || test "x$with_tcl" = "x" ; then
	for tcl_path in /usr/local/tcl8.0 /usr/local/tcl7.6 /usr/local/tcl7.5 /usr/local /usr /usr/local/tcl7.4; do
		if test -f $tcl_path/lib/tclConfig.sh; then
			with_tcl=$tcl_path; break
		elif test -f $tcl_path/lib/itcl/tclConfig.sh; then
			itcl='/itcl'
			with_tcl=$tcl_path; break
		fi
	done
	if test "x$with_tcl" = "xyes"; then
		echo "Unable to find Tcl base installation directory."
		echo "Please give directory (such as /usr/local) as argument to --with-tcl=dir."
		with_tcl=no
	fi
else
	if test -f $with_tcl/lib/itcl/tclConfig.sh; then
		itcl='/itcl'
	fi
fi 

AC_MSG_CHECKING(for Tcl)
TCL_VERSION=""
TCL_LIBS=""
TCL_LIB_SPEC=""
TCL_PACKAGE_SPEC=""
TCL_INC=""
TCL_BIN=""
TK_VERSION=""
TK_LIBS=""
TK_LIB_SPEC=""
TK_INC=""
TK_BIN=""
TCLTK_FLAGS=""
TCLTKFLAGS=""
TCLLIBS=""
TKLIBS=""
TCLTKLIBS=""
if test "x$with_tcl" = "x"; then
	AC_MSG_RESULT(not found)
elif test "x$with_tcl" = "xno"; then
	AC_MSG_RESULT(not used)
else
	tclConfig="$with_tcl/lib$itcl/tclConfig.sh"
	if test ! -f $tclConfig; then
		AC_MSG_RESULT(Unable to find tclConfig.sh)
	else
		. $tclConfig
		AC_MSG_RESULT(version $TCL_VERSION from $tclConfig)
		TCL_INC="-I$TCL_PREFIX/include$itcl"
		TCL_BIN="$TCL_EXEC_PREFIX/bin"
		TCLLIBS="$TCL_LIB_SPEC $TCL_LIBS"
		TCLTKLIBS="$TCLLIBS"
		TCLTK_LIBS="\$(TCL_LIB_SPEC) \$(TCL_LIBS)"
		TCLTK_FLAGS="-DTCL \$(TCL_INC)"
		TCLTKFLAGS="-DTCL $TCL_INC"
		WITHTCL='WITHTCL'
		
		if test "x$with_itcl" = "xyes" || test "x$with_itcl" = "x"; then
			for itcl_path in $with_tcl; do
				if test -f $itcl_path/lib$itcl/itclConfig.sh; then
					with_itcl=$itcl_path; break
				fi
			done
			if test "x$with_itcl" = "xyes"; then
				echo "Unable to find Itcl base installation directory."
				echo "Please give directory (such as /usr/local/itcl2.2) as argument to --with-itcl=dir."
				with_itcl=no
			fi
		fi 

		AC_MSG_CHECKING(for Itcl)
		if test "x$with_itcl" = "x"; then
			AC_MSG_RESULT(not found)
		elif test "x$with_itcl" = "xno"; then
			AC_MSG_RESULT(not used)
		else
			itclConfig="$with_itcl/lib$itcl/itclConfig.sh"
			if test ! -f $itclConfig; then
				AC_MSG_RESULT(Unable to find itclConfig.sh)
			else
				. $itclConfig
				AC_MSG_RESULT(version $ITCL_VERSION from $itclConfig)
				ITCL_INC="-I$TK_PREFIX/include$itcl"
				#TK_BIN="$TK_EXEC_PREFIX/bin"
				#TKLIBS="$TK_LIB_SPEC $TCL_LIB_SPEC $TK_LIBS"
				#TCLTK_LIBS="\$(TK_LIB_SPEC) \$(TCL_LIB_SPEC) \$(TK_LIBS)"
				#TCLTK_FLAGS="$TCLTK_FLAGS -DTK \$(TK_INC)"
				#TCLTKLIBS="$TKLIBS"
				#WITHTK='WITHTK'
				if test "x$ITCL_INC" = "x$TCL_INC"; then
					ITCL_INC=" #same as TCL_INC"
				#	TCLTKFLAGS="-DTCL -DTK $TCL_INC"
				#else
				#	TCLTKFLAGS="-DTCL -DTK $TCL_INC $TK_INC"
				fi
			fi
		fi

		if test "x$with_tk" = "xyes" || test "x$with_tk" = "x"; then
			for tk_path in $with_tcl; do
				if test -f $tk_path/lib$itcl/tkConfig.sh; then
					with_tk=$tk_path; break
				fi
			done
			if test "x$with_tk" = "xyes"; then
				echo "Unable to find Tk base installation directory."
				echo "Please give directory (such as /usr/local) as argument to --with-tk=dir."
				with_tk=no
			fi
		fi 
		
		AC_MSG_CHECKING(for Tk)
		if test "x$with_tk" = "x"; then
			AC_MSG_RESULT(not found)
		elif test "x$with_tk" = "xno"; then
			AC_MSG_RESULT(not used)
		else
			tkConfig="$with_tk/lib$itcl/tkConfig.sh"
			if test ! -f $tkConfig; then
				AC_MSG_RESULT(Unable to find tkConfig.sh)
			else
				. $tkConfig
				AC_MSG_RESULT(version $TK_VERSION from $tkConfig)
				TK_INC="-I$TK_PREFIX/include$itcl"
				TK_BIN="$TK_EXEC_PREFIX/bin"
				TKLIBS="$TK_LIB_SPEC $TCL_LIB_SPEC $TK_LIBS"
				TCLTK_LIBS="\$(TK_LIB_SPEC) \$(TCL_LIB_SPEC) \$(TK_LIBS)"
				TCLTK_FLAGS="$TCLTK_FLAGS -DTK \$(TK_INC)"
				TCLTKLIBS="$TKLIBS"
				WITHTK='WITHTK'
				if test "x$TK_INC" = "x$TCL_INC"; then
					TK_INC=" #same as TCL_INC"
					TCLTKFLAGS="-DTCL -DTK $TCL_INC"
				else
					TCLTKFLAGS="-DTCL -DTK $TCL_INC $TK_INC"
				fi
			fi
		fi


	fi
fi

TCL_PACKAGE_PATH=`echo $TCL_PACKAGE_PATH | sed 's/ .*//'`

AC_SUBST(TCL_VERSION)
AC_SUBST(TCL_LIBS)
AC_SUBST(TCL_LIB_SPEC)
AC_SUBST(TCL_PACKAGE_PATH)
AC_SUBST(TCL_INC)
AC_SUBST(TCL_BIN)
AC_SUBST(TK_VERSION)
AC_SUBST(TK_LIBS)
AC_SUBST(TK_LIB_SPEC)
AC_SUBST(TK_INC)
AC_SUBST(TK_BIN)
AC_SUBST(TCLTK_LIBS)
AC_SUBST(TCLTK_FLAGS)
AC_SUBST(TCLLIBS)
AC_SUBST(TKLIBS)
AC_SUBST(TCLTKLIBS)
AC_SUBST(TCLTKFLAGS)


TCLEXECS=''
if test ! "x$TCL_VERSION" = "x"; then
	TCLEXECS='pilot-debug$(EXT)'
fi
AC_SUBST(TCLEXECS)

if test "x$with_python" = "xyes" || test "x$with_python" = "x" ; then
        AC_PATH_PROG(with_python,python,"")
	if test "x$with_python" = "xyes"; then
		echo "Unable to find Python base directory."
		echo "Please give directory (such as /usr/local/) as argument to --with-python=dir."
		with_python=no
	fi
fi 

AC_MSG_CHECKING(for Python)

PYTHON_PREFIX=''
PYTHON_EXEC_PREFIX=''
PYTHONLIBDIR=''
PYTHONINCDIR=''
PYTHONVER=''
if test "x$with_python" = "x"; then
	AC_MSG_RESULT(not found)
elif test "x$with_python" = "xno"; then
	AC_MSG_RESULT(not used)
else
	PYTHON_PREFIX=`$with_python -c "import sys; print sys.prefix"`
	PYTHON_EXEC_PREFIX=`$with_python -c "import sys; print sys.exec_prefix"`
	PYTHONVER=[`$with_python -c "import sys, string; print string.split(sys.version)[0]"`]
	PYTHONLIBVER=[`$with_python -c "\
import sys, string
ver = string.split(sys.version)[0]
while '.' in ver:
  trim = ver[:string.rfind(ver, '.')]
  if '.' not in trim:
    break
  ver = trim
print ver"`]
	if test ! -f ${PYTHON_PREFIX}/include/python$PYTHONLIBVER/Python.h; then
		AC_MSG_RESULT(Unable to find ${PYTHON_PREFIX}/include/python$PYTHONLIBVER/Python.h)
	elif test ! -f ${PYTHON_EXEC_PREFIX}/lib/python$PYTHONLIBVER/config/Makefile; then
		AC_MSG_RESULT(Unable to find ${PYTHON_EXEC_PREFIX}/lib/python$PYTHONLIBVER/config/Makefile)
	else
		PYTHONLIBDIR=${PYTHON_EXEC_PREFIX}/lib/python$PYTHONLIBVER
		PYTHONINCDIR=${PYTHON_PREFIX}/include/python$PYTHONLIBVER
		WITHPYTHON='WITHPYTHON'
		AC_MSG_RESULT(Python version $PYTHONVER in $PYTHONLIBDIR)
	fi
fi
AC_SUBST(PYTHON_PREFIX)
AC_SUBST(PYTHON_EXEC_PREFIX)
AC_SUBST(PYTHONLIBDIR)
AC_SUBST(PYTHONINCDIR)
AC_SUBST(PYTHONVER)
AC_SUBST(PYTHONLIBVER)


dnl Derive warning flag
AC_CACHE_CHECK([for maximum warnings compiler flag],
  ac_cv_cwflag,
[case "${CC-cc}" in
  *gcc*) ac_cv_cwflag=-Wall;;
  *)	case "$host" in
			*irix*)	ac_cv_cwflag=-fullwarn ;;
			*)		ac_cv_cwflag=;;
		esac ;;
esac])
CWFLAG=$ac_cv_cwflag
AC_SUBST(CWFLAG)

dnl NeXT support
case "$host" in
    *next*) CPLIB="ln -s"
            ARFLAGS="cur" ;;
    *)      CPLIB="cp"
            ARFLAGS="cur" ;;
esac
AC_SUBST(CPLIB)
AC_SUBST(ARFLAGS)

AC_CONFIG_HEADER(include/pi-config.h include/pi-sockaddr.h)

if test $ac_cv_prog_cxx_works = yes; then
  cc_m='libcc/Makefile'
fi

AC_SUBST(WITHTCL)
AC_SUBST(WITHJAVA)
AC_SUBST(WITHTK)
AC_SUBST(WITHPYTHON)
AC_SUBST(WITHPERL5)
AC_SUBST(WITHCXX)

LTLIBOBJS=`echo "$LIBOBJS" | sed 's/\.o/\.lo/g'`
AC_SUBST(LTLIBOBJS)
LTALLOCA=`echo "$ALLOCA" | sed 's/\.o/\.lo/g'`
AC_SUBST(LTALLOCA)
AC_OUTPUT(libsock/Makefile $cc_m Makefile dubious/Makefile Python/Makefile Tcl/Makefile Perl5/Makefile.PL Java/Makefile tests/Makefile)
