#!/bin/sh
#
# $COPYRIGHT$
#
# This script needs to be built by configure.
#
# This is a script to install ROMIO. It can be invoked with 
#    make install
#
#    (if you used -prefix at configure time) or,
#
#    make install PREFIX=/usr/local/romio  (or whatever directory you like)
#
# in the top-level romio directory
#

ROMIO_HOME="@ROMIO_HOME@"
RANLIB="@RANLIB@"

#
# Need the top build dir and top src dir as well
#

ROMIO_TOP_SRCDIR="$ROMIO_HOME"
ROMIO_TOP_BUILDDIR="`pwd`"

# Short version: LAM's configure sets values like $datadir and whatnot
# in the file $ROMIO_TOP_BUILDDIR/util/lam-configure-values, so we have to
# source it to get them here.  See romio/util/lam-configure-values.in
# for the full story.

. $ROMIO_TOP_BUILDDIR/util/lam-configure-values

WANT_INSTALL=1

for arg in "$@" ; do
    case "$arg" in 
        -prefix=*)
            prefix=`echo $arg | sed -e 's/-prefix=//'`
            ;;
        -datadir=*)
            datadir=`echo $arg | sed -e 's/-datadir=//'`
            ;;
        -libdir=*)
            libdir=`echo $arg | sed -e 's/-libdir=//'`
            ;;
        -includedir=*)
            includedir=`echo $arg | sed -e 's/-includedir=//'`
            ;;
        -mandir=*)
            mandir=`echo $arg | sed -e 's/-mandir=//'`
            ;;
        -uninstall)
            WANT_INSTALL=0
            ;;
        *)
            echo "romioinstall: Unrecognized argument $arg ."
            exit 1
            ;;
    esac
done
if test -z "$prefix" ; then
    echo "Usage: make install prefix=/usr/local/romio  (or whatever directory you like)"
    echo "in the top-level romio directory"
fi

#
# Subroutine to make a directory
#
make_dir() {
    if test ! -d "$1"; then
	parent="`dirname $1`"
	make_dir "$parent"

	mkdir "$1"
	chmod 755 "$1"
    fi
}

#
# Append an individual file to the list of files to copy
#
append() {
    files="$files $1";
}

#
# Subroutine to add a whole directory of files
#
append_files() {
    src_dir="$1"
    dest_dir="$2"
    filemask="$3"

    cd "$src_dir"
    for file in `ls $filemask`; do
	append "$src_dir/$file:$dest_dir/$file"
    done
    cd "$top"
}

#
# LAM top-level switch to install or uninstall
#

# Find out where we are
top="`pwd`"

# Some common directories
testdir="$datadir/lam"
docdir="$datadir/lam/doc"

# The master list of files to copy over
files=

# Documentation files
dir="$docdir"
append "$ROMIO_TOP_SRCDIR/COPYRIGHT:$dir/ROMIO-COPYRIGHT"
append "$ROMIO_TOP_SRCDIR/doc/README:$dir/ROMIO-README"
append "$ROMIO_TOP_SRCDIR/README_LAM:$dir/ROMIO-README_LAM"
append "$ROMIO_TOP_SRCDIR/doc/users-guide.ps.gz:$dir/romio-users-guide.ps.gz"

# Include files
dir="$includedir"
append "$ROMIO_TOP_BUILDDIR/include/mpio.h:$dir/mpio.h"
append "$ROMIO_TOP_BUILDDIR/include/mpiof.h:$dir/mpiof.h"

# Library files
dir="$libdir"
append "$ROMIO_TOP_BUILDDIR/lib/liblammpio.a:$dir/liblammpio.a"

# Man pages
dir="$mandir/man3"
append_files "$ROMIO_TOP_SRCDIR/man/man3" "$dir" "*.3"

# Example / test programs
# Executive decision (JMS): Per advice from RedHat, let's not install
# these.  Leave them in the source tree.
#dir="$testdir"
#append_files "$ROMIO_TOP_SRCDIR/test/std" "$dir/std" "*.std"
#append "$ROMIO_TOP_BUILDDIR/test/Makefile:$dir/Makefile"
#append "$ROMIO_TOP_BUILDDIR/test/runtests:$dir/std/runtests"
#append_files "$ROMIO_TOP_BUILDDIR/test" "$dir" "*.c"
#if test -f $ROMIO_TOP_BUILDDIR/test/fperf.f; then
#    append_files "$ROMIO_TOP_BUILDDIR/test" "$dir" "*.f"
#fi

#
# Do the install or uninstall
#

for pair in $files; do
    src="`echo $pair | cut -d: -f1`"
    dest="`echo $pair | cut -d: -f2`"
    if test "$WANT_INSTALL" = "1"; then
	dir="`dirname $dest`"
	if test ! -d $dir; then
	    make_dir $dir
	fi

	echo "Installing: $dest"
	cp $src $dest
	chmod 644 $dest
    else
	echo "Removing: $dest"
	if test -f $dest; then
	    rm -f $dest
	fi
    fi
done

#
# Final steps
#

if test "$WANT_INSTALL" = "1"; then

    # Run ranlib on the library, if we have to

    $RANLIB "$libdir/liblammpio.a"

    # Need to change the Makefile in the test/example directory to
    # remove the LAM-building-specific flags.

# Unnecessary since we aren't installing the test/example programs.

#    sed -e 's/CC[ ]*= [ /a-z0-9.A-Z_-]*/CC = mpicc/' \
#        -e 's/F77[ ]*= [ /a-z0-9.A-Z_-]*/F77 = mpif77/' \
#        -e 's/INCLUDE_DIR[ ]*= [ /a-z0-9.A-Z_-]*/INCLUDE_DIR = /' \
#        -e 's/LIBS[ ]*= [ /a-z0-9.A-Z_-]*/LIBS = /' \
#        -e 's/-lam-building//' \
#	"$testdir/Makefile" > "$testdir/.romiotmp"

#    mv "$testdir/.romiotmp" "$testdir/Makefile"
#    chmod 644 "$testdir/Makefile"

    # Make the "runtests" script executable

#    chmod a+x "$testdir/std/runtests"

    # All done!

    echo "ROMIO has been installed"
else
    echo "ROMIO has been uninstalled"
fi


syntax highlighted by Code2HTML, v. 0.9.1