dnl ####################################################################
dnl #   Copyright (c)                                                  #
dnl #                                                                  #
dnl #   You may copy, distribute, and modify it freely as long as you  #
dnl #   preserve this copyright and permission notice.                 #
dnl #                                                                  #
dnl #                                                                  #
dnl ####################################################################
dnl
dnl File         : configure.in
dnl Author       : Guansong Zhang
dnl Created      : Wed Apr  8 10:03:10 EDT 1998
dnl Revision     : $Revision: 1.13 $
dnl Updated      : $Date: 1998/05/12 16:33:52 $
dnl 
dnl Process this file and acconfig.h by autoconf to produce a configure script.
dnl 

AC_INIT(./configure)

AC_PREFIX_DEFAULT(`pwd`)
AC_CONFIG_AUX_DIR(./config)

AC_CONFIG_HEADER(./src/config.h)

AC_CANONICAL_SYSTEM
AC_ARG_PROGRAM
AC_HEADER_STDC

dnl AC_CHECK_TYPE(u_short,unsigned short)
AC_HEADER_SYS_WAIT

AC_PROG_RANLIB

VERSION=1.00
AC_DEFINE_UNQUOTED(VERSION, "$VERSION")
AC_SUBST(VERSION)

dnl an inline which function

CWD=`pwd`
AC_SUBST(CWD)

which () {
  oldIFS=$IFS

  IFS=:
  for dir in $PATH
  do
    if test -x $dir/$1; then
       found="$dir/$1"; 
       echo $found; 
       break ;
    fi
  done;

  if test -z "$found" ; then
    echo $1; # echo input if not found.
  fi 

  IFS=$oldIFS 
}


dnl function to find mpi.h

mpiinc () {
 
  MPI_PREFIX=`which mpirun | sed 's%/bin/mpirun%%'`
  if test -f $MPI_PREFIX/include/mpi.h; then
#    echo $MPI_PREFIX/include
    MPI_INC=$MPI_PREFIX/include
  else
    MPI_PREFIX=`which mpirun | sed 's%/lib/[^/]*/[^/]*/mpirun%%'`
    if test -f $MPI_PREFIX/include/mpi.h; then
#      echo $MPI_PREFIX/include
      MPI_INC=$MPI_PREFIX/include
    else
      echo "Please input the absolute path for mpich include dir:"
      read MPI_INC
    fi
  fi      
}

dnl function to find libmpi.a

mpilib () {
  MPI_PREFIX=`which mpirun | sed 's%/bin/mpirun%%'`
  num=0
  for dir in $MPI_PREFIX/lib/*/*; do 
    if test -f $dir/libmpi.a ; then
      num=`expr $num + 1`  
    fi
  done
# echo $num
  if test `expr $num` -eq 1 ; then
    for dir in $MPI_PREFIX/lib/*/*; do 
      if test -f $dir/libmpi.a ; then
	MPI_LIB=$dir
        return
      fi
    done
  fi
  if test `expr $num` -lt 1 ; then
    MPI_LIB=`which mpirun | sed 's%/mpirun%%'`
    if test -f $MPI_LIB/libmpi.a ; then
      return
    else
      echo "Please input the absolute path for mpich library dir:"
      read MPI_LIB
      return
    fi
  fi
  if test `expr $num` -gt 1 ; then
    echo "Configure script found multi communication devices,"
    echo "Please input the absolute path for mpich library dir,"
    echo "the one work with "$MPI_PREFIX/bin/mpirun:
    read MPI_LIB
    return
  fi
}

dnl deciding mpi package

AC_MSG_CHECKING(MPI package)

AC_ARG_WITH(MPI, [
--with-MPI=mpich          Use mpich MPI (the defualt setting)
--with-MPI=sunhpc         Use HPC MPI from SUN
--with-MPI=lam            Use LAM/MPI from University of Notre Dame
--with-MPI=sp2            Use SP2 MPI from IBM ], MPI=${withval}, MPI=mpich )

AC_CACHE_VAL(mpiJava_cv_mpipackage, mpiJava_cv_mpipackage=$MPI) 

AC_MSG_RESULT($mpiJava_cv_mpipackage)

MPI=$mpiJava_cv_mpipackage
AC_SUBST(MPI)

case "$MPI" in
  mpich )

  dnl check if mpi installed.
  if test ! -x `which mpirun`
  then 
    echo "Please install mpich first ..."
    exit
  fi

  dnl decide whether there is MPICXX

  AC_CHECK_PROG(MPICXX, mpiCC, mpiCC)

  dnl check whether mpiCC work.

  if test "$MPICXX" = "mpiCC" 
  then
    AC_MSG_CHECKING(whether mpiCC configured)

    echo "any(){}" > conftest.C

    rm -f conftest.o
    mpiCC -c conftest.C > /dev/null 2> /dev/null
    
    if test -f conftest.o
    then
      MPICXXCONFIG=yes
    else
      MPICXXCONFIG=no
    fi

    AC_MSG_RESULT($MPICXXCONFIG)
     	
    rm -f conftest.C conftest.o
  fi  

  dnl decide whether there is MPICC

  AC_CHECK_PROG(MPICC, mpicc, mpicc)

  if test "$MPICC" = "mpicc" 
  then
    AC_MSG_CHECKING(whether mpicc configured)

    echo "any(){}" > conftest.c

    rm -f conftest.o
    mpicc -c conftest.c > /dev/null 2> /dev/null
    
    if test -f conftest.o
    then
      MPICCCONFIG=yes
    else
      MPICCCONFIG=no
    fi

    AC_MSG_RESULT($MPICCCONFIG)
     	
    rm -f conftest.c conftest.o
  fi  

  dnl get mpi prefix

  AC_MSG_CHECKING(MPI include dir)

  AC_CACHE_VAL(mpiJava_cv_mpi_inc, mpiinc;mpiJava_cv_mpi_inc=$MPI_INC) 
  AC_MSG_RESULT($mpiJava_cv_mpi_inc)

  MPI_INC=$mpiJava_cv_mpi_inc
  AC_SUBST(MPI_INC)

  dnl get mpich exec_prefix

  AC_MSG_CHECKING(MPI lib dir)

  AC_CACHE_VAL(mpiJava_cv_mpi_lib, mpilib; mpiJava_cv_mpi_lib=$MPI_LIB) 

  AC_MSG_RESULT($mpiJava_cv_mpi_lib)

  MPI_LIB=$mpiJava_cv_mpi_lib
  AC_SUBST(MPI_LIB) 

  dnl tarch and tdevice from mpich only return defaults setting.

  ARCH=`echo $MPI_LIB | sed 's/^.*lib\///' | sed 's/\/.*$//'`

  dnl deciding compiler and linker

  if test "$MPICXXCONFIG" = "yes" 
  then
    CXX=mpiCC
    AC_SUBST(CXX)

    CXXLIBKEY=
  else
    dnl decide compiler and lib according to machine.
    CXXLIBKEY="-lmpi"

    case $target in
      *ibm* )
        AC_CHECK_PROGS(CXX, xlC c++)        	
	if test "$CXX" = "xlC"
	then
	  LIBINLINE=
	  CPLINLINE=
	fi
        ;;

      *solaris* )
        AC_CHECK_PROGS(CXX, CC c++)
        CXXLIBKEY="$CXXLIBKEY -lsocket -lnsl" 
        case $CXX in
          CC*)
	    LIBINLINE=
	    CPLINLINE=+d
            ;;
        esac
        ;;

      *dec* )
        AC_CHECK_PROGS(CXX, cxx c++)
        if test $CXX = cxx 
        then
          CXXLIBKEY="$CXXLIBKEY -lcomplex -lm"
	  LIBINLINE=-noinline
	  CPLINLINE=-noinline
        fi
        ;;

      *mips* )
        AC_CHECK_PROGS(CXX, CC c++)
	if test "$CXX" = "CC"
	then
          CXXLIBKEY="$CXXLIBKEY -lm"
          case $ARCH in
            IRIX64)
              CXX="CC -64"
              ;;
            IRIXN32)
              CXX="CC -n32"
              ;;
            IRIX32)
              CXX="CC -32"
              ;;
            *)
              ;;
          esac
	fi
        ;;

      * ) 
        AC_CHECK_PROGS(CXX, c++)
	echo "CXXLIBKEY does not tested on $target"
        ;; 
    esac
  fi

  if test "$CXX" = "" ; then
    echo "Found no C++ compiler on $target"
    exit
  fi

  AC_SUBST(LIBKEY)
  AC_SUBST(LIBINLINE)
  AC_SUBST(CPLINLINE)


  if test "$MPICCCONFIG" = "yes" 
  then
    CC=mpicc
    AC_SUBST(CC)

    CCLIBKEY=
  else
    dnl decide compiler and lib according to machine.
    CCLIBKEY="-lmpi"
    case $target in
      *linux* )
        AC_CHECK_PROGS(CC, gcc)
        ;;

      * ) 
        AC_CHECK_PROGS(CC, cc gcc)
#	echo "CCLIBKEY does not tested on $target"
        ;; 
    esac
  fi

  AC_SUBST(CCLIBKEY)

  if test "$CC" = "" ; then
    echo "Found no C compiler on $target"
    exit
  fi

  ;;

  chimp )
  echo Not supported 
  ;;

  sp2 )

  AC_CHECK_PROG(MPICC, mpCC, mpCC)

  if test "$MPICC" = "mpCC" 
  then 
    CXX=mpCC
  else
    echo "Please install SP2 mpCC first ..."
    exit
  fi

  MPI_INC=
  AC_SUBST(MPI_INC)

  MPI_LIB=
  AC_SUBST(MPI_LIB) 

  LIBKEY=
  AC_SUBST(LIBKEY)
  ;;

  sunhpc )

  AC_CHECK_PROG(MPIRUN, tmrun, tmrun)

  if test "$MPIRUN" = "tmrun" 
  then 
    AC_CHECK_PROGS(CXX, CC c++)
    case $CXX in
      CC*)
	LIBINLINE=
	CPLINLINE=+d
        ;;
    esac
  else
    echo "Please install Sun HPC first ..."
    exit
  fi

  MPI_INC="/opt/SUNWhpc/include"
  AC_SUBST(MPI_INC)

  MPI_LIB="/opt/SUNWhpc/lib -R/opt/SUNWhpc/lib:/opt/SUNWspro/lib"
  AC_SUBST(MPI_LIB) 

  LIBKEY="-lmpi -lrte -lnsl -lgen -lsocket -lrpcsvc -lposix4"
  AC_SUBST(LIBKEY)
  ;;

  lam )

  AC_CHECK_PROG(MPICC, hcc, hcc)

  if test "$MPICC" = "hcc" 
    CXX=mpCC
  else
    echo "Please install Sun HPC first ..."
    exit
  fi

  MPI_INC=
  AC_SUBST(MPI_INC)

  MPI_LIB=
  AC_SUBST(MPI_LIB) 

  LIBKEY="-lmpi"
  AC_SUBST(LIBKEY)
  ;;


  * )
  echo The package does not support $MPI MPI.
  exit
  ;;

esac

dnl check where MPIPATH is

MPIPATH=`which mpirun | sed 's%/bin/mpirun%%'`

echo checking mpich is at $MPIPATH
AC_SUBST(MPIPATH)

dnl for adJava interface

JAVA=`which java | sed 's%/bin/java%%'`
echo checking jdk is at $JAVA

dnl if JDK is available

if test "$JAVA" != "" -a "$MPI" = "mpich"
then
  AC_SUBST(JAVA)

  case $target in

    *aix* )
    JOS="aix"
    ;;

    *solaris* )
    JOS="solaris"
    ;;

    *irix* )
    JOS="irix"
    ;;

    *linux*)
    JOS="genlinux"
    ;;

    *)
    echo Java interface not tested on $target yet.
    ;;

  esac

  AC_SUBST(JOS)

#  adJava=adJava
  adJava=
else
  adJava=
fi

AC_SUBST(adJava)



AC_OUTPUT(
Makefile 
src/Makefile
script/prunjava:script/prunjava.in
script/prun:script/prun.in
tests/Makefile:tests/Makefile.in
tests/simple/Makefile:tests/simple/Makefile.in
tests/simple/RunTests:tests/simple/RunTests.in
tests/ccl/Makefile:tests/ccl/Makefile.in
tests/ccl/RunTests:tests/ccl/RunTests.in
tests/comm/Makefile:tests/comm/Makefile.in
tests/comm/RunTests:tests/comm/RunTests.in
tests/dtyp/Makefile:tests/dtyp/Makefile.in
tests/dtyp/RunTests:tests/dtyp/RunTests.in
tests/env/Makefile:tests/env/Makefile.in
tests/env/RunTests:tests/env/RunTests.in
tests/group/Makefile:tests/group/Makefile.in
tests/group/RunTests:tests/group/RunTests.in
tests/pt2pt/Makefile:tests/pt2pt/Makefile.in
tests/pt2pt/RunTests:tests/pt2pt/RunTests.in
tests/topo/Makefile:tests/topo/Makefile.in
tests/topo/RunTests:tests/topo/RunTests.in
examples/metropolis/Makefile:examples/metropolis/Makefile.in
examples/metropolis/RunIsing:examples/metropolis/RunIsing.in
examples/swendsen_wang/Makefile:examples/swendsen_wang/Makefile.in
examples/swendsen_wang/RunIsing:examples/swendsen_wang/RunIsing.in
)


if  test -d classes 
then 
	if test ! -d classes/mpi
	 then
		mkdir classes/mpi
	fi
else
	mkdir classes
	mkdir classes/mpi
fi

if  test ! -d lib
then
	mkdir lib
fi

chmod a+x script/prun
chmod a+x script/prunjava
chmod a+x tests/simple/RunTests
chmod a+x tests/ccl/RunTests
chmod a+x tests/comm/RunTests
chmod a+x tests/dtyp/RunTests
chmod a+x tests/env/RunTests
chmod a+x tests/group/RunTests
chmod a+x tests/pt2pt/RunTests
chmod a+x tests/topo/RunTests
chmod a+x examples/metropolis/RunIsing
chmod a+x examples/swendsen_wang/RunIsing

