#! /bin/sh
# @(#)INSTALL version 3.33 created 6/13/96
# @(#)Copyright (c), 1987, 1996 StatSci, Inc.  All rights reserved.
# This program installs S-Plus, less and their associated UNIX man
# pages, X11 files as well as optional Suntools files in the chosen
# directories, assuming CONFIGURE has been run to set those options.

# Make sure that we are using this program from the right directory.
if [ ! -d ./splus/lib/install ]
then
    echo "ERROR: You must use $0 from the top S-PLUS directory"
    exit 1
fi 

# Make sure that the CONFIGURE program has been run first
if [ ! -r ".configured" ]
then
    ./CONFIGURE -b
fi 

# Source the environment variables used by the program
. ./splus/lib/install/DEFAULTS.sh
. $SHOME/splus/lib/install/USER.env

if [ ! -d $SPLUS_DEST ]
then
    cat << EOF
You have no $SPLUS_DEST directory. Please create the $SPLUS_DEST directory
and check to see that the $SPLUS_DEST directory permissions are writeable 
by you or check with your systems administrator. You may need root 
privileges to install the Splus file.

You can try again to install S-PLUS from your UNIX shell by typing:

    ./INSTALL

once you correct the problems encountered above.  Please see "Troubleshooting
Installation and Licensing Problems" in the S-PLUS Installation Manual.

EOF
    exit 1
fi

# Set Traps
trap "echo; echo Re-start $0 to continue installing S-PLUS; exit 1" 1 2 3

# Make them input a serial number if there isn't already one:
if [ ! -r $SERIAL_FILE ]
then
    echo
    echo-n "Please input the serial number shown on your tape: "
    read SERIAL_NUM

    # Make sure that it is non-zero length
    if [ "x$SERIAL_NUM" = "x" ]
    then
        echo "Error"
        echo "You must enter the serial number shown on your tape."
        exit 1
    else
        echo "Saving your serial number"
        echo $SERIAL_NUM > $SERIAL_FILE
    fi
fi

# If device drivers are compressed, uncompress them
compressed_flag=splus/x/.compressed
if [ -r $compressed_flag ]
then
        echo ===== Uncompressing device drivers.
        for f in splus/x/*.Z
        do uncompress -v $f
        done
        rm $compressed_flag
fi

# Go through the license installation procedure if an S-PLUS license is not
# installed. NOTE: "00.lic" is the S-PLUS license "feature name".
echo; echo "===== Checking license installation."

# Make sure adm/lic/logs directory exists and has full write permission
[ ! -d $SHOME/adm/lic/logs ] && mkdir $SHOME/adm/lic/logs
chmod 777 $SHOME/adm/lic/logs 2> /dev/null  || cat <<EOCAT

Unable to set permissions on $SHOME/adm/lic/logs directory.  

All S-PLUS users should have read-write-execute access to this directory. 
If this is not corrected, users may experience problems starting S-PLUS.

Continuing license installation...
EOCAT

test_feature=$SHOME/adm/lic/keys/00.lic
if [ ! -r $test_feature ]
then
    $SHOME/HOSTINFO
    cat <<EOCAT

If you have not gotten a license key yet, just interrupt out (generally by
pressing CNTL-C) of the LICENSE install procedure that follows. Note that the
license key installation will only work on the system that was designated as
the license server system when the license key was generated.

EOCAT
    $SHOME/cmd/S LICENSE install
fi
if [ -r $test_feature ]
then
    if $SHOME/cmd/S LICENSE Slmhelper okserver $test_feature 2>/dev/null
    then
        $SHOME/cmd/S LICENSE Slmhelper upserver $test_feature 2>/dev/null ||
        (echo "Running 'LICENSE server start'";
         $SHOME/cmd/S LICENSE server start)
    fi
fi

echo; echo "===== Beginning optional installations."

if [ "$SPLUS_MAN_DIR" ]
then
    if [ ! -d "$SPLUS_MAN_DIR" -a -w "$SPLUS_MAN_DIR" ]
    then
        echo "Cannot install man pages, directory $SPLUS_MAN_DIR does not exist"
        echo ""
        echo "Suggest you pick the item again and enter a directory that does"
        echo "exist, such as /usr/man/man1 or /usr/man/manl"
    else
        echo "Installing the S-PLUS manual pages ..."

        # Give them write permission
        chmod u+w $SPLUS_MAN_FILES
        for f in $SPLUS_MAN_FILES
        do
            echo Copying $f to $SPLUS_MAN_DIR
            cp $f $SPLUS_MAN_DIR || exit 1
        done
    fi
fi

if [ "$LESS_DIR" ]
then
    if [ ! -d "$LESS_DIR" ]
    then
        echo "Cannot install less pager, directory $LESS_DIR does not exist"
        echo ""
        echo "Suggest you pick the item again and enter a directory that does"
        echo "exist, such as /usr/man/man1 or /usr/man/manl"
    else
        echo "Installing the less pager ..."
        for f in $LESS_FILES
        do
            echo Copying $f to $LESS_DIR
            cp $f $LESS_DIR || exit 1
        done
    fi
fi
    
if [ "$LESS_MAN_DIR" ]
then
    if [ ! -d "$LESS_MAN_DIR" ]
    then
        echo "Cannot install man pages, directory $LESS_MAN_DIR does not exist"
    else
        echo "Installing the less manual pages ..."
    
        # Give them write permission
        chmod u+w $LESS_MAN_FILES
        for f in $LESS_MAN_FILES
        do
            echo Copying $f to $LESS_MAN_DIR
            cp $f $LESS_MAN_DIR || exit 1
        done
    fi
fi

if [ "$LIC_MAN1_DIR" ]
then
    if [ ! -d "$LIC_MAN1_DIR" ]
    then
        echo "Cannot install man pages, directory $LIC_MAN1_DIR does not exist"
    else
        echo "Installing the Elan License Manager command manual pages ..."
        for f in $LIC_MAN1_FILES
        do
            echo Copying $f to $LIC_MAN1_DIR
            cp $f $LIC_MAN1_DIR || exit 1
        done
    fi
fi

if [ "$LIC_MAN5_DIR" ]
then
    if [ ! -d "$LIC_MAN5_DIR" ]
    then
        echo "Cannot install man pages, directory $LIC_MAN5_DIR does not exist"
    else
        echo "Installing the Elan License Manager file format manual pages ..."
        for f in $LIC_MAN5_FILES
        do
            echo Copying $f to $LIC_MAN5_DIR
            cp $f $LIC_MAN5_DIR || exit 1
        done
    fi
fi

if [ "$SUN_TOOLS_DIR" ]
then
    if [ ! -d "$SUN_TOOLS_DIR" ]
    then
        echo "Cannot install SunTools defaults, directory $SUN_TOOLS_DIR"
        echo "does not exist"
    else
        echo "Installing the SunTools default files..."
        (cd $SUN_SOURCE;
         for f in $SUN_FILES
         do
             echo Copying $SUN_FILES to $SUN_DEST
             cp $SUN_FILES $SUN_DEST
         done
        )
    fi
fi

echo; echo "===== Installing Splus script to $SPLUS_DEST"
if [ "`/bin/pwd`" = "`cd $SPLUS_DEST; /bin/pwd`" ] || cp Splus $SPLUS_DEST
then
    cat << EOF

You have successfully installed S-PLUS.
To run S-PLUS, log out then log back in to a user account and type:

    Splus

EOF
else
    cat << EOF

There were problems installing S-PLUS.  Please check to see that the
$SPLUS_DEST directory permissions are writeable by you or check with
your systems administrator.  You may need root privileges to install
the Splus file.  

You can try again to install S-PLUS from your UNIX shell by typing:

    ./INSTALL

once you correct the problems encountered above.  Please see "Troubleshooting
Installation and Licensing Problems" in the S-PLUS Installation Manual.
EOF
    exit 1
fi

trap "" 0
echo ""
echo "===== Installation complete."
echo ""
exit 0
