#! /bin/sh
#
# This is a script to install mpi.  It can be called from the top-level 
# Makefile or directly.  Note that this script needs to be built by configure.
#
# As the install takes place, it keeps a list of files that were installed
# in a form that allows their easy removal
#
# Bug: If there is an error before the install is finished, the mpiuninstall
# file will not contain the directories to remove.
# Possible fix: add a routine that appends these to the uninstall file
# that can be called before any exit
#
# *CHANGE*
# In previous versions, files were not overwritten.  This caused mysterious
# problems for users that did a reinstall over a damaged or incorrect 
# installation (for example, installing a correction produced by a patch).
# The previous behavior is available with the -noreplace option
# *CHANGE*
# We now build the libraries in lib and the binaries in bin.
# Installation into architecture-specific directories is done by this script,
# not by the original build.
# *CHANGE IN PROGRESS*
# Changing the various "FixupFile" proceedures to work with the configured
# versions, looking for specific directory assignment statements such as
# prefix=... and includedir=... (config.status does something similar).
# This requires changing the affected scripts to follow a standard format; 
# see mpif90.in for an example
CPP_DIR=@CPP_DIR@
buildcpp=@buildcpp@
CPP_COMPILER="@CPP_COMPILER@"
ARCH=@ARCH@
COMM=@COMM@
PREFIX=@PREFIX@
prefix=@prefix@
DEVICE=@DEVICE@
CPRP="@CPRP@"
MAKE="@MAKE@"
RSHCOMMAND="@RSHCOMMAND@"
NOMPE=@NOMPE@
SHAREDLIB_LOCALDIR=@SHAREDLIB_LOCALDIR@
MPIR_HOME=@MPIR_HOME@
#
# Default paths (set at configure time)
    exec_prefix=@exec_prefix@
    bindir=@bindir@
    sbindir=@sbindir@
    includedir=@includedir@
    sysconfdir=@sysconfdir@
    libdir=@libdir@
    sharedlib_dir=@sharedlib_dir@
    messagecat_dir=@messagecat_dir@
    mandir=@mandir@
    htmldir=@htmldir@
    datadir=@datadir@
    docdir=@docdir@
# Location of sources
top_srcdir=@top_srcdir@
#
includebuild_dir=@includebuild_dir@
binbuild_dir=@binbuild_dir@
libbuild_dir=@libbuild_dir@
# File access mode
MODE=0664
XMODE=0775
replace=1
# Propagate install arguments to mpe install
mpeargs=""
# If we're doing -libonly, we may want a different file....
#
# dirlist is used to uninstall empty directories at the end
dirlist=""
#
inlib=0
#
errs=0
#
# 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
for arg in "$@" ; do
    case "$arg" in 
	-echo) set -x ;;
	-mode=*) MODE=`echo $arg | sed -e 's/-mode=//g'` ;;
	-xmode=*)XMODE=`echo $arg | sed -e 's/-xmode=//g'` ;;
	-prefix=*)
		  # make install passes the prefix.  If the old and
		  # new prefix are the same, don't set the override
	          # switch incase mandir etc. were set on the configure
		  # line (it uses the PREFIX, not prefix, value)
		  prefix=`echo $arg | sed -e 's/-prefix=//g'` 
		  if [ $prefix != $PREFIX ] ; then
  	              prefix_override=1 
		  fi
		;;
	-aprefix=*)APREFIX=`echo $arg | sed -e 's/-aprefix=//g'` 
		   arg="" ;;   # aprefix not valid for mpeargs
	-shliblocal=*)
	    SHAREDLIB_LOCALDIR=`echo A$arg | sed -e 's/A-shliblocal=//g'` 
		   arg="" ;;   # shliblocal not valid for mpeargs
	-noreplace|-no_replace) replace=0 ;;
	-replace) replace=1 ;;
	-noman) noman=1 ;;
	-device=*) DEVICE=`echo A$arg | sed -e 's/A-device=//g'` 
	           COMM=$DEVICE 
		   arg="" ;;   # device not valid for mpeargs
	-manpath=*)mandir=`echo $arg | sed -e 's/-manpath=//g'` 
		   mandir_override=1
		   arg="" ;;   # manpath not valid for mpeargs
	-libonly) libonly=1 
		   arg="" ;;   # libonly not valid for mpeargs
	# -inlib is a private option
	-inlib) inlib=1 
		   arg="" ;;   # inlib not valid for mpeargs
	#-arch=*)  ARCH=`echo A$arg | sed -e 's/A-arch=//g'` ;;
        -no_verbose|-noverbose)  verbose=0 ;;
	-verbose)                verbose=1 ;;
	-soft) failmode="soft" ; chmodfailmode="soft" ;;
	-hard) failmode="hard" ; chmodfailmode="hard" ;;
	-softchmod) chmodfailmode="soft" ;;
	-t) Show=echo
	    just_testing=1
	    failmode="soft"
	    ;;
	-help|-u|-usage|-h)
cat <<EOF
Install MPICH into $prefix.
-mode=nnnn   - mode for regular files.  Default is $MODE .
-xmode=nnnn  - mode for execuables and directories.  Default is $XMODE .
-prefix=path - Destination directory.
-aprefix=path- Path to use in sample files.  Default is same
               as prefix.
-t           - Try only; do no installation.
-libonly     - Install only the libraries; this is 
               appropriate for installing a second device.
-manpath=path- Set an alternate path for the man pages
-noman       - Do not install the man pages.
-device=name - Select a different device type to install
-noreplace   - Do not replace files that are found in the
               installation directory
-soft        - Do not abort on failure
-softchmod   - Do not abort on failure of chmod
EOF
	exit 1
	;;
	*)
	echo "Unrecognized argument $arg ."
	exit 1
	;;
    esac
    # If arg isn't appropriate for mpe, set it to null
    if [ -n "$arg" ] ; then 
        mpeargs="$mpeargs $arg"
    fi
done

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

# Installation directories
# If the prefix was set, we want to override these choices
if [ "$prefix_override" = 1 ] ; then
    bindir=$prefix/bin
    sbindir=$prefix/sbin
    includedir=$prefix/include
    sysconfdir=$prefix/etc
    libdir=$prefix/lib
    sharedlib_dir=$libdir/shared
    messagecat_dir=$libdir
    if [ "$mandir_override" != 1 ] ; then
        mandir=$prefix/man
    fi
    datadir=$prefix/share
    docdir=$prefix/doc
fi

# Uninstall filename
if [ -z "$UNINSTALLFILE" ] ; then 
    UNINSTALLFILE="$sbindir/mpiuninstall"
fi

if [ -z "$APREFIX" ] ; then
    APREFIX=$prefix
fi
#
# Check final directories
if [ -z "$bindir" ] ; then echo "No bindir" ; exit 1 ; fi
if [ -z "$libdir" ] ; then echo "No libdir" ; exit 1 ; fi
if [ -z "$includedir" ] ; then echo "No includedir" ; exit 1 ; fi
if [ -z "$datadir" ] ; then echo "No datadir" ; exit 1 ; fi
#
#
# If libonly, we need to get the correct default machine etc.  We could 
# get this from the config.status for the device, but the problem is that
# there are MANY items to restore.  A better approach is to use an install
# script from the library directory itself.
if [ -n "$libonly" ] ; then
    if [ $inlib = 0 -a -x $binbuild_dir/mpiinstall ] ; then
        $binbuild_dir/mpiinstall -inlib "$@"
        exit $?
    fi
fi
#
# 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
#
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 /bin/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 "/bin/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="$CPRP -p"
    CopyFile $1 $2 $3
    CP=cp
}

#
# Make the given directory.  This handles building intermediate directories
# as required, and maintains a list of created directories in dirlist.
MkDir() {
if [ ! -d $1 ] ; then
    dir_to_make=`echo $1 | sed 's%/% /%g'`
    path_to_date=''
    for path in $dir_to_make ; do
	path_to_date="$path_to_date$path"
	if [ ! -d $path_to_date ] ; then 
            if [ $verbose = 1 ] ; then echo "Creating directory $1" ; fi
 	    $Show "mkdir $path_to_date"
	    rc=$?
	    if [ $rc != 0 ] ; then 
	        echo "**Error making directory $1**"
                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
	    $Show chmod $XMODE $path_to_date
	    rc=$?
	    if [ $rc != 0 ] ; then
		echo "**Error setting mode on directory $path_to_date**"
		errs=`expr $errs + 1`
		if [ $chmodfailmode = "hard" ] ; then exit $rc ; fi
	    fi
	fi
    done
fi
}
#
# Fixup files that need #...# replaced
# Only mpireconfig and ch4p_servs.  chp4_servs should be replaced with a
# perl program.  mpireconfig can use FixupFile2 .
#
FixupFile() {
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"
else
    if [ -f $dest ] ; then $Show /bin/rm -f $dest ; fi
    if [ $just_testing = 0 ] ; then
        sed \
 	-e "s%\#datadir\#%$datadir%g" \
	-e "s%\#DEFAULT_ARCH\#%$ARCH%g" \
        -e "s%\#MPIR_HOME\#%$prefix%g" \
	-e "s%\#PREFIX\#%$prefix%g" \
	-e "s%\#RSHCOMMAND\#%$RSHCOMMAND%g" \
	-e "s%\#top_srcdir\#%@top_srcdir@%g" \
	-e "s%\#bindir\#%@bindir@%g" \
	-e "s%\#binbuild_dir\#%@bindir@%g" \
	$1 > $dest
        rc=$?
        if [ $rc != 0 ] ; then
	    echo "**Error fixing up file $dest**"
            errs=`expr $errs + 1`
            if [ $failmode = "hard" ] ; then exit $rc ; fi
        else
	    echo "/bin/rm -f $dest" >> $UNINSTALLFILE
        fi
    else
	if [ $verbose = 1 ] ; then echo "Fixup $1.in and copy to $dest" ; fi
    fi
fi
if [ -z "$3" ] ; then
    mode=$MODE
else
    mode=$3
fi
$Show chmod $mode $2
rc=$?
if [ $rc != 0 ] ; then
    echo "**Error setting mode on file $2**"
    errs=`expr $errs + 1`
    if [ $failmode = "hard" ] ; then exit $rc ; fi
fi
}

#
# This version merely replaces builddir versions with the master versions
# 
FixupFile2() {
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"
else
    if [ -f $dest ] ; then $Show /bin/rm -f $dest ; fi
    if [ $just_testing = 0 ] ; then
        # The $ forms are for PERL programs
        sed -e "s%^prefix=.*%prefix=$prefix%g" \
            -e "s%^includedir=.*%includedir=$includedir%g" \
	    -e "s%^mandir=.*%mandir=$mandir%g" \
	    -e "s%\$bindir=.*;%\$bindir=\"$bindir\";%"g \
	    -e "s%^bindir=.*%bindir=$bindir%"g \
	    -e "s%\$datadir=.*;%\$datadir=\"$datadir\";%"g \
	    -e "s%^datadir=.*%datadir=$datadir%"g \
            -e "s%^libdir=.*%libdir=$libdir%g" \
	    -e "s%^sharedlibdir=.*%sharedlibdir=$sharedlib_dir%g" \
		$1 > $dest
        rc=$?
        if [ $rc != 0 ] ; then
	    echo "**Error fixing up file $dest**"
            errs=`expr $errs + 1`
            if [ $failmode = "hard" ] ; then exit $rc ; fi
        else
	    echo "/bin/rm -f $dest" >> $UNINSTALLFILE
        fi
    else
	if [ $verbose = 1 ] ; then echo "Fixup $1 and copy to $dest" ; fi
    fi
fi
if [ -z "$3" ] ; then
    mode=$MODE
else
    mode=$3
fi
$Show chmod $mode $2
rc=$?
if [ $rc != 0 ] ; then
    echo "**Error setting mode on file $2**"
    errs=`expr $errs + 1`
    if [ $failmode = "hard" ] ; then exit $rc ; fi
fi
}
##############################################################################
#
# End of routines
#
##############################################################################
if [ ! -s $libbuild_dir/lib@MPILIBNAME@.a ] ; then 
    echo "You must make MPI before installing it!"
    echo "Could not find $libbuild_dir/lib@MPILIBNAME@.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 $prefix on the $ARCH with $DEVICE"
fi

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

# 
# These build the default paths to libdir/bindir/includedir.  
# We need to replace this with code that picks apart the directories.
#
MkDir $libdir
MkDir $sharedlib_dir
if [ -n "$SHAREDLIB_LOCALDIR" ] ; then
    MkDir $SHAREDLIB_LOCALDIR
fi
MkDir $bindir
MkDir $sbindir
MkDir $includedir
MkDir $datadir
MkDir $docdir
MkDir $sysconfdir

MkDir $prefix/examples
if [ -n "$CPP_DIR" -a "$buildcpp" = 1 ] ; then 
    MkDir $prefix/examples/$CPP_DIR
    MkDir $includedir/mpi2c++
fi

#
# Remove old uninstall file.  We can't do this until we've created the
# sbindir directory.
if [ -x $UNINSTALLFILE ] ; then
    if [ -x ${UNINSTALLFILE}.old ] ; then
        rm -f ${UNINSTALLFILE}.old
    fi
    mv $UNINSTALLFILE ${UNINSTALLFILE}.old
fi
/bin/rm -f $UNINSTALLFILE
cat > $UNINSTALLFILE <<EOF
#! /bin/sh
set -x
EOF
chmod +x $UNINSTALLFILE

if [ -z "$libonly" ] ; then
    CopyFile ${top_srcdir}/bin/tarch $bindir $XMODE
    CopyFile ${top_srcdir}/bin/tdevice $bindir $XMODE
    CopyFile util/mpichlib.conf ${sysconfdir} 
    #
    # mpiman
    $Show /bin/rm -f $bindir/mpiman
    FixupFile2 bin/mpiman $bindir/mpiman $XMODE
    #
    # mpirun et al.  This would be a good place to identify 
    # "partial" mpirun installations
    # mpirun.in requires:
    #     mpirun.args.in
    #     mpirun.<device>.in (some variations are mapped to different
    #                         devices)
    # ch_p4, execer also use
    #     mpirun.pg.in
    # This needs to be modified to simply move the needed files, not 
    # all of them.
    # Install should not execute a make.
    # See the install_$DEVICE script.
    # tstmachines is handled as part of the ch_p4 device code (and placed in
    # sbin)
    # This step relies on a clean bin build directory
    # Only mpirun really needs FixupFile2; all others just need copyfile
    if [ -x bin/mpirun ] ; then
        for file in bin/mpirun* ; do
	    bfile=`basename $file`
            FixupFile2 $file ${bindir}/$bfile $XMODE
        done
    fi

    #echo "/bin/rm -f $bindir/mpirun" >> $UNINSTALLFILE
    if [ ! -x $bindir/tarch ] ; then
        CopyFile $top_srcdir/bin/tarch $bindir $XMODE
    fi
    #
    $Show /bin/rm -f $bindir/mpireconfig
    if [ $verbose = 1 ] ; then 
        echo "Creating mpireconfig from util/mpireconfig.in"
    fi
    FixupFile $top_srcdir/util/mpireconfig.in $bindir/mpireconfig $XMODE
    #
    # Updating the config.status file is trickier, since we need to edit
    # the directories that might have contained the original path.
    # It is important to update the CORRECT file, in the case that we are doing
    # a -libonly installation on a previously constructed version.
    #
    if [ $just_testing = 0 ] ; then
    # Be VERY careful to only change the assignments that begin a line,
    # since other asignments may be strings (arguments to configure)
    sed -e 's%$top_srcdir/$file%$file%g' \
	-e 's%$top_srcdir/${file}%${file}%g' \
	-e "s%^bindir=.*%bindir=$bindir%g" \
	-e "s%^includedir=.*%includedir=$includedir%g" \
	-e "s%^prefix=.*%prefix=$prefix%g" \
	-e "s%^datadir=.*%datadir=$datatid%g" \
	-e "s%^MPIR_HOME=.*\$%MPIR_HOME='$prefix'%g" \
        -e "s%^LIB_PATH=\([^/ ]*\)/[^ ']*%LIB_PATH=\1$libdir%g" \
	-e "s%^FINC=\([^/ ]*\)/[^ ']*%FINC=\1$includedir%g" \
        -e "s%^FLIB_PATH=\([^/ ]*\)/[^ ']*%FLIB_PATH=\1$libdir%g" \
	-e "s%^F90INC=\([^/ ]*\)/[^ ']*%F90INC=\1$includedir%g" \
        -e "s%^F90LIB_PATH=\([^/ ]*\)/[^ ']*%F90LIB_PATH=\1$libdir%g" \
	-e "s%^MPE_DIR=.*\$%MPE_DIR='$prefix/mpe'%g" \
        -e "s%^MPICC=\([^/ ]*\)/.*\$%MPICC=$bindir/mpicc%g" \
        -e "s%^MPIF77=\([^/ ]*\)/.*\$%MPIF77=$bindir/mpif77%g" \
        -e "s%^MPIF90=\([^/ ]*\)/.*\$%MPIF90=$bindir/mpif90%g" \
        -e "s%^MPICPLUSPLUS=\([^/ ]*\)/.*\$%MPICPLUSPLUS=$bindir/mpiCC%g" \
		config.status > ${bindir}/mpireconfig.dat
    chmod $XMODE ${bindir}/mpireconfig.dat
    echo "/bin/rm -f ${bindir}/mpireconfig.dat" >> $UNINSTALLFILE
    else
        echo "Construct new ${bindir}/mpireconfig.dat from config.status"
    fi

    $Show /bin/rm -f $bindir/execer
    CopyFile $top_srcdir/util/execer $bindir/execer $XMODE
    #
    # mpicc, mpif77, mpif90
    FixupFile2 bin/mpicc $bindir/mpicc $XMODE
    if [ -x src/fortran/src/mpif77 -a "@NOF77@" = "0" ] ; then
        CopyFile src/fortran/src/mpif77 $bindir/mpif77 $XMODE
        CopyFile src/fortran/src/mpif77.conf $sysconfdir
        CopyFile $includebuild_dir/mpif.h $includedir
    fi
    if [ -x src/fortran/src/mpif90 -a "@NO_F90@" = "0" ] ; then
        CopyFile src/fortran/src/mpif90 $bindir/mpif90 $XMODE
	CopyFile src/fortran/src/mpif90.conf $sysconfdir
    fi
    #
    # mpiCC, if C++ selected
    if [ -n "$CPP_COMPILER" ] ; then
	FixupFile2 bin/mpiCC $bindir/mpiCC $XMODE
    fi
    # If there is a device install script, use that.  We
    # put this last in case it wants to update any of the scripts or
    # other files that are installed.  This is executed as an included
    # script so that it has access to the various variables (e.g., 
    # prefix and bindir).  If you need to run a program, run it from
    # within the script
    if [ -x mpid/$DEVICE/install_$DEVICE ] ; then
        . mpid/$DEVICE/install_$DEVICE
    elif [ -x $top_srcdir/mpid/$DEVICE/install_$DEVICE ] ; then
	. $top_srcdir/mpid/$DEVICE/install_$DEVICE
    fi
else
    # libonly
    #
    # mpirun et al.
    # Only mpirun really needs FixupFile2; all others just need copyfile
    if [ -x bin/mpirun ] ; then 
        for file in bin/mpirun* ; do
            bfile=`basename $file`
            FixupFile2 $file $bindir/$bfile $XMODE
        done
    fi
    if [ ! -x $bindir/tarch ] ; then
	CopyFile $binbuild_dir/tarch $bindir/tarch $XMODE
    fi
    FixupFile2 bin/mpicc $bindir/mpicc $XMODE
    if [ -n "$CPP_COMPILER" ] ; then
        FixupFile2 bin/mpiCC $bindir/mpiCC $XMODE
    fi
    CopyFile util/mpichlib.conf ${sysconfdir}
    FixupFile $top_srcdir/util/mpireconfig.in $bindir/mpireconfig $XMODE
    # No user include uses mpichconf.h
    CopyFile $includebuild_dir/mpichconf.h $sysconfdir/mpichconf.h.dat
    CopyFile $includebuild_dir/mpidefs.h $includedir 
    CopyFile $includebuild_dir/mpi.h $includedir
    if [ -s $includebuild_dir/mpif.h -a "@NOF77@" = "0" ] ; then
        CopyFile $includebuild_dir/mpi_fortdefs.h $includedir
        CopyFile src/fortran/src/mpif77 $bindir/mpif77 $XMODE
	CopyFile src/fortran/src/mpif77.conf $sysconfdir
        CopyFile $includebuild_dir/mpif.h $includedir
    fi
    if [ -x src/fortran/src/mpif90 -a "@NO_F90@" = "0" ] ; then
        CopyFile src/fortran/src/mpif90 $bindir/mpif90 $XMODE
	CopyFile src/fortran/src/mpif90.conf $sysconfdif
    fi
    if [ -s romio/include/mpio.h ] ; then
        CopyFile romio/include/mpio.h $includedir
        CopyFile romio/include/mpiof.h $includedir
    fi
    # Fortran 90 modules
    if [ -d $includebuild_dir/f90base -a "@NO_F90@" = "0" ] ; then
        MkDir $includedir/f90base
        for file in $includebuild_dir/f90base/* ; do
	    CopyFile $file $includedir/f90base
	done
    fi
    if [ -d $includebuild_dir/f90choice -a "@NO_F90@" = "0" ] ; then
        MkDir $includedir/f90choice
        for file in $includebuild_dir/f90choice/* ; do
	    CopyFile $file $includedir/f90choice
	done
    fi
fi
#
# P4
if [ "$DEVICE" = "ch_p4" ] ; then
    FixupFile2 util/tstmachines $sbindir/tstmachines $XMODE
    # Installs should not do builds
    if [ -x mpid/server/serv_p4 ] ; then 
	CopyFile mpid/server/serv_p4 $bindir $XMODE
	$Show /bin/rm -f util/chp4_servs
	FixupFile $top_srcdir/util/chp4_servs.in $sbindir/chp4_servs $XMODE
	FixupFile2 util/chkserv $sbindir/chkserv $XMODE
    else
        echo "Server not available"
    fi
fi
#
if [ -z "$libonly" ] ; then
    #
    # Machines database
    #	if [ ! -d $prefix/util/machines.anl ] ; then \
    #	    mkdir $prefix/util/machines.anl ; fi
    #	$CPRP -r util/machines $prefix/util/machines.anl
    for file in util/machines/machines* ; do
        if [ -s $file ] ; then 
            CopyFile $file $datadir
	fi
    done
    #
    # Include files
    CopyFile $includebuild_dir/mpi.h $includedir
    if [ -s $includebuild_dir/mpif.h -a "@NOF77@" = "0" ] ; then
        CopyFile $includebuild_dir/mpi_fortdefs.h $includedir
    fi
    CopyFile $includebuild_dir/mpi_errno.h $includedir
    #CopyFile include/protofix.h $includedir
    #CopyFile include/patchlevel.h $includedir
    # No user include uses mpichconf.h
    CopyFile $includebuild_dir/mpichconf.h $sysconfdir/mpichconf.h.dat
    CopyFile $includebuild_dir/mpidefs.h $includedir
    # Install ROMIO files
    if [ -s romio/include/mpio.h ] ; then
        CopyFile romio/include/mpio.h $includedir
        CopyFile romio/include/mpiof.h $includedir
    fi
    # Fortran 90 modules
    if [ -d $includebuild_dir/f90base -a "@NO_F90@" = "0" ] ; then
        MkDir $includedir/f90base
        for file in $includebuild_dir/f90base/* ; do
	    CopyFile $file $includedir/f90base
	done
    fi
    if [ -d $includebuild_dir/f90choice -a "@NO_F90@" = "0" ] ; then
        MkDir $includedir/f90choice
        for file in $includebuild_dir/f90choice/* ; do
	    CopyFile $file $includedir/f90choice
	done
    fi
fi
#
# Include files for C++
# (We should eventually use the install target in C++)
if [ -n "$CPP_DIR" -a "$buildcpp" = 1 ] ; then 
    if [ ! -d $includedir/mpi2c++ ] ; then
        MkDir $includedir/mpi2c++
    fi
    # MPI-2-C++/src has the regular files; MPI-2-C++ has the config-dependent
    # files
    for file in $top_srcdir/MPI-2-C++/src/mpi2c++/*.h \
	        MPI-2-C++/src/mpi2c++/mpi2c++_config.h ; do 
        if [ -s $file ] ; then
            CopyFile $file $includedir/mpi2c++
	fi
    done
fi
#
# Libaries (including shared libraries)
# Warning: the -h option for testing for a symbolic link 
# is not universally recognized. 
for file in $libbuild_dir/*.a ; do
    if [ -f $file ] ; then 
        # Be careful of libpmpich.a (may be a link to libmpich.a)
        if [ "@HAS_WEAK_SYMBOLS@" = 0 ] ; then
           CopyFileP $file $libdir
        elif [ ! -h $file ] ; then 
           CopyFileP $file $libdir
        # else we'll need to make a link... (see below)
	fi
    fi
done
# Copy the Message Queue dynamic link library if it exists
for file in $libbuild_dir/libtvmpich.so* ; do
    if [ -f $file ] ; then
	CopyFileP $file $libdir
    fi
done
# The Fortran wrapper libraries should have been created in libbuild_dir
#if [ "@NOF77@" = "0" ] ; then
#    for file in src/fortran/src/lib*.a ; do
#        if [ -f $file ] ; then
#            CopyFileP $file $libdir
#        fi
#    done
#fi
# Copy the shared library versions
for file in $libbuild_dir/shared/*.so* ; do
    # Also need to handle any links!
    if [ ! -h $file -a -f $file ] ; then 
        CopyFileP $file $sharedlib_dir
	if [ -n "$SHAREDLIB_LOCALDIR" -a \
	     "$SHAREDLIB_LOCALDIR" != $sharedlib_dir ] ; then
	    CopyFileP $file $SHAREDLIB_LOCALDIR
        fi
    fi
done
# Create links from foo.so.* to foo.so
for file in $sharedlib_dir/*.so* ; do
    filebase=`echo $file | sed -e 's%\.so\.[^/]*$%.so%g'`
    if [ ! -h $filebase -a -s $file ] ; then 
        $Show ln -s $file $filebase
	echo "/bin/rm -f $filebase" >>$UNINSTALLFILE
    fi
done
#

# Handle the case of a weak-symbol profiling library
if [ ! -s $libdir/libp@MPILIBNAME@.a ] ; then
    (cd $libdir ; ln -s lib@MPILIBNAME@.a libp@MPILIBNAME@.a )
    echo "/bin/rm -f $libdir/libp@MPILIBNAME@.a" >> $UNINSTALLFILE
fi
# Message queue routine for debuggers must be built with the proper prefix.
# This is a mess; it really should be configured and built like that from the
# start.
if [ -n "@SHAREDKIND_FOR_TV@" -a "@SHAREDKIND_FOR_TV@" != "ignore" -a \
    "$prefix" != "@PREFIX@" ] ; then
    echo "You must set the prefix when you configure to get the message queues"
    echo "Reconfigure and remake MPICH to get working message queue routines"
fi
#
# Special programs (helper executables)
# So far we have
#     spxcp - Executable copy program for IBM SP.
for file in spxcp ; do
    if [ -x bin/$file ] ; then 
        CopyFileP bin/$file $bindir
    fi
done
#
# Message files
if [ -z "$libonly" ] ; then 
    # Note that we need to get the mpich.*.m files for IRIX which has
    # a message file directory as well as the message file.
    for file in $libbuild_dir/mpich.* ; do
        if [ $file = "mpich.cat" ] ; then
	    # This is a link; it assumes that En_US is the default.
            if [ -f $messagecat_dir/mpich.En_US.cat ] ; then 
                $Show ln -s $messagecat_dir/mpich.En_US.cat \
	   		    $messagecat_dir/mpich.cat
		echo "/bin/rm -f $messagecat_dir/mpich.cat" >>$UNINSTALLFILE
            fi
        elif [ -s $file ] ; then 
	    # This will handle the case of no mpich.* matches
            CopyFile $file $messagecat_dir
        fi
    done
fi
    
# Other files in lib need to be updated...
CopyFile $includebuild_dir/mpidefs.h $includedir
#
# Documentation
if [ -z "$noman" -a -z "$libonly" ] ; then 
    if [ -z "$mandir" ] ; then
        mandir=$prefix/man
    fi
    MkDir $mandir
    CopyFile $top_srcdir/man/mandesc $mandir
    # No files in man5 (was ADI routines)
    for dir1 in man1 man3 man4 ; do
       MkDir $mandir/$dir1
        for file in $top_srcdir/man/$dir1/* ; do 
	    # man5 in particular might be empty; be careful
            if [ -f "$file" -a -s "$file" ] ; then 
  	        CopyFile $file $mandir/$dir1
	    fi
        done
    done
    # do the Romio man pages, only man3
    for file in $top_srcdir/romio/man/man3/* ; do
        if [ -f "$file" -a -s "$file" ] ; then 
  	    CopyFile $file $mandir/man3
	fi
    done
    # HTML pages
    if [ -z "$htmldir" ] ; then
        htmldir=$prefix/www
    fi
    MkDir $htmldir
    if [ -s $top_srcdir/www/index.html ] ; then
        # Some development versions don't include the www directories
        CopyFile $top_srcdir/www/index.html $htmldir
        # No files in www5 (was ADI routines)
        for dir1 in www1 www3 www4 ; do
           MkDir $htmldir/$dir1
            for file in $top_srcdir/www/$dir1/* ; do 
	        # www5 in particular might be empty; be careful
                if [ -f "$file" -a -s "$file" ] ; then 
  	            CopyFile $file $htmldir/$dir1
	        fi
            done
        done
    fi
fi
if [ -z "$libonly" ] ; then
    for file in functions.ps.gz guide.ps.gz install.ps.gz ; do
	if [ -f $top_srcdir/doc/$file ] ; then 
            CopyFile $top_srcdir/doc/$file $docdir
	fi
    done
    #$Show $CPRP -r doc $prefix
    #if [ -f ref/adiman.ps.gz ] ; then 
    #    CopyFile ref/adiman.ps.gz $prefix/doc
    #fi
    if [ -f $top_srcdir/ref/mpiman.ps.gz ] ; then 
        CopyFile $top_srcdir/ref/mpiman.ps.gz $docdir
    fi
fi
#
if [ "$NOMPE" = "0" ] ; then
    if [ ! -x mpe/sbin/mpeinstall ] ; then
        echo "** mpeinstall script is not available in mpe/sbin!"
        echo "** MPE will not be installed"
    else
        echo "Installing MPE"
	# mpeargs should be the same for the most part as mpi args.
	# see command line processing.
	mpeargs="$mpeargs -uninstall=$UNINSTALLFILE"
        ( cd mpe ; ./sbin/mpeinstall $mpeargs )
    fi
fi
#
# If we used SYSV semaphores or ipcs, we should copy cleanipcs into sbin.
CopyFile $top_srcdir/util/cleanipcs $sbindir $XMODE
#
# Example programs
CopyFile $top_srcdir/installtest/Makefile.in $prefix/examples
#
# This SHOULD use mpireconfig...
Savevar=$prefix
# Configure (version 1) has the *FEATURE* of replacing prefix=xxxx with
# prefix=@prefix@ when ever prefix= starts in the first column (!)
# It also does this with exec_prefix (!!)
     prefix=$APREFIX
(cd $prefix/examples ; $bindir/mpireconfig Makefile )
echo "(cd $prefix/examples ; $MAKE clean)" >> $UNINSTALLFILE
echo "/bin/rm -f $prefix/examples/Makefile" >> $UNINSTALLFILE
     prefix=$Savevar
CopyFile $prefix/examples/Makefile $datadir/Makefile.sample
CopyFile $top_srcdir/installtest/cpi.c $prefix/examples
CopyFile $top_srcdir/installtest/cpip.c $prefix/examples
CopyFile $top_srcdir/installtest/cpilog.c $prefix/examples
CopyFile $top_srcdir/installtest/hello++.cc $prefix/examples
if [ "@NOF77@" = "0" ] ; then
    CopyFile $top_srcdir/installtest/pi3.f $prefix/examples
    CopyFile $top_srcdir/installtest/pi3p.f $prefix/examples
    if [ "@NO_F90@" = "0" ] ; then
        CopyFile $top_srcdir/installtest/pi3f90.f90 $prefix/examples
    fi
fi
if [ -s $top_srcdir/romio/test/simple.c ] ; then 
    CopyFile $top_srcdir/romio/test/simple.c $prefix/examples/simpleio.c
fi
CopyFile $top_srcdir/installtest/README $prefix/examples
#
# Test build the examples
$Show "(cd $prefix/examples ; /bin/rm -f mpirun ; \
	  ln -s ../bin/mpirun mpirun )"
echo "/bin/rm -f $prefix/examples/mpirun" >> $UNINSTALLFILE
#
echo "About to run installation test..."
$Show "(cd $prefix/examples; $MAKE all ; $MAKE clean )"
if [ @SHAREDKIND@ != "ignore" ] ; then
    echo "About to run installation test for shared libraries ..."
    $Show "(cd $prefix/examples; MPICH_USE_SHLIB="yes"; export MPICH_USE_SHLIB; $MAKE all ; $MAKE clean )"
fi
#
# This SHOULD use mpireconfig...
Savevar=$prefix
if [ -n "$CPP_DIR" -a "$buildcpp" = 1 ] ; then 
    # C++ Example programs 
    CopyFile $top_srcdir/installtest/mpi-2-c++/Makefile.in $prefix/examples/$CPP_DIR
    #
    prefix=$APREFIX
    CPPEXDIR=$top_srcdir/$CPP_DIR/contrib/examples
    # This isn't correct because it doesn't handle all of the fields in
    # the C++ examples Makefile, particularly the TEMPLATE_REP field
    # The intent here is to update the directories; but we also need
    # to update a few additional fields
    (cd $prefix/examples/$CPP_DIR ; $bindir/mpireconfig Makefile )
    echo "(cd $prefix/examples/$CPP_DIR ; $MAKE clean)" >> $UNINSTALLFILE
    echo "/bin/rm -f $prefix/examples/$CPP_DIR/Makefile" >> $UNINSTALLFILE
    prefix=$Savevar
    CopyFile $CPPEXDIR/hello_world.cc $prefix/examples/$CPP_DIR
    CopyFile $CPPEXDIR/ring.cc $prefix/examples/$CPP_DIR
    CopyFile $CPPEXDIR/topology.cc $prefix/examples/$CPP_DIR
    CopyFile $CPPEXDIR/user_bcast.cc $prefix/examples/$CPP_DIR
    CopyFile $CPPEXDIR/chapter_10_mpi2.cc $prefix/examples/$CPP_DIR
    CopyFile $CPPEXDIR/pi.cc $prefix/examples/$CPP_DIR
    CopyFile $CPPEXDIR/README $prefix/examples/$CPP_DIR
    #
    # Test build the C++ examples
    $Show "(cd $prefix/examples/$CPP_DIR ; /bin/rm -f mpirun ; \
	      ln -s ../../bin/mpirun mpirun )"
    echo "/bin/rm -f $prefix/examples/$CPP_DIR/mpirun" >> $UNINSTALLFILE
    #
    echo "About to run C++ installation test..."
    $Show "(cd $prefix/examples/$CPP_DIR; $MAKE ; $MAKE clean)"
    #
fi
#
echo "/bin/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
#
echo installed MPICH in $prefix
echo $UNINSTALLFILE may be used to remove the installation.
#
if [ $errs -gt 0 ] ; then
    rc=1
else
    rc=0
fi
exit $rc
