#! /bin/sh
#
# This script has to be built by configure.
#
# This is a script to install MPE Profiling Environment. It can be invoked with
#
#    make installcheck  ( if you used -prefix at configure time )
#
#    or,
#
#    make installcheck PREFIX=Path_to_the_installation_of_MPE
#
# in the top-level MPE __build__ directory which could be the same
# or different from the source directory
#

MAKE="make --no-print-directory"

# Location of sources
# top_srcdir=/proj/tpfimos/larour/Libraries/linux/mpich2-1.0.2p1/src/mpe2
top_srcdir=/proj/tpfimos/larour/Libraries/linux/mpich2-1.0.2p1/src/mpe2       # in absolute path

# Build locations
includebuild_dir=/proj/tpfimos/larour/Libraries/linux/mpich2-1.0.2p1/src/mpe2/include
libbuild_dir=/proj/tpfimos/larour/Libraries/linux/mpich2-1.0.2p1/src/mpe2/lib
binbuild_dir=/proj/tpfimos/larour/Libraries/linux/mpich2-1.0.2p1/src/mpe2/bin
sbinbuild_dir=/proj/tpfimos/larour/Libraries/linux/mpich2-1.0.2p1/src/mpe2/sbin

# Installation directories: Default paths (set at configure time)
prefix=/proj/tpfimos/larour/Libraries/linux/mpich2-gnu/
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib
bindir=${exec_prefix}/bin
sbindir=${exec_prefix}/sbin
datadir=${prefix}/share
sysconfdir=${prefix}/etc

# Assume PROF_LIBNAME=MPE_LIBNAME.
PROF_LIBNAME=mpe
CPRP="cp"
RM="rm"
LN_S="ln -s"
MPI_IMPL=MPICH2
MPE_BUILD_FORTRAN2C=yes

Show=eval
prefix_override=0

for arg in "$@" ; do
    case "$arg" in
        -prefix=*)
            prefix=`echo $arg | sed -e 's/-prefix=//g'`
            if [ "$prefix" != "/proj/tpfimos/larour/Libraries/linux/mpich2-gnu/" ] ; then
                prefix_override=1
            fi
            ;;
        -t)
            Show=echo
            ;;
        -echo)                    set -x ;;
        -help|-u|-usage|-h)
cat <<EOF
Check MPE installed at $prefix.
-prefix=path     - Destination directory.
-t               - Try only; do no actual checking.
EOF
            exit 1
            ;;
        *)
            # Silently skip empty arguments (these can occur on
            # some systems and within some scripts; they are
            # harmless)
            if [ -n "$arg" ] ; then
                echo "mpeinstallcheck: Unrecognized argument $arg ."
                exit 1
            fi
            ;;
    esac
done

if [ "$SHELL_ECHO" = "on" ] ; then
    set -x
fi

# Check if installation location is set
if [ ! -n "$prefix" ] ; then
    echo "Set an installation location with -prefix=<location> ."
    exit 1
fi

# If the prefix was set, we want to override these choices
if [ "$prefix_override" = 1 ] ; then
    includedir=$prefix/include
    libdir=$prefix/lib
    bindir=$prefix/bin
    sbindir=$prefix/sbin
    datadir=$prefix/share
fi

if [ ! -s $libdir/lib${PROF_LIBNAME}.a ] ; then
    echo "Could not find $libdir/lib${PROF_LIBNAME}.a !"
    echo "You must install MPE before checking the installation!"
    echo "Try to do make install or mpeinstall first."
    exit 1
fi

# Set up Examples directory for the MPE Logging programs
logging_dirname="src/wrappers/test"
graphics_dirname="src/graphics/contrib/test"
for pkg in logging graphics ; do
    dirname=${pkg}_dirname
    eval dirvalue=\$"$dirname"
    examplesdir=$datadir/examples_${pkg}
    if [ -f $examplesdir/Makefile ] ; then
        echo "Running installation test for C $pkg program..."
        $Show "( cd $examplesdir && $MAKE linktest_C )"
        if test "$MPE_BUILD_FORTRAN2C" = "yes" ; then
            echo "Running installation test for Fortran $pkg program..."
            $Show "( cd $examplesdir && $MAKE linktest_F77 )"
        fi
    fi
done
