#!/bin/sh
# Do the HDF5 library configure, make, and tests for Blue Gene/P
# Usage: bgmakeh5 

USAGE()
{
cat <<EOF
Make HDF5 library for Blue Gene Machine.
Buildhdf5 builds the HDF5 library by running configure, make and make check.
It skips the configure part if one has been done already.  In effect, one
can continue from a previous build.

Command Syntax
==============
$0 [-config] [-szlib] [-help] [-n] [-srcdir dir] [-fortran] [-cxx] [-pp] config-arguments ...
   -config:	run configure only. [default to do build too]
   -szlib:      configure in the szlib option
   -help:	show this help page
   -n:   	no execution, just show commands
   -srcdir:	use dir as the source directory
             	   [Note: this is different from --srcdir
             	    which will be passed to configure]
   -fortran:	add --enable-fortran
   -cxx:	add --enable-cxx
   -pp:		add --enable-parallel
   all other arguments are passed to configure


Configure in place or by srcdir
===============================
By default, the command looks for the configure command in
'.' and then '../hdf5'.  When it finds it, it uses it to do
the configure part.  In effect, if ./configure is found, it
does the build in place.  If it finds ../hdf5/configure, it
does the --srcdir (that is separated source) build.  Therefore,
if you have the following structure setup, you can run multiple
hosts building simultantously using a common source code.
     hdf5_v1.7/hdf5       # holds the source
	  .../sunbox      # for SunOS
	  .../linux       # for Linux
	  .../linuxpp     # for Linux parallel
EOF
}
# Use single gmake because mpirun can only run one at a time
MAKE=gmake
export MAKE
SRCDIR=${SRCDIR:-.}
export SRCDIR
# hard set cc, fc and cxx because system /etc/cshrc.local has errors
# that keeps adding /usr/bin to $path.
export CC=${CC:-mpixlc}
export FC=${FC:-mpixlf90}
export CXX=${CXX:-mpixlcxx}
export FCFLAGS="-O3 -g -qsimd=auto -qhot=level=1 -qprefetch -qunroll=yes"
export CCFLAGS="-O3 -g -qsimd=auto -qhot=level=1 -qprefetch -qunroll=yes"
export RUNSERIAL=${RUNSERIAL:./}
export hdf5_cv_system_scope_threads=no
export hdf5_cv_gettimeofday_tz=yes
export hdf5_cv_vsnprintf_works=yes
export hdf5_cv_printf_ll=yes
export hdf5_cv_have_lfs=yes
export RUNPARALLEL=""
# definitely keep these two set or file a bug with your MPI vendor
# huh! actually, HDF5 seems to ask MPI to do bad things (free built-in type) if
# these are set
export hdf5_cv_mpi_special_collective_io_works=yes
export hdf5_cv_mpi_complex_derived_datatype_works=no

export hdf5_ldouble_to_integer_works=yes
export hdf5_cv_ldouble_to_integer_works=yes
export hdf5_cv_ulong_to_fp_bottom_bit_accurate=yes
# set this to 'no' for the PGI compiler 
export hdf5_fp_to_ullong_accurate=yes
export hdf5_cv_fp_to_ullong_accurate=yes
# don't set this on HPUX:
export hdf5_cv_ulong_to_float_accurate=yes
export hdf5_fp_to_ullong_right_maximum=yes
export hdf5_cv_fp_to_ullong_right_maximum=yes
export hdf5_ldouble_to_uint_accurate=yes
export hdf5_cv_ldouble_to_uint_accurate=yes
export hdf5_cv_ullong_to_ldouble_precision=yes
# don't set this on cray X1
export hdf5_cv_fp_to_integer_overflow_works=yes
export hdf5_ldouble_to_llong_accurate=yes
export hdf5_cv_ldouble_to_llong_accurate=yes
export hdf5_llong_to_ldouble_correct=yes
export hdf5_cv_llong_to_ldouble_correct=yes

# only set this on power6 systems..
export hdf5_cv_ldouble_to_long_special=no
export hdf5_cv_long_to_ldouble_special=no

# TESTS_ENVIRONMENT tells it how to run the tests
# Can't see where TESTS_ENVIRONMENT is used.
export TESTS_ENVIRONMENT=$RUNSERIAL
echo export TESTS_ENVIRONMENT=$RUNSERIAL

# Variables initialize
# Shared lib does not work.
CONFIG_OP="--disable-shared "


# Main
# Parse command options
while [ $# -gt 0 ]; do
    case "$1" in
	-config)
	    # do configure only
	    CONFIG_ONLY=yes
	    ;;
	-szlib)
	    WITHSZLIB=--with-szlib=/usr/gapps/silo/szip/2.1/sles_10_ppc64-xlc
	    CONFIG_OP="$CONFIG_OP $WITHSZLIB"
	    ;;
	-zlib)
	    WITHZLIB=--with-zlib=/usr/gapps/silo/zlib/1.2.3/sles_10_ppc64-xlc
	    CONFIG_OP="$CONFIG_OP $WITHZLIB"
	    ;;
	-help)
	    USAGE
	    exit 0
	    ;;
	-n)
	    NOEXEC='noexec'
	    ;;
#	-srcdir)
#	    shift
#	    SRCDIRLIST="$1"
#	    ;;
	-cxx)
	    CONFIG_OP="$CONFIG_OP --enable-cxx"
	    ;;
	-fortran)
	    CONFIG_OP="$CONFIG_OP --enable-fortran"
	    ;;
	-pp)
	    CONFIG_OP="$CONFIG_OP --enable-parallel"
	    ;;
	*)	# pass it as a configure option
	    CONFIG_OP="$CONFIG_OP $1"
	    ;;
    esac
    shift
done
	    
# setup yodconfigure
cp $SRCDIR/configure $SRCDIR/configure.yod
$SRCDIR/bin/yodconfigure $SRCDIR/configure.yod
CONFIGURE="$SRCDIR/configure.yod $CONFIG_OP"

# Some handy definitions
TIMESTAMP()
{
    echo "=====" "`date`" "====="
}


# Do one step bracketed with time stamps
STEP()
{
    banner="$1"
    command="$2"
    resultfile="$3"

    echo "$banner"
    (TIMESTAMP; nerror=0 ;
	sh -c $command || nerror=1 ;
	TIMESTAMP; exit $nerror) >> "$resultfile" 2>&1
    if [ $? -ne 0 ]; then
	echo "error in '$banner'.  makeh5 aborted."
	exit 1
    fi
}


# Main body

# Setup exit steps and print starting time
trap TIMESTAMP 0
TIMESTAMP

# Make sure we are at the library root level
# by checking couple typical files.  Not bullet-proof.
if [ ! \( -d $SRCDIR/src -a -d $SRCDIR/config -a -f $SRCDIR/configure \) ]
then
    echo "Could not find the source dir or configure script.  Abort."
    exit 1
fi


# no configure if already done.
if [ ! -f config.status ]; then
    echo "$CONFIGURE $@ ..."
    (TIMESTAMP; nerror=0;
	$CONFIGURE $@ < /dev/null || nerror=1;
	TIMESTAMP; exit $nerror) > '#config' 2>&1
    if [ $? -ne 0 ]; then
	echo "error in Configure.  Aborted."
	exit 1
    fi
else
    echo configure.status already exists. Skip configure.
fi

# need to edit the src/H5pubconf.h file.
# fork() compile okay but does not exec.
xfile=src/H5pubconf.h
for macro in H5_HAVE_FORK; do
    echo comment out $macro from $xfile 
    ed -s $xfile <<EOF
/^\#define ${macro} 1/s/.*/\/\* & \*\//p
w
q
EOF
done

# Generate two .c files that require mpirun execution
( cd src; gmake H5lib_settings.c H5Tinit.c ) >> '#make' 2>&1

# Compile the library
# Okay to use parallel gmake for compiling
gmake -i -j 8 >> '#make' 2>&1
gmake -i install
#test_log="#test.$$"
# Do make check
#env HDF5_Make_Ignore=yes gmake check > $test_log 2>&1

#if grep FAILED $test_log; then
#    echo "***FAILED detected in " $test_log
#fi
