#!/bin/sh
#
# 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
#
PREFIX=@prefix@
ROMIO_HOME=@ROMIO_HOME@
ARCH=@ARCH@
RANLIB=@RANLIB@
TOP_BUILD_DIR=@top_build_dir@
# LAM Do we want to install or uninstall?
WANT_INSTALL=1
#
# Default paths (set at configure time)
prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
sbindir=@sbindir@
includedir=@includedir@
#sysconfdir=@sysconfdir@
libdir=@libdir@
#sharedlib_dir=@sharedlib_dir@
mandir=@mandir@
htmldir=@htmldir@
# datarootdir required as of autoconf 2.60
datarootdir=@datatrootdir@
#datadir=@datadir@
docdir=@docdir@
## Location of sources
#top_srcdir=@top_srcdir@
#
# File access mode
MODE=0644
XMODE=0755
replace=1
verbose=0

failmode=soft

# Shell procedures to copy files and create directories
#
# We could use install, but install is too different and too hard to 
# test.  So here are the routines to copy file, make directories, and 
# replace #...# and @...@ in files
CP=cp
#
MkDir() {
    if [ ! -e $1 ]; then mkdir $1 ; fi
}
CopyFile() {
if [ -z "$3" ] ; then
mode=$MODE
else
mode=$3
fi
if [ -d $2 ] ; then
dest=$2/`basename $1`
else
    dest=$2
fi
if [ $replace = 0 -a -f $dest ] ; then
    if [ $verbose = 1 ] ; then echo "$dest exists; not changed" ; fi
elif [ -d $1 ] ; then
    echo ">>> $1 is a directory; not copied <<<"
    errs=`expr $errs + 1`
    if [ $failmode = "hard" ] ; then exit 1 ; fi
elif [ ! -f $1 ] ; then 
    echo "**File $1 does not exist (or is not a regular file)!"
    errs=`expr $errs + 1`
    if [ $failmode = "hard" ] ; then exit 1 ; fi
else
    if [ $verbose = 1 ] ; then echo "Copying $1 to $dest" ; fi
    # We don't delete the file in the event that we are copying the
    # file over itself (we SHOULD check for that separately, by checking 
    # that directories are distinct)
    #if [ -f $dest ] ; then $Show rm -f $dest ; fi
    $Show $CP $1 $dest
    rc=$?
    if [ $rc != 0 ] ; then
	echo "**Error copying file $1 to $dest **"
        errs=`expr $errs + 1`
        if [ $failmode = "hard" ] ; then exit $rc ; fi
    else
	echo "rm -f $dest" >> $UNINSTALLFILE
    fi
    $Show chmod $mode $dest
    rc=$?
    if [ $rc != 0 ] ; then
	echo "**Error setting mode on file $dest**"
        errs=`expr $errs + 1`
        if [ $chmodefailmode = "hard" ] ; then exit $rc ; fi
    fi
fi
}

#
# A version of copy file that preserves file dates
CopyFileP() {
    CP="cp -p"
    CopyFile $1 $2 $3
    CP=cp
}

#
for arg in "$@" ; do
    case "$arg" in 
        -prefix=*)
            PREFIX=`echo $arg | sed -e 's/-prefix=//'`
	    prefix=$PREFIX
            ;;
# LAM Added -uninstall option
        -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

# Uninstall filename
if [ -z "$UNINSTALLFILE" ] ; then 
    UNINSTALLFILE="$sbindir/romiouninstall"
    MkDir `dirname $UNINSTALLFILE`
fi

# LAM top-level switch to install or uninstall
if test "$WANT_INSTALL" = "1"; then

# Directories

# Files
    MkDir $prefix

    echo "copying directory $ROMIO_HOME/doc to $docdir"
    MkDir $docdir
    for file in COPYRIGHT README users-guide.ps.gz ; do
	CopyFile $ROMIO_HOME/$file $docdir
    done

    echo "copying directory include to $includedir"
    MkDir $includedir
    for file in include/*.h ; do
        CopyFile $file $includedir
    done

    MkDir $libdir
    echo "copying directory $TOP_BUILD_DIR/lib to $libdir"
    #cp -r $ROMIO_HOME/lib $PREFIX
    #chmod 755 $PREFIX/lib
    for file in $TOP_BUILD_DIR/lib/* ; do
	if [ -f $file ] ; then 
            CopyFileP $file $libdir 
        fi
    done
    # Romio also copies directories in the lib directory.  

    if [ -z "$mandir" ] ; then 
	mandir=$PREFIX/man
    fi
    echo "copying directory $ROMIO_HOME/man to $mandir"
    MkDir $mandir
    MkDir $mandir/man3
    for file in $ROMIO_HOME/man/man3/* ; do
        CopyFile $file $mandir/man3
    done

    if [ -z "$exampledir" ] ; then exampledir=$PREFIX/examples ; fi
    echo "copying directory $ROMIO_HOME/test to $exampledir"
    MkDir $exampledir
    MkDir $exampledir/std
    for file in $ROMIO_HOME/test/std/* ; do 
	CopyFile $file $exampledir/std
    done
    CopyFile test/Makefile $exampledir
    CopyFile test/README $exampledir
    for file in test/*.c test/*.f test/runtests ; do
	CopyFile $file $exampledir
    done
    # Ranlib should not be necessary if cp -p is used
    #$RANLIB $PREFIX/lib/$ARCH/libmpio.a
    # change the include and lib paths in Makefile in the test directory

    echo "updating paths in $exampledir/Makefile"
    TMPNEWINC=-I$includedir
    TMPNEWLIB=$libdir/$ARCH/libmpio.a
    NEWINC=`echo $TMPNEWINC | sed 's/\//\\\\\//g'`
    NEWLIB=`echo $TMPNEWLIB | sed 's/\//\\\\\//g'`
    sed -e 5s/INCLUDE_DIR[\ ]*=\ [/a-z0-9.A-Z_-]*/INCLUDE_DIR\ =\ $NEWINC/ \
        -e 6s/LIBS[\ ]*=\ [/a-z0-9.A-Z_-]*/LIBS\ =\ $NEWLIB/ $exampledir/Makefile\
           > $exampledir/.romiotmp 
    mv $exampledir/.romiotmp $exampledir/Makefile
    
    #
    MkDir $sbindir
    echo "rm -f $UNINSTALLFILE" >> $UNINSTALLFILE
    # Add the directory removes to the UNINSTALL file
    for dir in $dirlist ; do
        echo "if [ -d $dir ] ; then rmdir $dir ; fi" >> $UNINSTALLFILE
    done
else

# LAM do all the things for uninstall -- do the opposite of above.
# This whole section is new.
    $sbindir/romiouninstall
    echo "ROMIO uninstalled"
fi
