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

MAKE="make --no-print-directory"
PKGS=" trace_sample trace_rlog"

# Source locations
top_srcdir=/proj/tpfimos/larour/Libraries/linux/mpich2-1.0.2p1/src/mpe2/src/slog2sdk

# Build locations
libbuild_dir=/proj/tpfimos/larour/Libraries/linux/mpich2-1.0.2p1/src/mpe2/src/slog2sdk/lib
binbuild_dir=/proj/tpfimos/larour/Libraries/linux/mpich2-1.0.2p1/src/mpe2/src/slog2sdk/bin
docbuild_dir=/proj/tpfimos/larour/Libraries/linux/mpich2-1.0.2p1/src/mpe2/src/slog2sdk/doc
logfilesbuild_dir=/proj/tpfimos/larour/Libraries/linux/mpich2-1.0.2p1/src/mpe2/src/slog2sdk/logfiles
etcbuild_dir=/proj/tpfimos/larour/Libraries/linux/mpich2-1.0.2p1/src/mpe2/src/slog2sdk/etc
sharebuild_dir=/proj/tpfimos/larour/Libraries/linux/mpich2-1.0.2p1/src/mpe2/src/slog2sdk/share
sbinbuild_dir=/proj/tpfimos/larour/Libraries/linux/mpich2-1.0.2p1/src/mpe2/src/slog2sdk/sbin

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

# 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="SLOG-2/Jumpshot-4"
fi

# File access mode
MODE=0644
XMODE=0755
replace=1
# set failmode to soft to let failures accumulate
failmode="hard"
# set copymode to "all" (out of all, core, optional) to install everything
copymode="all"
# 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=""
# 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
# For now, Unix machine which has JAVA should be new enough to have basename ?
# Uninstall filename
if [ -z "$UNINSTALLFILE" ] ; then
    INSTALL_base=`basename $0`
    UNINSTALL_base=`echo $INSTALL_base | sed -e 's/install/uninstall/'`
    UNINSTALLFILE=$DESTDIR${sbindir}/${UNINSTALL_base}
fi

# Update the $prefix if necessary
for arg in "$@" ; do
    case "$arg" in
        -prefix=*)
            prefix=`echo $arg | sed -e 's/-prefix=//'`
            prefix_override=1
            ;;
        -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'`
            ;;
        -copymode=*)
            copymode=`echo $arg | sed -e 's/-copymode=//g'`
            ;;
        -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       - permission mode for regular files.  Default is $MODE .
-xmode=nnnn      - permission mode for execuables and directories.
                   Default is $XMODE .
-copymode=xxx    - copy mode for files and directories.  Possible values are
                   {all, core, optional}.
-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
            ;;
        *)
            echo "install-viewer: Unrecognized argument $arg ."
            exit 1
            ;;
    esac
done



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


##############################################################################
#
# Start of Routines
#
##############################################################################
RM="rm -f"

# 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 $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 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
elif [ "$1" = "$dest" ] ; then
    echo "**File $1 is being copied to itself!"
    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
        echo "Replacing existing file $dest..."
        $Show $RM $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
        # store the destination filename copied
        filelist="$filelist $destfile"
    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
CPRP=cp
CopyFileP() {
    CP="$CPRP -p"
    CopyFile $1 $2 $3
    CP=cp
}



#
# A version of recursive directory copy that preserves file dates
# but does NOT copy CVS, old, tex directory. 
# $1 is the src directory, $2 is dest directory, $3 is $MODE or $XMODE
CopyDirRecurP() {
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
                CopyFileP $file $2/$file $3
            fi
        done
        for dir in * ; do
            if [ -d $dir \
                 -a "$dir" != "CVS" -a "$dir" != "old" \
                 -a "$dir" != "tex" \
               ] ; then
                CopyDirRecurP $dir $2/$dir $3
            fi
        done
        # cd back where it comes from
        # counter the effect of "cd $1" above,
        # so "for" loop stiil 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
            $Show chmod $XMODE $path_to_date
            rc=$?
            if [ $rc != 0 ] ; then
                echo "**Error setting mode to $XMODE on directory $path_to_date**"
                errs=`expr $errs + 1`
                if [ $chmodfailmode = "hard" ] ; then exit $rc ; fi
            fi
        fi
    done
fi
}


#
# This is a version of FixupFile that handles @...@ instead of #...#.
# This is a little trickier, since this file itself is processed by
# with @...@ replacements.  We take advantage of the fact that [char] 
# matches the same things as char.
# This has a few more fields than FixupFile
#
FixupFileForInstall() {
    # Change the FIRST libpath to the new form.  This allows 
    # the LIB_PATH to contain multiple names, as long as the MPICH libpath
    # is first
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 copied"
else
    if [ -f $dest ] ; then $Show $RM $dest ; fi
    if [ $just_testing = 0 ] ; then
        sed \
        -e "s%^GUI_LIBDIR=.*%GUI_LIBDIR=$libdir%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
            # store the destination filename copied
            filelist="$filelist $destfile"
        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 $dest
rc=$?
if [ $rc != 0 ] ; then
    echo "**Error setting mode on file $dest**"
    errs=`expr $errs + 1`
    if [ $failmode = "hard" ] ; then exit $rc ; fi
fi
}

##############################################################################
#
# End of Routines
#
##############################################################################


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

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

if [ "$prefix_override" = 1 ] ; then
    libdir=$prefix/lib
    bindir=$prefix/bin
    sbindir=$prefix/sbin
    docdir=$prefix/doc
    etcdir=$prefix/etc
    sharedir=$prefix/share
    logfilesdir=$prefix/logfiles
    if [ $appendUninstall = 0 ] ; then
        UNINSTALLFILE=$DESTDIR${sbindir}/${UNINSTALL_base}
    fi
fi

if [ $appendUninstall = 0 ] ; then
    if [ "$copymode" = "core" -o "$copymode" = "optional" ] ; then
        UNINSTALLFILE="/dev/null"
    fi
fi

# echo "UNINSTALLFILE = $UNINSTALLFILE"

# check if $UNINSTALLFILE lives under $sbindir
uninstall_dirname=`dirname $UNINSTALLFILE`
if [ "$uninstall_dirname" = "$DESTDIR$sbindir" ] ; then
    if [ ! -d $DESTDIR$sbindir ] ; then
        echo "Creating ${pkgname}'s sbin"
        MkDir $sbindir
    fi
fi

if [ "$copymode" = "all" -o "$copymode" = "core" ] ; then
    for type in lib doc logfiles etc ; do
        fromdir_name=${type}build_dir
        todir_name=${type}dir
        eval fromdir_value=\$"$fromdir_name"
        eval todir_value=\$"$todir_name"
        if [ -n "$fromdir_value" ] ; then
            if [ -d $fromdir_value ] ; then
                echo "Copying ${pkgname}'s $type"
                if [ $verbose = 1 ] ; then
                    echo "$fromdir_value -> $DESTDIR$todir_value"
                fi
                CopyDirRecurP $fromdir_value $todir_value $MODE
            fi
        fi 
    done
fi

if [ "$copymode" = "all" ] ; then
    if [ -n "$PKGS" ] ; then
        if [ ! -d $DESTDIR$sharedir ] ; then
            echo "Creating ${pkgname}'s share"
            MkDir $sharedir
        fi
    fi

    for type in share ; do
        fromdir_name=${type}build_dir
        todir_name=${type}dir
        eval fromdir_value=\$"$fromdir_name"
        eval todir_value=\$"$todir_name"
        if [ -n "$fromdir_value" ] ; then
            if [ -d $fromdir_value ] ; then
                echo "Copying ${pkgname}'s $type"
                if [ $verbose = 1 ] ; then
                    echo "$fromdir_value -> $DESTDIR$todir_value"
                fi
                CopyDirRecurP $fromdir_value $todir_value $MODE
            fi
        fi
    done
fi

if [ "$copymode" = "all" -o "$copymode" = "core" ] ; then
    EXEFILES="clogprint clogTOslog2 clog2print clog2TOslog2 slog2navigator slog2print slog2filter slog2updater logconvertor jumpshot "
    if [ -n "$binbuild_dir" ] ; then
        if [ -d $binbuild_dir ] ; then
            echo "Creating ${pkgname}'s bin"
            MkDir $bindir
            for exefile in $EXEFILES ; do
                if [ -x $binbuild_dir/$exefile ] ; then
                    FixupFileForInstall $binbuild_dir/$exefile \
                                        $bindir/$exefile $XMODE
                fi
            done
        fi
    fi
fi
    
if [ "$copymode" = "all" -o "$copymode" = "optional" ] ; then
    CopyFileP $top_srcdir/COPYRIGHT           $prefix
    CopyFileP $top_srcdir/CHANGES             $prefix
    CopyFileP $top_srcdir/FAQ                 $prefix
    CopyFileP $top_srcdir/README.sdk          $prefix
    CopyFileP $top_srcdir/README.rte          $prefix
fi

# Remove old uninstall file & Prepare the UNINSTALLFILE for appending
if [ $appendUninstall = 0 -a -f $UNINSTALLFILE ] ; then
    $RM $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

if [ "$UNINSTALLFILE" != "/dev/null" ] ; then
    chmod u+x $UNINSTALLFILE
fi

# Install subpackages as specified in $PKGS
if [ "$copymode" = "core" ] ; then
    for pkg in $PKGS ; do
        if [ -d $binbuild_dir/../$pkg ] ; then
            if [ "X$pkg" = "Xtrace_rlog" ] ; then
                ( cd $binbuild_dir/../$pkg && \
                  if [ -x ./sbin/install-package ] ; then \
                      ./sbin/install-package -prefix=$prefix \
                                             -uninstall=$UNINSTALLFILE \
                                             -mode=$MODE -xmode=$XMODE ; \
                  fi )
            fi
        fi
    done
fi

if [ "$copymode" = "all" ] ; then
    for pkg in $PKGS ; do
        if [ -d $binbuild_dir/../$pkg ] ; then
            if [ "X$pkg" = "Xtrace_rlog" ] ; then
                ( cd $binbuild_dir/../$pkg && \
                  if [ -x ./sbin/install-package ] ; then \
                      ./sbin/install-package -prefix=$prefix \
                                             -uninstall=$UNINSTALLFILE \
                                             -mode=$MODE -xmode=$XMODE ; \
                  fi )
            else
                ( cd $binbuild_dir/../$pkg && \
                  if [    -x ./sbin/install-package \
                       -a -d $DESTDIR$sharedir ] ; then \
                      ./sbin/install-package -prefix=$sharedir/$pkg \
                                             -uninstall=$UNINSTALLFILE \
                                             -mode=$MODE -xmode=$XMODE ; \
                  fi )
            fi
        fi
    done
fi

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

# Add the directories to be removed to the UNINSTALLFILE
for dir in $dirlist ; do
    echo "if [ -d $dir ] ; then rmdir $dir ; fi" >> $UNINSTALLFILE
done

#
echo "Installed ${pkgname} in $DESTDIR$prefix"
echo "$UNINSTALLFILE may be used to remove the installation"
#
if [ $errs -gt 0 ] ; then
    rc=1
else
    rc=0
fi
exit $rc
