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, Sung-Hoon Ko
dnl Created      : Wed Aug  8 10:03:10 EDT 1998
dnl Revision     : $Revision: 1.18 $
dnl Updated      : $Date: 1999/09/16 20:10:11 $
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(./config/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)
AC_CHECK_SIZEOF(void *)

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 libmpich.a(MPICH1.1.2)
mpilib_new () {
  MPI_PREFIX=`which mpirun | sed 's%/bin/mpirun%%'`
  num=0
  CCLIBKEY="-lmpich"

  for dir in $MPI_PREFIX/build/*/*/lib; do 
      if test -f $dir/libmpich.a ; then
        num=`expr $num + 1`
      fi       
  done
# echo $num
  if test `expr $num` -eq 1 ; then
    for dir in $MPI_PREFIX/build/*/*/lib; do 
        if test -f $dir/libmpich.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/libmpich.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 function to find libmpi.a or libmpich.a(MPICH1.1.1)
mpilib_old () {
  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`
      CCLIBKEY="-lmpi"
    else
      if test -f $dir/libmpich.a ; then
        num=`expr $num + 1`
        CCLIBKEY="-lmpich"
      fi       
    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
      else 
        if test -f $dir/libmpich.a ; then  
  	  MPI_LIB=$dir
          return         
        fi
      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 
      if test -f $MPI_LIB/libmpich.a ; then
        return
      else
        echo "Please input the absolute path for mpich library dir:"
        read MPI_LIB
        return
      fi
    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
}

mpilib () {
    MPI_PREFIX=`which mpirun | sed 's%/bin/mpirun%%'`
    if test -d $MPI_PREFIX/build; then
	mpilib_new;
    else
        mpilib_old;
    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=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)


#echo "cclibkey22 = $CCLIBKEY"
  if test "$MPICCCONFIG" = "yes" 
  then
    CC=mpicc
    AC_SUBST(CC)
    CCLIBKEY=$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

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

  CC="tmcc"
  AC_SUBST(CC)

  MPI_INC="$MPIPATH/include"
  AC_SUBST(MPI_INC)

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

  CCLIBKEY="-lmpi"
  AC_SUBST(CCLIBKEY)

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

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

esac

dnl check where MPIPATH is

case "$MPI" in
  mpich )
    MPIPATH=`which mpirun | sed 's%/bin/mpirun%%'`
    ;;
  sunhpc )
    MPIPATH=`which tmrun | sed 's%/bin/tmrun%%'`
    ;;
  * )
    MPIPATH=`which mpirun | sed 's%/bin/mpirun%%'`
    exit
    ;;
esac


echo checking mpi is at $MPIPATH
AC_SUBST(MPIPATH)

dnl for mpiJava interface


######
dnl function to find JAVA package
#####
javadir () {
    JAVAPATH=`which javac | sed 's%/bin/javac%%'`
    if test -f $JAVAPATH/bin/java ; then
	return
    else
	echo "Please input the absolute path for java package:"
	read JAVAPATH
	while :
	do
	if test -f $JAVAPATH/bin/javac ; then
	    break
	else
	    echo "Wrong java installed path!  No javac in $JAVAPATH/bin ."  
	    echo "Please input the absolute path for java package:"
	    read JAVAPATH
	fi
	done	
	return
    fi

## Searching and setting java class path

    if test "$CLASSPATH" = ""     
    then                          
	CLASSPATH=
    else                          
	CLASSPATH=$CLASSPATH
    fi
    AC_SUBST(CLASSPATH)

}

javadir;JAVA="$JAVAPATH"
echo checking jdk is at $JAVA


#### End Searching JAVA package #####

dnl if JDK is available


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

  case $target in

    *aix* )
    JOS="aix"
    ;;

    *solaris* )
    JOS="solaris"
    changequote(,)
    javaversion=`$JAVAPATH/bin/java -version 2>&1|  grep 'java version' | \
                 sed -e 's/^.*"\([0-9].[0-9]\).*$/\1/'`
    changequote([,])
    ;;

    *irix* )
    JOS="irix"
    changequote(,)
    javaversion=`$JAVAPATH/bin/java -version 2>&1|  grep 'java version' | \
                  sed -e 's/^.*Sun \([0-9].[0-9]\).*$/\1/'`
    changequote([,])
     ;;

    *linux*)
    JOS="linux"
    changequote(,)
    javaversion=`$JAVAPATH/bin/java -version 2>&1|  grep 'java version' | \
                 sed -e 's/^.*"\([0-9].[0-9]\).*$/\1/'`
    changequote([,])
    ;;

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

  esac
fi

echo checking java target is $JOS
AC_SUBST(JOS)

###
dnl setting java thread option.
###
if test "$javaversion" = "1.2" ; then
   JAVATHREAD="-native"
   echo "checking java thread option is -native"
else
   echo "checking java thread option is -green"
fi
AC_SUBST(JAVATHREAD)

###
dnl setting link editor(ld) and options.
###
if test "$CC" != "" ; then
  case $target in
    *aix* )
    LD="$MPIPATH/bin/$CC"
    LDFLAG=
    ;;

    *solaris* )
    LD="$MPIPATH/bin/$CC"
    LDFLAG="-G"
    ;;

    *irix* )
    LD="$MPIPATH/bin/$CC"
    LDFLAG="-shared"
     ;;

    *linux*)
    LD=ld
    LDFLAG="-shared"
    ;;

    *)
    echo link option not tested on $target yet.
    ;;

  esac
fi
echo "checking link editor(ld) is $LD"
echo "checking link editor(ld) option is $LDFLAG"
AC_SUBST(LD)
AC_SUBST(LDFLAG)



AC_OUTPUT(
Makefile 
src/Makefile
src/C/Makefile
src/Java/Makefile
src/Java/mpi/Makefile
src/scripts/prunjava:src/scripts/prunjava.in
src/scripts/prun:src/scripts/prun.in

tests/Makefile:tests/Makefile.in
tests/ccl/Makefile:tests/ccl/Makefile.in
tests/ccl/RunTests:tests/ccl/RunTests.in
tests/ccl_ObjSer/Makefile:tests/ccl_ObjSer/Makefile.in
tests/ccl_ObjSer/RunTests:tests/ccl_ObjSer/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/dtyp_ObjSer/Makefile:tests/dtyp_ObjSer/Makefile.in
tests/dtyp_ObjSer/RunTests:tests/dtyp_ObjSer/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/pt2pt_ObjSer/Makefile:tests/pt2pt_ObjSer/Makefile.in
tests/pt2pt_ObjSer/RunTests:tests/pt2pt_ObjSer/RunTests.in
tests/topo/Makefile:tests/topo/Makefile.in
tests/topo/RunTests:tests/topo/RunTests.in

examples/simple/Makefile:examples/simple/Makefile.in
examples/simple/RunTests:examples/simple/RunTests.in
examples/PingPong/Makefile:examples/PingPong/Makefile.in
examples/PingPong/RunBench:examples/PingPong/RunBench.in
examples/metropolis/Makefile:examples/metropolis/Makefile.in
examples/metropolis/RunIsing:examples/metropolis/RunIsing.in
examples/potts/Makefile:examples/potts/Makefile.in
examples/potts/RunIsing:examples/potts/RunIsing.in
examples/Nozzle/RunNozzle:examples//Nozzle/RunNozzle.in
examples/Nozzle/Makefile:examples//Nozzle/Makefile.in
)
#examples/swendsen_wang/Makefile:examples/swendsen_wang/Makefile.in
#examples/swendsen_wang/RunIsing:examples/swendsen_wang/RunIsing.in


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

chmod a+x src/scripts/prun
chmod a+x src/scripts/prunjava

chmod a+x tests/ccl/RunTests
chmod a+x tests/ccl_ObjSer/RunTests
chmod a+x tests/comm/RunTests
chmod a+x tests/dtyp/RunTests
chmod a+x tests/dtyp_ObjSer/RunTests
chmod a+x tests/env/RunTests
chmod a+x tests/group/RunTests
chmod a+x tests/pt2pt/RunTests
chmod a+x tests/pt2pt_ObjSer/RunTests
chmod a+x tests/topo/RunTests

chmod a+x examples/simple/RunTests
chmod a+x examples/PingPong/RunBench
chmod a+x examples/metropolis/RunIsing
chmod a+x examples/potts/RunIsing
chmod a+x examples/Nozzle/RunNozzle
#chmod a+x examples/swendsen_wang/RunIsing

rm -f ./config.cache

