dnl Process this file with autoconf to produce a configure script.
AC_INIT(Include/swill/swill.h)
AC_PREREQ(2.0)

# Set name for machine-dependent library files
AC_SUBST(MACHDEP)
AC_MSG_CHECKING(MACHDEP)
if test -z "$MACHDEP"
then
	if test -f /usr/lib/NextStep/software_version; then
		set X `hostinfo | grep 'NeXT Mach.*:' | \
			sed -e 's/://' -e 's/\./_/'` && \
		ac_sys_system=next && ac_sys_release=$4
		MACHDEP="$ac_sys_system$ac_sys_release$ac_sys_cpu"
	else
		ac_sys_system=`uname -s`
		if test "$ac_sys_system" = "AIX" ; then
			ac_sys_release=`uname -v`
		else
			ac_sys_release=`uname -r`
		fi
		ac_md_system=`echo $ac_sys_system |
				   tr -d '[/ ]' | tr '[[A-Z]]' '[[a-z]]'`
		ac_md_release=`echo $ac_sys_release |
				   tr -d '[/ ]' | sed 's/\..*//'`
		MACHDEP="$ac_md_system$ac_md_release"
	fi
	case MACHDEP in
	'')	MACHDEP=unknown;;
	esac
fi
AC_MSG_RESULT($MACHDEP)

AC_PROG_CC
AC_PROG_RANLIB
AC_SUBST(AR)
AC_CHECK_PROGS(AR, ar aal, ar)

dnl Checks for header files.
AC_HEADER_STDC
dnl Checks for library functions.


# Set info about shared libraries.
AC_SUBST(SO)
AC_SUBST(LDSHARED)
AC_SUBST(CCSHARED)
AC_SUBST(CXXSHARED)

# SO is the extension of shared libraries `(including the dot!)
# -- usually .so, .sl on HP-UX
AC_MSG_CHECKING(SO)
if test -z "$SO"
then
	case $ac_sys_system in
	hp*|HP*)   SO=.sl;;
	CYGWIN*)   SO=.dll;;
	Darwin)    SO=.dylib;;
	*)	   SO=.so;;
	esac
fi
AC_MSG_RESULT($SO)
# LDSHARED is the ld *command* used to create shared library
# -- "ld" on SunOS 4.x.x, "ld -G" on SunOS 5.x, "ld -shared" on IRIX 5
AC_MSG_CHECKING(LDSHARED)
if test -z "$LDSHARED"
then
	case $ac_sys_system/$ac_sys_release in
	AIX*) LDSHARED="\$(srcdir)/ld_so_aix \$(CC)";;
	CYGWIN*) LDSHARED="gcc -shared";;
	IRIX/5*) LDSHARED="ld -shared";;
	IRIX*/6*) LDSHARED="ld ${SGI_ABI} -shared -all";;
	SunOS/4*) LDSHARED="ld";;
	SunOS/5*) LDSHARED="ld -G";;
	hp*|HP*) LDSHARED="ld -b";;
	OSF*) LDSHARED="ld -shared -expect_unresolved \"*\"";;
	DYNIX/ptx*) LDSHARED="ld -G";;
	next/*)
		if test "$ns_dyld"
		then LDSHARED='$(CC) $(LDFLAGS) -bundle -prebind'
		else LDSHARED='$(CC) $(CFLAGS) -nostdlib -r';
		fi
                if test "$with_next_framework" ; then
		    LDSHARED="$LDSHARED \$(LDLIBRARY)"
		fi ;;
	Linux*) LDSHARED="gcc -shared";;
	dgux*) LDSHARED="ld -G";;
	FreeBSD*/3*) LDSHARED="gcc -shared";;
	FreeBSD*|OpenBSD*) LDSHARED="ld -Bshareable";;
	NetBSD*)
		if [[ "`$CC -dM -E - </dev/null | grep __ELF__`" != "" ]]
		then
			LDSHARED="cc -shared"
		else
			LDSHARED="ld -Bshareable"
		fi;;
	SCO_SV*) LDSHARED="cc -G -KPIC -Ki486 -belf -Wl,-Bexport";;
	Darwin/6*) LDSHARED="libtool -dynamic -lSystem";;
	*)	LDSHARED="ld";;
	esac
fi
AC_MSG_RESULT($LDSHARED)
# CXXSHARED is the ld *command* used to create C++ shared library
# -- "ld" on SunOS 4.x.x, "ld -G" on SunOS 5.x, "ld -shared" on IRIX 5
# (Shared libraries in this instance are shared modules to be loaded into
# Python, as opposed to building Python itself as a shared library.)
AC_MSG_CHECKING(CXXSHARED)
if test -z "$CXXSHARED"
then
	CXXSHARED="$LDSHARED"
fi
AC_MSG_RESULT($CXXSHARED)

# CCSHARED are the C *flags* used to create objects to go into a shared
# library (module) -- this is only needed for a few systems
AC_MSG_CHECKING(CCSHARED)
if test -z "$CCSHARED"
then
	case $ac_sys_system/$ac_sys_release in
	hp*|HP*) if test "$GCC" = yes;
		 then CCSHARED="-fpic";
		 else CCSHARED="+z";
		 fi;;
	Linux*) CCSHARED="-fpic";;
	FreeBSD*|OpenBSD*) CCSHARED="-fpic";;
	NetBSD*) CCSHARED="-fPIC";;
	SCO_SV*) CCSHARED="-KPIC -dy -Bdynamic";;
	IRIX*/6*)  case $CC in
		   *gcc*) CCSHARED="-shared";;
		   *) CCSHARED="";;
		   esac;;
	esac
fi
AC_MSG_RESULT($CCSHARED)

# RPATH is the path used to look for shared library files.
AC_MSG_CHECKING(RPATH)
if test -z "$RPATH"
then
	case $ac_sys_system/$ac_sys_release in
	SunOS/5*) RPATH='-R. -R$(exec_prefix)/lib';;
        IRIX*) RPATH='-rpath .:$(exec_prefix)/lib';;
	Linux*) RPATH='-Xlinker -rpath $(exec_prefix)/lib -Xlinker -rpath .';;
	*)	RPATH='';;
	esac
fi
AC_MSG_RESULT($RPATH)
AC_SUBST(RPATH)


# Check for specific libraries.   Used for SWIG examples
AC_CHECK_LIB(dl, dlopen)	# Dynamic linking for SunOS/Solaris and SYSV
AC_CHECK_LIB(dld, shl_load)	# Dynamic linking for HP-UX

# The following three (nsl,inet,socket) are needed on Sequent;
# the order of checking must be this.  Most SVR4 platforms will
# need -lsocket and -lnsl.  However on SGI IRIX 5, these exist but
# broken.  I see no elegant solution (probably CHECK_LIB should be
# fixed to only add the library if the given entry point is not
# satisfied without it).

if test "`uname -s`" != IRIX
then
AC_CHECK_LIB(nsl, t_open, [NETLIBS="-lnsl"]) # SVR4
AC_CHECK_LIB(inet, gethostbyname, [NETLIBS="-linet $NETLIBS"], [], -lnsl) # Sequent
AC_CHECK_LIB(socket, socket, [NETLIBS="-lsocket $NETLIBS"], [], $NETLIBS) # SVR4 sockets
fi

AC_SUBST(NETLIBS)


########################################################################
# MPI?
########################################################################
# We don't need to know anything about libmpi at this stage. -TL 1/16/02
########################################################################
USEMPI= 
MPISUFFIX=
MPIPACKAGE=
MPIINCLUDE=
#MPILIB=
AC_ARG_WITH(mpi, [  --with-mpi[=PATH]       Enable MPI/Set location of MPI package], [MPIPACKAGE="$withval"], [MPIPACKAGE=])
AC_ARG_WITH(mpiincl,[  --with-mpiincl=PATH     Set location of MPI include directory],[MPIINCLUDE="-I$withval"], [MPIINCLUDE=])
#AC_ARG_WITH(mpilib,[  --with-mpilib=PATH      Set location of MPI library directory],[MPILIB="-L$withval"], [MPILIB=])

AC_MSG_CHECKING([for MPI configuration])

if test -n "$MPIPACKAGE" 
then
	USEMPI="-D__USE_MPI"
        MPISUFFIX="mpi"
	MPICC="$MPIPACKAGE/bin/mpicc"
fi

# if test -z "$MPILIB"; then
#    if test -n "$MPIPACKAGE" -a "$MPIPACKAGE" != "yes"; then
#         MPILIB="-L$MPIPACKAGE/lib -lMPI"
#    fi
# fi

if test -z "$MPIINCLUDE"
then 
   if test -n "$MPIPACKAGE" -a "$MPIPACKAGE" != "yes"
   then
        MPIINCLUDE="-I$MPIPACKAGE/include"
   fi
fi
#  AC_MSG_CHECKING([for MPI library])
#  if test -z "$MPILIB"; then
#  dirs="$prefix/lib /usr/local/lib /usr/lib /opt/local/lib"
#  for i in $dirs ; do
#  	if test -r $i/libmpi.a; then
#  	    AC_MSG_RESULT($i)
#  	    MPILIB="-L$i -lmpi"
#  	    break
#  	fi
#  done
#  if test -z "$MPILIB"; then
#  	AC_MSG_RESULT(not found)
#  fi
#  else
#  AC_MSG_RESULT($MPILIB)
#  fi

AC_MSG_CHECKING([for MPI header files])
if test -z "$MPIINCLUDE"; then
AC_TRY_CPP([#include <mpi.h>], , MPIINCLUDE="")
if test -z "$MPIINCLUDE"; then
	dirs="$prefix/include /usr/local/include /usr/include /opt/local/include"
	for i in $dirs ; do
		if test -r $i/mpi.h; then
			AC_MSG_RESULT($i)
			MPIINCLUDE="-I$i"
			break
		fi
	done
fi
if test -z "$MPIINCLUDE"; then
    	AC_MSG_RESULT(not found)
fi
else
        AC_MSG_RESULT($MPIINCLUDE)
fi
AC_SUBST(MPIINCLUDE)
#AC_SUBST(MPILIB)
AC_SUBST(USEMPI)
AC_SUBST(MPICC)
AC_SUBST(MPISUFFIX)

########################################################################

# srcdir is set and managed by configure.  We export it to the Makefiles
SRC_DIR=$srcdir
AC_SUBST(SRC_DIR)

source_dirs="Source/SWILL Source/Objects Examples"
for d in $source_dirs; do
       test -d "$d" || mkdir -p "$d"
done

AC_OUTPUT(Makefile Source/SWILL/Makefile Source/Objects/Makefile Examples/Makefile)


