#! /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 install  ( if you used -prefix at configure time )
#
#    or,
#
#    make install 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
#

# Location of sources
# top_srcdir=@top_srcdir@
top_srcdir=@top_srcdir_abs@       # in absolute path

# Build locations
includebuild_dir=@includebuild_dir@
libbuild_dir=@libbuild_dir@
binbuild_dir=@binbuild_dir@
sbinbuild_dir=@sbinbuild_dir@
etcbuild_dir=@etcbuild_dir@

# Installation directories: Default paths (set at configure time)
prefix=@prefix@
exec_prefix=@exec_prefix@
includedir=@includedir@
libdir=@libdir@
bindir=@bindir@
sbindir=@sbindir@
datadir=@datadir@
sysconfdir=@sysconfdir@
mandir=@mandir@
htmldir=@htmldir@
docdir=@docdir@

# Assume PROF_LIBNAME=MPE_LIBNAME.
PROF_LIBNAME=@MPE_LIBNAME@
MPI_IMPL=@MPI_IMPL@
MPE_BUILD_FORTRAN2C=@MPE_BUILD_FORTRAN2C@

# Determine this package's name
tmpname="`echo $top_srcdir | sed -e 's%\(.*\)/\([^/]*\)%\2%'`"
pkgname="`echo $tmpname | sed -e 'y%abcdefghijklmnopqrstuvwxyz/%ABCDEFGHIJKLMNOPQRSTUVWXYZ_%'`"
if [ "x$pkgname" = "x" ] ; then
    pkgname="MPE2"
fi

MAKE="@MAKE@"
INSTALL="@INSTALL@"
RM="@RM@"
LN_S="@LN_S@"

# File access mode
MODE=0644
XMODE=0755
DMODE=0755
replace=1
# set failmode to soft to let failures accumulate
failmode="hard"
# chmod can fail for various reasons.  Let it?
chmodfailmode="hard"
Show=eval
# set verbose to 0 to suppress success output
verbose=0
just_testing=0
prefix_override=0

# filelist records files that have been installed
filelist=""
# dirlist records directories created
dirlist=""
# cmdlist records commands to be carried out during uninstall
cmdlist=""
# initialize the error code return counter
errs=0

# The variable, appendUninstall, is to signal this script to allow uninstall
# information to append to an existing uninstall file.  Default is NO.
appendUninstall=0

# Set uninstall file
# Should replace basename which is NOT available on all machine ?
# Is ok for now. Popular Unix boxes, AIX, solaris, Linux, all have basename
if [ -z "$UNINSTALLFILE" ] ; then
    INSTALL_base=`basename $0`
    UNINSTALL_base=`echo $INSTALL_base | sed -e 's/install/uninstall/'`
    UNINSTALLFILE=$DESTDIR${sbindir}/${UNINSTALL_base}
fi

for arg in "$@" ; do
    case "$arg" in
        -prefix=*)
            prefix=`echo $arg | sed -e 's/-prefix=//g'` 
            if [ "$prefix" != "@prefix@" ] ; then
                prefix_override=1
            fi
            ;;
        -uninstall=*)
            UNINSTALLFILE=`echo A$arg | sed -e 's/A-uninstall=//g'` 
            appendUninstall=1
            ;;
        -mode=*)
            MODE=`echo $arg | sed -e 's/-mode=//g'`
            ;;
        -xmode=*) 
            XMODE=`echo $arg | sed -e 's/-xmode=//g'` 
            ;;
        -dmode=*) 
            DMODE=`echo $arg | sed -e 's/-dmode=//g'` 
            ;;
        -t)
            Show=echo ; just_testing=1 ; failmode="soft"
            ;;
        -noman)                   noman=1 ;;
        -echo)                    set -x ;;
        -noreplace|-no_replace)   replace=0 ;;
        -replace)                 replace=1 ;;
        -no_verbose|-noverbose)   verbose=0 ;;
        -verbose)                 verbose=1 ;;
        -soft)                    failmode="soft" ; chmodfailmode="soft" ;;
        -hard)                    failmode="hard" ; chmodfailmode="hard" ;;
        -softchmod)               chmodfailmode="soft" ;;
        -help|-u|-usage|-h)
cat <<EOF
Install $pkgname into $prefix.
-prefix=path     - Destination directory.
-uninstall=file  - File to append uninstall information to
-mode=nnnn       - mode for regular files.  Default is $MODE .
-xmode=nnnn      - mode for execuables.  Default is $XMODE .
-dmode=nnnn      - mode for directories.  Default is $DMODE .
-t               - Try only; do no installation.
-manpath=path    - Set an alternate path for the man pages
-noman           - Do not install the man pages.
-noreplace       - Do not replace files found in the installation directory
-soft            - Do not abort on failure
-softchmod       - Do not abort on failure of chmod
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 "mpeinstall: Unrecognized argument $arg ."
                exit 1
            fi
            ;;
    esac
done



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

##############################################################################
#
# Start of Routines
#
##############################################################################

INSTALLER="$INSTALL"
#
InstallFile() {
if [ -z "$3" ] ; then
    mode=$MODE
else
    mode=$3
fi
if [ -d $DESTDIR$2 ] ; then
    dest=$DESTDIR$2/`basename $1`
    destfile=$2/`basename $1`
else
    dest=$DESTDIR$2
    destfile=$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 installed <<<"
    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
elif [ "$1" = "$dest" ] ; then
    echo "*** File $1 is being installed to itself! ***"
    errs=`expr $errs + 1`
    if [ $failmode = "hard" ] ; then exit 1 ; fi
else
    if [ $verbose = 1 ] ; then echo "Installing $1 to $dest" ; fi
    if [ -f $dest ] ; then
        echo "Replacing existing file $dest..."
        $Show $RM -f $dest
    fi
    if [ "X$mode" != "X" ] ; then
        $Show $INSTALLER -m $mode $1 $dest
    else
        $Show $INSTALLER $1 $dest
    fi
    rc=$?
    if [ $rc != 0 ] ; then
        echo "*** Error installing file $1 to $dest with mode $mode. ***"
        errs=`expr $errs + 1`
        if [ $failmode = "hard" ] ; then exit $rc ; fi
    else
        # store the destination installed filename
        filelist="$filelist $destfile"
    fi
fi
}



#
# Recursive directory install that installs or replaces files
# (file replacement of replace=1) but excluding directories,
# CVS/, old/ and tex/.
# $1 is the src directory, $2 is dest directory, $3 is $MODE or $XMODE
InstallDirRecur() {
if [ -n "$1" -a -n "$2" ] ; then
    if [ -d $1 ] ; then
        MkDir $2
        # use $4 as a local variable
        # & set it as the original PWD before "cd $1"
        set $1 $2 ${3:-"$MODE"} `pwd`
        cd $1
        for file in * ; do
            if [ -f $file ] ; then
                InstallFile $file $2/$file $3
            fi
        done
        for dir in * ; do
            if [ -d $dir \
                 -a "$dir" != "CVS" -a "$dir" != "old" \
                 -a "$dir" != "tex" \
               ] ; then
                InstallDirRecur $dir $2/$dir $3
            fi
        done
        # cd back where it comes from
        # counter the effect of "cd $1" above,
        # so "for" loop still works
        cd $4
    fi
fi
}



#
# Make the given directory.  This handles building intermediate directories
# as required, and maintains a list of created directories in dirlist.
MkDir() {
dest=$DESTDIR$1
if [ ! -d $dest ] ; then
    dir=`echo $dest | sed 's%/% /%g'`
    path_to_date=''
    for path in $dir ; do
        path_to_date="$path_to_date$path"
        if [ ! -d $path_to_date ] ; then
            if [ $verbose = 1 ] ; then echo "Creating directory $dest" ; fi
            $Show "mkdir $path_to_date"
            rc=$?
            if [ $rc != 0 ] ; then
                echo "*** Error making directory $dest. ***"
                errs=`expr $errs + 1`
                if [ $failmode = "hard" ] ; then exit $rc ; fi
                echo "Failed to create directory $path_to_date"
                exit 1
            else
                # Note that we store in inverse order
                dirlist="$1 $dirlist"
            fi
            if [ "X$DMODE" != "X" ] ; then
                $Show chmod $DMODE $path_to_date
                rc=$?
                if [ $rc != 0 ] ; then
                    echo "*** Error setting mode to $DMODE on directory $path_to_date. ***"
                    errs=`expr $errs + 1`
                    if [ $chmodfailmode = "hard" ] ; then exit $rc ; fi
                fi
            fi
        fi
    done
fi
}



#
# Fixup files with various xxxxbuild_dir to xxxxdir, install directory.
#
FixInstallFile() {
if [ -d $DESTDIR$2 ] ; then
    dest=$DESTDIR$2/`basename $1`
    destfile=$2/`basename $1`
else
    dest=$DESTDIR$2
    destfile=$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 fixed"
else
    if [ -f $dest ] ; then $Show $RM -f $dest ; fi
    if [ $just_testing = 0 ] ; then \
        if [ "$MPI_IMPL" = "MPICH" -o "$MPI_IMPL" = "MPICH2" ] ; then
            sed \
            -e "s%^MPI_CC\([ ]*\)=\([ ]*\).*\$%MPI_CC\1=\2$bindir/mpicc%g" \
            -e "s%^MPI_F77\([ ]*\)=\([ ]*\).*\$%MPI_F77\1=\2$bindir/mpif77%g" \
            -e "s%^MPE_INCDIR\([ ]*\)=\([ ]*\).*\$%MPE_INCDIR\1=\2$includedir%g" \
            -e "s%^MPE_LIBDIR\([ ]*\)=\([ ]*\).*\$%MPE_LIBDIR\1=\2$libdir%g" \
            -e "s%^MPE_BINDIR\([ ]*\)=\([ ]*\).*\$%MPE_BINDIR\1=\2$bindir%g" \
            -e "s%^MPE_ETCDIR\([ ]*\)=\([ ]*\).*\$%MPE_ETCDIR\1=\2$sysconfdir%g" \
            -e "s%^MPE_SBINDIR\([ ]*\)=\([ ]*\).*\$%MPE_SBINDIR\1=\2$sbindir%g" \
            $1 > $dest
        else
            sed \
            -e "s%^MPE_INCDIR\([ ]*\)=\([ ]*\).*\$%MPE_INCDIR\1=\2$includedir%g" \
            -e "s%^MPE_LIBDIR\([ ]*\)=\([ ]*\).*\$%MPE_LIBDIR\1=\2$libdir%g" \
            -e "s%^MPE_BINDIR\([ ]*\)=\([ ]*\).*\$%MPE_BINDIR\1=\2$bindir%g" \
            -e "s%^MPE_ETCDIR\([ ]*\)=\([ ]*\).*\$%MPE_ETCDIR\1=\2$sysconfdir%g" \
            -e "s%^MPE_SBINDIR\([ ]*\)=\([ ]*\).*\$%MPE_SBINDIR\1=\2$sbindir%g" \
            $1 > $dest
        fi
        rc=$?
        if [ $rc != 0 ] ; then
            echo "*** Error fixing up file $dest. ***"
            errs=`expr $errs + 1`
            if [ $failmode = "hard" ] ; then exit $rc ; fi
        else
            # store the destination fixed filename
            filelist="$filelist $destfile"
        fi
    else
        if [ $verbose = 1 ] ; then echo "Fixup $1 and install to $dest" ; fi
    fi
fi
if [ -z "$3" ] ; then
    mode=$MODE
else
    mode=$3
fi
if [ "X$mode" != "X" ] ; then
    $Show chmod $mode $dest
    rc=$?
    if [ $rc != 0 ] ; then
        echo "*** Error setting mode $mode on file $dest. ***"
        errs=`expr $errs + 1`
        if [ $failmode = "hard" ] ; then exit $rc ; fi
    fi
fi
}


##############################################################################
#
# End of routines
#
##############################################################################
if [ ! -s $libbuild_dir/lib${PROF_LIBNAME}.a ] ; then 
    echo "You must make $pkgname before installing it!"
    echo "Could not find $libbuild_dir/lib${PROF_LIBNAME}.a !"
    exit 1
fi

if [ ! -n "$prefix" ] ; then 
    echo "Set an installation location with -prefix=<location> ."
    exit 1
fi
if [ $verbose = 1 ] ; then
    echo "Install into $DESTDIR$prefix"
fi

if [ -d $DESTDIR$prefix ] ; then
    if [ $verbose = 1 ] ; then
        echo "using existing directory $DESTDIR$prefix"
    fi
else
    MkDir $prefix
fi

# echo "prefix = $prefix"
# echo "exec_prefix = $exec_prefix"
# echo "includedir = $includedir"
# echo "libdir = $libdir"
# echo "bindir = $bindir"
# echo "sbindir = $sbindir"
# echo "datadir = $datadir"
# echo "sysconfdir = $sysconfdir"
# echo "mandir = $mandir"
# echo "htmldir = $htmldir"
# echo "docdir = $docdir"

# 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
    sysconfdir=$prefix/etc
    mandir=$prefix/man
    htmldir=$prefix/html
    docdir=$prefix/doc
    if [ $appendUninstall = 0 ] ; then
        UNINSTALLFILE=$DESTDIR${sbindir}/${UNINSTALL_base}
    fi
fi

# Install end-user include Files, i.e. mpe*h
if [ -n "$includebuild_dir" ] ; then
    if [ -d $includebuild_dir ] ; then
        echo "Installing $pkgname include files to $DESTDIR$includedir"
        if [ ! -d $DESTDIR$includedir ] ; then
            MkDir $includedir
        fi
        cd $includebuild_dir
        for file in clog*.h mpe*.h ; do
            if [ -f $includebuild_dir/$file ] ; then
                InstallFile $includebuild_dir/$file $includedir/$file $MODE
            fi
        done
    fi
fi

# Install libraries
if [ -n "$libbuild_dir" ] ; then
    if [ -d $libbuild_dir ] ; then
        echo "Installing $pkgname libraries to $DESTDIR$libdir"
        InstallDirRecur $libbuild_dir $libdir $MODE
    fi
fi

# Install utility programs
if [ -n "$binbuild_dir" ] ; then
    if [ -d $binbuild_dir ] ; then
         echo "Installing $pkgname utility programs to $DESTDIR$bindir"
         InstallDirRecur $binbuild_dir $bindir $XMODE
         if [ -s $binbuild_dir/mpecc -a -x $binbuild_dir/mpecc ] ; then
             FixInstallFile $binbuild_dir/mpecc $bindir/mpecc $XMODE
         fi
         if [ -s $binbuild_dir/mpefc -a -x $binbuild_dir/mpefc ] ; then
             FixInstallFile $binbuild_dir/mpefc $bindir/mpefc $XMODE
         fi
    fi
fi

# Install compiler configuration files
if [ -n "$etcbuild_dir" ] ; then
    if [ -d $etcbuild_dir ] ; then
         echo "Installing $pkgname configuration files to $DESTDIR$sysconfdir"
         InstallDirRecur $etcbuild_dir $sysconfdir $MODE
         cd $etcbuild_dir && \
         for file in *.conf ; do \
             FixInstallFile $file $sysconfdir/$file ; \
         done
    fi
fi

# Create $sbindir outside of nested IF blocks because $sbindir is needed
# even if $sbinbuild_dir/* do not exist.
MkDir $sbindir

# Install system-admin related utilies
if [ -n "$sbinbuild_dir" ] ; then
    if [ -d $sbinbuild_dir ] ; then
         echo "Installing $pkgname system utility programs to $DESTDIR$sbindir"
         InstallFile    $sbinbuild_dir/mpecheckinstall  $sbindir $XMODE
         FixInstallFile $sbinbuild_dir/mpetestlink      $sbindir $XMODE
         FixInstallFile $sbinbuild_dir/mpetestexec      $sbindir $XMODE
         FixInstallFile $sbinbuild_dir/mpetestexeclog   $sbindir $XMODE
    fi
fi

MkDir $datadir

# Set up Examples directory for the MPE Logging programs
logging_dirname="src/wrappers/test"
graphics_dirname="src/graphics/contrib/test"
collchk_dirname="src/collchk/test"
for pkg in logging graphics collchk ; do
    dirname=${pkg}_dirname
    eval dirvalue=\$"$dirname"
    if [ -f $libbuild_dir/../$dirvalue/Makefile ] ; then
        examplesdir=$datadir/examples_${pkg}
        MkDir $examplesdir
#       Install example C and Fortran programs
        InstallDirRecur $top_srcdir/$dirvalue $examplesdir

#       Create examples Makefile
        cd $DESTDIR$examplesdir && \
        FixInstallFile $libbuild_dir/../$dirvalue/Makefile \
                       $examplesdir/Makefile

#        MPICH2 1.0.2 mpd does not sypport symbolic link to mpiexec
#        if [ -x $bindir/mpiexec -o -x $DESTDIR$bindir/mpiexec ] ; then
#            $Show "( cd $DESTDIR$examplesdir && \
#                     $RM -f mpiexec && \
#                     $LN_S $bindir/mpiexec mpiexec )"
#            filelist="$filelist $examplesdir/mpiexec" 
#        elif [ -x $bindir/mpirun -o -x $DESTDIR$bindir/mpirun ] ; then
#            $Show "( cd $DESTDIR$examplesdir && \
#                     $RM -f mpirun && \
#                     $LN_S $bindir/mpirun mpirun )"
#            filelist="$filelist $examplesdir/mpirun" 
#        fi
    fi
done


# Install documentation related files if they exist,
# ie INSTALL $top_srcdir/doc  $docdir
#    INSTALL $top_srcdir/man  $mandir
#    INSTALL $top_srcdir/html $htmldir
for type in doc man html ; do
    typename=${type}dir
    eval typevalue=\$"$typename"
    if [ -d $top_srcdir/$type ] ; then
        if [ ! -d $DESTDIR$typevalue ] ; then
            MkDir $typevalue
        fi
        echo "Installing $pkgname $type to $DESTDIR$typevalue"
        InstallDirRecur $top_srcdir/$type $typevalue $MODE
    fi
done

# Remove old uninstall file
if [ "$appendUninstall" = 0 -a -f $UNINSTALLFILE ] ; then
    $RM -f $UNINSTALLFILE
fi
#
# Open an uninstall file and force strong echo if one does NOT exist
if [ ! -f $UNINSTALLFILE ] ; then
    cat > $UNINSTALLFILE <<EOF
#! /bin/sh
set -x
EOF
fi

chmod u+x $UNINSTALLFILE

# Install the SLOG-2 tools if they exist
# Don't set the prefix if the default is chosen.
if [ -x $libbuild_dir/../src/slog2sdk/sbin/install-package ] ; then
    install_opts="-mode=$MODE -xmode=$XMODE -dmode=$DMODE -copymode=core"
    install_opts="-uninstall=$UNINSTALLFILE $install_opts"
    if [ "$prefix_override" = 1 ] ; then
        install_opts="-prefix=$prefix $install_opts"
    fi
    ( cd $libbuild_dir/../src/slog2sdk && ./sbin/install-package $install_opts )
fi

#  Clean up the example directories
for pkg in logging graphics ; do
    dirname=${pkg}_dirname
    examplesdir=$datadir/examples_${pkg}
    if [ -s $DESTDIR$examplesdir/Makefile ] ; then
       echo "( cd $examplesdir && $MAKE clean )" >> $UNINSTALLFILE
    fi
done

# Add the files to be removed to the UNINSTALLFILE
for file in $filelist ; do
    echo "$RM -f $file" >> $UNINSTALLFILE
done

if [ "$appendUninstall" = 0 ] ; then
    echo "$RM -f $sbindir/mpeuninstall" >> $UNINSTALLFILE
    echo "$RM -f $UNINSTALLFILE" >> $UNINSTALLFILE
fi

# Add the directory removes to the UNINSTALL file
for dir in $dirlist ; do
    echo "if [ -d $dir ] ; then rmdir $dir ; fi" >> $UNINSTALLFILE
done

if [ "$appendUninstall" = 0 ] ; then
    cp $UNINSTALLFILE $sbinbuild_dir/mpeuninstall
#
    echo "Installed $pkgname in $DESTDIR$prefix"
    echo "$UNINSTALLFILE may be used to remove the installation"
fi

if [ $errs -gt 0 ] ; then
    rc=1
else
    rc=0
fi
exit $rc
