#! /bin/sh
#
# Usage: Splus MIGRATE old_SHOME_directory
#
# This script updates a newly installed S-PLUS directory tree with
# localizations from an older S-PLUS directory tree.  This can involve, for
# example, copying add-on modules from the old tree to the new tree.
#
logfile=$SHOME/.migrate.log

PATH=$SHOME/splus/lib/install:$PATH export PATH

if [ $# -ne 1 ]; then
    cat <<\! >&2
Usage: Splus MIGRATE old_Splus_directory

For example,

    Splus MIGRATE `Splus3.2 SHOME`

!
    exit 1
fi

# Random variable settings
arcinfo_feature=50

# NOTE: SHOME got set by Splus wrapper script.
newSHOME=$SHOME export newSHOME
oldSHOME=$1 export oldSHOME
if [ `cd $newSHOME; /bin/pwd` = `cd $oldSHOME; /bin/pwd` ]; then
    overlay=yes
else
    overlay=no
fi

keydir=`LICENSE keydir` export keydir
newmodules=''

# Log some information
echo STARTED `date` >> $logfile
echo "oldSHOME=$oldSHOME" >> $logfile
echo "newSHOME=$newSHOME" >> $logfile
trap '(echo; echo FINISHED[SIGNAL] `date`; echo ==============================) >> $logfile; exit 1' 2 3 15

if [ "X$overlay" = Xyes ]; then
    # Doing inplace overlay of old SHOME with new SHOME.
    echo "Checking new S-PLUS tree overlay of old S-PLUS tree." |
        tee -a $logfile

else
    # Copying files from old SHOME to new SHOME.

    echo "Checking old S-PLUS tree for file to copy to new S-PLUS tree." |
        tee -a $logfile

    # Check for S+Interface files.
    if [ -r $oldSHOME/cmd/CASIM-filter ]; then
        msg="Copy S+Interface files from old to new S-PLUS tree? [y] "
        echo-n "$msg"; echo-n "$msg" >> $logfile
        read answer
        echo "$answer" >>$logfile
        {
            case "X$answer" in
                X|Xy|XY|Xyes|XYes|XYES)
                    for dir in library/interface
                    do
                        [ -d $oldSHOME/$dir ] &&
                            (cd $oldSHOME; find $dir -depth -print)
                    done

                    # OLD-style S+Interface stuff
                    for dir in library/CASIM library/MUI uid examples \
                               include/casim include/mui
                    do
                        [ -d $oldSHOME/$dir ] &&
                            (cd $oldSHOME; find $dir -depth -print)
                    done

                    # Get the stray (not in library/interface) files.
                    stray_interface_files="Version.muis cmd/CASIM-filter
                        cmd/CMAKE cmd/DISTSPI cmd/MC cmd/MCOMPILE cmd/MMAKE
                        cmd/MRUN cmd/MRUN.sav cmd/SGUI cmd/Splus_client
                        cmd/cmakehead cmd/dirname cmd/m4.muis cmd/uil
                        include/MUIS_IAS include/MUIS_header
                        newfun/lib/libCASIM.a newfun/lib/libMUI.a
                    "

                    for f in $stray_interface_files
                    do [ -r $oldSHOME/$f ] && echo $f
                    done
                ;;
            esac | (cd $oldSHOME; cpio -dumvp $newSHOME)
        } 2>&1 | tee -a $logfile
    fi

    # Check for S+ARCINFO files.
    if [ -d $oldSHOME/library/arcinfo ]; then
        msg="Copy S+GISLINK for ARC/INFO files from old to new S-PLUS tree? [y] "
        echo-n "$msg"; echo-n "$msg" >> $logfile
        read answer
        echo "$answer" >>$logfile
        case "X$answer" in
            X|Xy|XY|Xyes|XYes|XYES)
                (cd $oldSHOME/library;
                 find arcinfo -depth -print |
                 cpio -dumvp $newSHOME/library) 2>&1 |
                tee -a $logfile
            ;;
        esac
    fi

    # Take care of add-on modules
    if [ -d $oldSHOME/module ]; then
        modules=`cd $oldSHOME/module; echo *`
        modnokey=''
        for module in $modules
        do  export module
            if [ -d $oldSHOME/module/$module ]; then
                if [ -d $newSHOME/module/$module ]; then
                    msg="WARNING: $module directory already in $newSHOME/module"
                    echo "$msg" >&2; echo "$msg" >> $logfile
                fi
                msg="Copy $module from old to new S-PLUS tree? [y] "
                echo-n "$msg"; echo-n "$msg" >> $logfile
                read answer
                echo "$answer" >>$logfile
                case "X$answer" in
                    X|Xy|XY|Xyes|XYes|XYES)
                        newmodules="${newmodules} ${module}"
                        (cd $oldSHOME/module;
                         [ -d $newSHOME/module ] || mkdir $newSHOME/module
                         find $module -depth -print |
                         cpio -dumvp $newSHOME/module) 2>&1 |
                        tee -a $logfile
                    ;;
                esac
            fi
        done
    fi
fi

# Wrapup
modnokey=''
for module in $newSHOME/module/*
do  module=`basename $module` export module
    [ "$module" = "*" ] && break
    feature=`LICENSE Slmhelper modfeature $module 2>/dev/null`
    if [ X$feature = X ] || [ X$feature = X-1 ]; then
        echo "WARNING: Could not locate licensing information for module $module."
        modnokey="$modnokey $module"
    else
        [ -r $keydir/$feature.lic ] || modnokey="$modnokey $module"
    fi 2>&1 | tee -a $logfile
done
if [ "X$modnokey" != X ]; then
    cat <<! | tee -a $logfile

WARNING:
  New license keys will be needed for these modules (the MODINSTALL command
  can be used to install the keys):

    $modnokey

!
fi
if [ "X$newmodules" != X ]; then
cat <<! | tee -a $logfile
WARNING:
  The following command(s) need to be run to update configuration information
  for newly copied modules:

!
    for module in $newmodules
    do echo "    Splus MODINSTALL $module"
    done | tee -a $logfile
    echo | tee -a $logfile
fi

if [ -r $newSHOME/library/arcinfo -a ! -r $keydir/$arcinfo_feature.lic ]; then
    cat <<! >&2
WARNING: Could not locate license key for S+GISLINK for ARC/INFO.

WARNING:
  A new license key will be needed for S+GISLINK for ARC/INFO, then the
  following commands will need to be run to complete their installation:

    cd $newSHOME/library/arcinfo
    ./INSTALL.NEWKEY

!
fi | tee -a $logfile

# Create a file in newSHOME to indicate that we've been here.
echo FINISHED `date` >> $logfile
echo ============================== >> $logfile
