#!/bin/sh
# This file is configure.
#
# Make all changes to configure.in, if at all possible.  After changing,
# run "autoconf".  If autoconf is not installed, then make changes to
# configure.
#
# Documentation on the autoconf program exists in GNU info file format.
# To read this, run emacs and type C-h i.  Then press the down arrow
# until you come to the line beginning "* Autoconf:".  Pressing enter on
# this line will bring up the documentaition.
#
# Type "configure" (i.e., no arguments) or read below for usage information.
#
print_error() {
    echo "$*" 2>&1 ;
}

print_usage()
{
cat <<.
Usage: configure -arch ARCH_TYPE [-cc CC] [-f77 FC] [-debug] [-help]
                 [-cflags CFLAGS] [-fflags FFLAGS]
                 [-mpiinc MPI_INC_DIR -mpilib MPI_LIB]
where
   ARCH_TYPE    = the type of machine that PGAPack is to be configured for
   CC           = the name of the ANSI C compiler
   FC           = the name of the FORTRAN 77 compiler
   CFLAGS       = C compiler flags
   FFLAGS       = FORTRAN compiler flags
   MPI_INC_DIR  = the MPI include directory
   MPI_LIB      = the full path to the MPI library


Known architectures are:

Workstations
	sun4		(SUN OS 4.x)
	hpux		(HP UX)
	irix    	(Silicon Graphics IRIX)
	alpha		(DEC Alpha)
	rs6000		(AIX for IBM RS6000)	
	next		(NeXT 680x0)
	freebsd		(PC clones running FreeBSD)
	linux		(PC clones running LINUX)
	generic		(Generic 32-bit UNIX-like machine)

MPP's
	powerchallenge	(Silicon Graphics PowerChallenge)
	challenge	(Silicon Graphics Challenge)
	t3d		(Cray-T3D)
	sp2		(IBM SP2)
	paragon		(Intel Paragon)
	exemplar	(Convex Exemplar)


In the absence of the -f77 or -cc options, configure assumes the name of 
a FORTRAN 77 or ANSI C compiler based on the architecture type.

If the -debug flag is set, then PGAPack will be built for debugging.  This
enables the extensive debugging options and datatype checks.

If both -mpiinc and -mpilib are missing, PGAPack is built using a non-parallel
"stub" library for all MPI calls.

The -help flag causes this usage guide to be printed.

Example 1:
----------
Configure and build for parallel operation on the sun4 architectue.  MPI is
in /usr/local/mpi; the MPI library is /usr/local/mpi/lib/sun4/ch_p4/libmpi.a.

  configure -arch sun4 -mpilib /usr/local/mpi/lib/sun4/ch_p4/libmpi.a \
            -mpiinc /usr/local/mpi/include
  make install

Example 2:
----------
Configure and build a debug sequential library on the rs6000 architecture.

  configure -arch rs6000 -debug
  make install
.
}

###############################################################################
# This section initializes the variables that will be used throughout
# the script.
###############################################################################
AC_INIT(source/pga.c)
ARCH=""                         # architecture type
PARALLEL=0                      # parallel libray, or use stub routines?
OPTIMIZE=1                      # optimized?
CPPFLAGS=""                     # flags to send to the C preprocessor
LDFLAGS=""                      # link flags
CFLAGS=""                       # flags to send to the C compiler
FFLAGS=""                       # flags to send to the FORTRAN compiler
PGA_DIR="`pwd`"                 # the root of the pgapack tree
PGA_DIR=`echo $PGA_DIR | sed "s=tmp_mnt/=="`    # get rid of 'tmp_mnt/'
HEADERS="$PGA_DIR/include/pgapack.h" # headers upon which pgapack depends
INCLUDES="-I$PGA_DIR/include"   # directories where to search for include files
LIB_DIRS=""                     # directories where to search for libraries
LIBS=""                         # libraries with which to link
MPI_INC_DIR=""
MPI_LIB=""
SHELL="/usr/bin/sh"
RM="/bin/rm -f"                 # command to force removal of files

OBJS='$(PGA_LIB_DIR)/binary.o        \\\
      $(PGA_LIB_DIR)/char.o          \\\
      $(PGA_LIB_DIR)/cmdline.o       \\\
      $(PGA_LIB_DIR)/create.o        \\\
      $(PGA_LIB_DIR)/cross.o         \\\
      $(PGA_LIB_DIR)/debug.o         \\\
      $(PGA_LIB_DIR)/duplcate.o      \\\
      $(PGA_LIB_DIR)/evaluate.o      \\\
      $(PGA_LIB_DIR)/fitness.o       \\\
      $(PGA_LIB_DIR)/hamming.o       \\\
      $(PGA_LIB_DIR)/heap.o          \\\
      $(PGA_LIB_DIR)/integer.o       \\\
      $(PGA_LIB_DIR)/mutation.o      \\\
      $(PGA_LIB_DIR)/parallel.o      \\\
      $(PGA_LIB_DIR)/pga.o           \\\
      $(PGA_LIB_DIR)/pop.o           \\\
      $(PGA_LIB_DIR)/random.o        \\\
      $(PGA_LIB_DIR)/real.o          \\\
      $(PGA_LIB_DIR)/report.o        \\\
      $(PGA_LIB_DIR)/restart.o       \\\
      $(PGA_LIB_DIR)/select.o        \\\
      $(PGA_LIB_DIR)/stop.o          \\\
      $(PGA_LIB_DIR)/system.o        \\\
      $(PGA_LIB_DIR)/user.o          \\\
      $(PGA_LIB_DIR)/utility.o'      # these are pgapack's object files

###############################################################################
# This section parse the command line options. There a two types of options:
# those that take arguments and those that don't.  Parsing the case where an
# option takes an argument is a little tricky.  This is how it works.  Suppose
# that the program comes upon "-cc".  Then we know that the next argument
# will be the name of the C compiler.  Hence, we set the flag next_cc to be
# equal to "yes".  The next time through the loop, it will be the case that
# x$next_cc equal "xyes" because $next_cc expands to "yes".  Thus, we know that
# this argument is the name of the compiler and we assign it to USR_CC.  Then
# we set next_cc back to a null string.  This way on the next time through
# the loop x$next_cc will equal just "x".  Since the shell initializes
# variables to null strings, we do not have to initialize the "next_" family
# of variables by hand.
###############################################################################
for arg
do
    if test x$next_arch = xyes
        then
            ARCH=$arg
            next_arch=
        elif test x$next_mpiinc = xyes
            then
                MPI_INC_DIR=$arg
                next_mpiinc=
        elif test x$next_mpilib = xyes
            then
                MPI_LIB=$arg
                next_mpilib=
        elif test x$next_f77 = xyes
            then
                USR_FC=$arg
                next_f77=
        elif test x$next_cc = xyes
            then
                USR_CC=$arg
                next_cc=
        elif test x$next_cflags = xyes
            then
                CFLAGS="$arg $CFLAGS"
                next_cflags=
        elif test x$next_fflags = xyes
            then
                FFLAGS="$arg $FFLAGS"
                next_fflags=
        else
            case $arg in
                -arch)
                    next_arch=yes
                    ;;
                -f77)
                    next_f77=yes
                    ;;
                -cc)
                    next_cc=yes
                    ;;
                -mpiinc)
                    next_mpiinc=yes
                    PARALLEL=1
                    ;;
                -mpilib)
                    next_mpilib=yes
                    PARALLEL=1
                    ;;
                -debug)
                    OPTIMIZE=0
                    ;;
                -cflags)
                    next_cflags=yes
                    ;;
                -fflags)
                    next_fflags=yes
                    ;;
                -help)
                    print_usage >& 2
                    exit 1
                    ;;
                *) 
                    ;;
            esac
    fi
done

if test "$MPI_LIB" -a "$MPI_INC_DIR" ; then
    if test ! -d "$MPI_INC_DIR" ; then
        print_error "MPI_INC_DIR == $MPI_INC_DIR is not a valid directory!"
        exit 1
    fi
    if test ! -f "$MPI_INC_DIR/mpi.h" ; then
        print_error "Couldn't find $MPI_INC_DIR/mpi.h!"
        exit 1
    fi

    if test ! -f "$MPI_LIB" ; then
        print_error "MPI_LIB == $MPI_LIB is not a file!"
        exit 1
    fi

    PARALLEL=1
else
    PARALLEL=0
    CPPFLAGS="-DFAKE_MPI $CPPFLAGS"
fi

if test $OPTIMIZE -eq 1 ; then
	CPPFLAGS="-DOPTIMIZE $CPPFLAGS"
	CFLAGS="-O $CFLAGS"
	FFLAGS="-O $FFLAGS"
	PGA_LIB="pgaO"
else
	CFLAGS="-g $CFLAGS"
	FFLAGS="-g $FFLAGS"
	PGA_LIB="pgag"
fi


###############################################################################
# This section sets the default values of certain variables based upon the
# architecture type specified.  If no architecture was specified, the script
# issues an error and aborts.
###############################################################################
if test -z "$ARCH"
    then
        print_error "You must specify the architecture with -arch <value>"
        print_error "Valid architectures are the following:"	
	print_error " Workstations      MPP's"
	print_error "    sun4        powerchallenge"
	print_error "    next        challenge"
	print_error "    rs6000      t3d"
	print_error "    freebsd     sp2"
	print_error "    irix        paragon"
	print_error "    linux       exemplar"
	print_error "    hpux"
	print_error "    alpha"
	print_error "    generic"
	print_error ""
	print_error "For more information type configure -help."
        exit 1
fi

case $ARCH in
    sun4)
        CC=cc
        FC=f77
        CPPFLAGS="-DWL=32 -DFORTRANUNDERSCORE $CPPFLAGS"
        ;;
    next)
        CC=cc
        FC=
        CPPFLAGS="-DWL=32 -DFORTRANUNDERSCORE $CPPFLAGS"
        ;;
    rs6000)
        CC=cc
        FC=f77
        CPPFLAGS="-DWL=32 $CPPFLAGS"
        ;;
    freebsd)
        CC=cc
        FC=f77
	FFLAGS="-w"
        CPPFLAGS="-DWL=32 -DFORTRANUNDERSCORE $CPPFLAGS"
        ;;
    irix)
        CC=cc
        FC=f77
        CPPFLAGS="-DWL=32 -DFORTRANUNDERSCORE $CPPFLAGS"
        ;;
    alpha)
        CC=cc
        FC=f77
        CPPFLAGS="-DWL=64 -DFORTRANUNDERSCORE $CPPFLAGS"
        ;;
    hpux)
        CC=cc
        FC=f77
        CPPFLAGS="-DWL=32 $CPPFLAGS"
	SHELL=/bin/sh
        ;;
    linux)
	SHELL="/bin/sh"
        CC=cc
        FC=f77
	FFLAGS="-w"
	LDFLAGS="-s $LDFLAGS"
        #CPPFLAGS="-DWL=32 -DFORTRANUNDERSCORE $CPPFLAGS"
	wordlength=$(sh debian/computeWordlength.sh)
        CPPFLAGS="${wordlength} -DFORTRANUNDERSCORE $CPPFLAGS"
        ;;
    t3d)
        CC=/mpp/bin/cc
        FC=/mpp/bin/cf77
        CFLAGS="-T cray-t3d"
        FFLAGS="-C cray-t3d -dp"
        CPPFLAGS="-DWL=64 -DFORTRANCAP $CPPFLAGS"
	SHELL=/bin/sh
        ;;
    powerchallenge)
        CC=cc
        FC=f77
	CFLAGS="-mips4 -fullwarn -64"
        CPPFLAGS="-DWL=64 -DFORTRANUNDERSCORE $CPPFLAGS"
        ;;
    challenge)
        CC=cc
        FC=f77
	CFLAGS="-mips2 -fullwarn -32"
        CPPFLAGS="-DWL=32 -DFORTRANUNDERSCORE $CPPFLAGS"
        ;;
    paragon)
        CC=cc
        FC=f77
        CFLAGS="-nx $CFLAGS"
        FFLAGS="-nx $FFLAGS"
        CPPFLAGS="-DWL=32 -DFORTRANUNDERSCORE $CPPFLAGS"
        ;;
    sp2)
        CC=cc
        FC=f77
        CPPFLAGS="-DWL=32 $CPPFLAGS"
        ;;
    exemplar)
        CC=cc
        FC=fort77
	FFLAGS="-L/usr/lib -lU77 $FFLAGS"
        CPPFLAGS="-DWL=32 $CPPFLAGS"
	SHELL=/bin/sh
        ;;
    *)
	CC=cc
	FC=f77
	CPPFLAGS="-DWL=32 $CPPFLAGS"
	print_error "WARNING:  Configuring for untested architecture.  Using"
        print_error "          word size of 32 bits, cc and f77 as compilers."
	print_error "          You might have trouble with Fortran programs"
        print_error "          not linking correctly.  Check the manual on"
        print_error "          the FORTRANUNDERSCORE #define."
	;;
esac

###############################################################################
# This section sets the variables that do not relate to parallel code.
###############################################################################
CPPFLAGS="-D$ARCH $CPPFLAGS"
PGA_LIB_DIR="../lib/$ARCH"
LIB_DIRS="-L$PGA_DIR/lib/$ARCH $LIB_DIRS"
LIBS="-l$PGA_LIB $LIBS"

#     Set output variable `RANLIB' to `ranlib' if `ranlib' is found,
#     otherwise to `:' (do nothing).
AC_PROG_RANLIB()

#    "Determine a C compiler to use.  If `CC' is not already set in the
#     environment, check for `gcc', and use `cc' if it's not found.  Set
#     output variable `CC' to the name of the compiler found.
#
#    "If using the GNU C compiler, set shell variable `GCC' to `yes',
#     empty otherwise.  If output variable `CFLAGS' was not already set,
#     set it to `-g -O' for the GNU C compiler (`-O' on systems where
#     GCC does not accept `-g'), or `-g' for other compilers."
if test -n "$USR_CC"
     then
          CC=$USR_CC
     else
          AC_PROG_CC()
fi

#     Check for each program in the whitespace-separated list
#     PROGS-TO-CHECK-FOR exists in `PATH'.  If it is found, set VARIABLE
#     to the name of that program.  Otherwise, continue checking the
#     next program in the list.  If none of the programs in the list are
#     found, set VARIABLE to VALUE-IF-NOT-FOUND; if VALUE-IF-NOT-FOUND
#     is not specified, the value of VARIABLE is not changed.
if test -n "$USR_FC"
     then
          FC=$USR_FC
     else
          AC_PROGRAMS_CHECK(FC, f77 fortran xlf gf77 mpxlf if77)
fi

# Originally had
#                     OBJS="$OBJS $(PGA_LIB_DIR)/f2c.o"
# However, freebsd bourne shell equates $(var) and `var`.  Hence, I had
# to break the assignment up into two parts.  $FORTWRAP is a Makefile
# dependency line defining how to compile f2c.c
if test -n "$FC"
    then
	TEMP='$(PGA_LIB_DIR)/f2c.o'
        OBJS="$OBJS $TEMP"
        FORTWRAP='$(PGA_LIB_DIR)/f2c.o: f2c.c $(HEADERS)\
	$(COMPILE.c) f2c.c'
fi

if test ! -d lib
    then
        mkdir lib
fi

if test ! -d lib/$ARCH
    then
        mkdir lib/$ARCH
fi

#  Set up symlinks for pgapackf.h to any directories with Fortran source
rm -f ./examples/fortran/pgapackf.h ./examples/mgh/pgapackf.h ./test/pgapackf.h
ln -s ../../include/pgapackf.h ./examples/fortran/pgapackf.h
ln -s ../../include/pgapackf.h ./examples/mgh/pgapackf.h
ln -s ../include/pgapackf.h    ./test/pgapackf.h

#  Use the stub library.
if test $PARALLEL -eq 0
  then
    rm -f ./include/mpi.h ./include/mpif.h ./examples/fortran/mpif.h ./examples/mgh/mpif.h ./test/mpif.h
    ln -s fakempi_h include/mpi.h
    ln -s fakempif_h include/mpif.h
    ln -s ../../include/mpif.h ./examples/fortran/mpif.h
    ln -s ../../include/mpif.h ./examples/mgh/mpif.h
    ln -s ../include/mpif.h ./test/mpif.h
    TEMP='$(PGA_LIB_DIR)/mpi_stub.o'
    OBJS="$OBJS $TEMP"
    MPICOMP='$(PGA_LIB_DIR)/mpi_stub.o: mpi_stub.c $(HEADERS)\
	$(COMPILE.c) mpi_stub.c'
#  The tab above is IMPORTANT!  Needed for make!
else
    rm -f ./include/mpi.h ./include/mpif.h ./examples/fortran/mpif.h ./examples/mgh/mpif.h ./test/mpif.h
    ln -s $MPI_INC_DIR/mpif.h ./examples/fortran/mpif.h
    ln -s $MPI_INC_DIR/mpif.h ./examples/mgh/mpif.h
    ln -s $MPI_INC_DIR/mpif.h ./test/mpif.h
    LIBS="$LIBS $MPI_LIB"
    INCLUDES="$INCLUDES -I$MPI_INC_DIR"
fi

LIBS="$LIBS -lm"
CPPFLAGS="$INCLUDES $CPPFLAGS"
LDFLAGS="$LDFLAGS $LIB_DIRS $LIBS"
###############################################################################
# This section exports the variables that have previously been set.
# Configure creates the Makefiles listed on the last line from the
# Makefile.in in each directory.  It does this by substituting the value
# of $VAR for every occurent of @VAR@ in the Makefile.in.  For
# example, if the characters @CC@ occur any place in a Makefile.in, they
# will be replaced with the value that $CC has when ac_subst(CC) is
# called.
###############################################################################
AC_SUBST(CC)
AC_SUBST(CFLAGS)
AC_SUBST(FFLAGS)
AC_SUBST(CPPFLAGS)
AC_SUBST(FC)
AC_SUBST(FORTWRAP)
AC_SUBST(HEADERS)
AC_SUBST(LDFLAGS)
AC_SUBST(PARALLEL)
AC_SUBST(PGA_LIB_DIR)
AC_SUBST(PGA_LIB)
AC_SUBST(OBJS)
AC_SUBST(MPICOMP)
AC_SUBST(RM)
AC_SUBST(RANLIB)
AC_SUBST(SHELL)
AC_OUTPUT(Makefile source/Makefile test/Makefile test/Makefile \
	  examples/Makefile examples/c/Makefile examples/fortran/Makefile \
	  examples/mgh/Makefile)

if test ! -f ".pgapack" ; then
  if test $PARALLEL -eq 1 ; then
      OPERATION="parallel"
      ECHO_LIBS="$MPI_LIB"
  else
      OPERATION="sequential"
      ECHO_LIBS="stub routines in mpi_stub.c"
  fi
  if test $OPTIMIZE -eq 1 ; then
      OPERATION="optimized $OPERATION"
  else
      OPERATION="debug $OPERATION"
  fi
  echo " "
  echo "PGAPack has been configured for $OPERATION operation on"
  echo "the $ARCH architecture, using $ECHO_LIBS."
  echo " "
  echo "Type \"make install\" to install PGAPack."
  echo " "
fi


