#! /bin/sh
# @(#) $RCSfile: INST.cpp,v $: $Revision: #64 $, $Date: 2008/10/16 $
# @(#) Copyright (c), 1987, 2008 Tibco Software Inc.  All rights reserved.
# This program installs Spotfire S+, 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 Spotfire S+ 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 Spotfire S+ from your UNIX shell by typing:
    ./INSTALL
once you correct the problems encountered above.
EOF
    exit 1
fi
# Set Traps
trap "echo; echo Re-start $0 to continue installing Spotfire S+ ; exit 1" 1 2 3
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 Spotfire S+ 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
#
# Set world write and execute permissions on the html help directories.
#
# find . -name __Hhelp -exec chmod -R a+rwx {} \;
cd "$SHOME"/
echo; echo "===== Attempting to Install Splus script to $SPLUS_DEST"
# Create the Splus file from the Splus.pre file stored in $INSTALL_DIR
if [ ! -f "$SHOME"/cmd/Splus.orig ]; then
    cp "$SHOME"/cmd/Splus "$SHOME"/cmd/Splus.orig
fi
SED_FILE "$INSTALL_DIR"/Splus.pre "$SHOME"/cmd/Splus || exit 1
chmod +x "$SHOME"/cmd/Splus
#
# Check for the unlikely event they chose $SHOME/cmd as the destination
# for Splus to prevent cp from failing.
#
if [ "$SPLUS_DEST" = "$SHOME"/cmd -a "$SPLUS_SCRIPT" = Splus ]
then
    SUCCESS=T
else
    if [ -f "$SPLUS_DEST/$SPLUS_SCRIPT" -a "$SCRIPT_OVERWRITE" = "n" ]
    then
         echo ""
         echo "*************** WARNING ***************"
         echo "A script named $SPLUS_SCRIPT already exists in $SPLUS_DEST,"
         echo "installing $SPLUS_SCRIPT in /tmp"
         echo "*************** WARNING ***************"
         echo ""
         SPLUS_DEST="/tmp"
    fi
    if cp "$SHOME"/cmd/Splus "$SPLUS_DEST/$SPLUS_SCRIPT"
    then
         SUCCESS=T
    fi
fi
if [ "$SUCCESS" = "T" ]
then
    cat << EOF
You have successfully installed Spotfire S+.
To run Spotfire S+, log out then log back in to a user account and type:
 
EOF
   echo "	$SPLUS_SCRIPT"
   echo ""
   echo "(This assumes that $SPLUS_DEST is in your path)"
else
    cat << EOF
There were problems installing Spotfire S+.  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 Spotfire S+ from your UNIX shell by typing:
    ./INSTALL
once you correct the problems encountered above.  The "Installing
TIBCO Spotfire S+ for UNIX/Linux" chapter of the "Spotfire S+
Installation and Administrative Guide" (SHOME/doc/admin.pdf)
contains some helpful information.
EOF
    exit 1
fi
trap "" 0
echo ""
echo "===== Installation complete."
echo ""
exit 0
