#!/bin/sh
# @(#) $RCSfile: CONFIGURE,v $: $Revision: #46 $, $Date: 2008/10/23 $
# @(#) Copyright (c), 1987, 2006 Insightful Corp.  All rights reserved.
# This program automates much of the Spotfire S+ installation.  It calls a number
# of scripts and files in the $INSTALL_DIR directory.
# This script starts the menu that will help you to:
#    modify the Spotfire S+ defaults
#    pick which files to install

# 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

# Source the environment variables used by the program
. ./splus/lib/install/DEFAULTS.sh

# page the license agreement
less LICENSE.TXT

if [ ! "$1" = "-b" ]
then
    CONTINUE=T
    while [ "$CONTINUE" = "T" ]
    do
	WHICH=99999
	until [ \( $WHICH -le 2 \) -a \( $WHICH -ge 1 \) ];
	do
	    cat << EOF

License Options:

1:    I accept
2:    I do not accept

EOF

echo "You must enter either a 1 or 2."
echo ""
echo-n "Enter the number of your choice: "
	    read WHICH;
	done
	case $WHICH in
	    1)    CONTINUE=F;;
	    2)    exit 0;;
	    *)    echo "You must enter either a 1 or 2." ;;
	esac
    done
fi

# Source file with Spotfire S+ Default Environment variables
if [ ! -r ./splus/lib/install/USER.env ]
then
#    echo Copying $SHOME/splus/lib/install/SPLUS.env to $SHOME/splus/lib/install/USER.env
    cp "$SHOME"/splus/lib/install/SPLUS.env "$SHOME"/splus/lib/install/USER.env
    chmod a+w "$SHOME"/splus/lib/install/USER.env
fi
# No license management code in 8.1 (
if [ "$S_USE_LICENSE_MANAGER" = "yes" ] ; then
    if [ ! -r ./splus/lib/install/LICENSE.env  ]
    then
        echo Copying \"$SHOME\"/splus/lib/install/SPLUSLIC.env to \"$SHOME\"/splus/lib/install/LICENSE.env
        cp "$SHOME"/splus/lib/install/SPLUSLIC.env "$SHOME"/splus/lib/install/LICENSE.env
        chmod a+w "$SHOME"/splus/lib/install/LICENSE.env
    fi
    if [ ! -r ./splus/lib/install/LICRSHCMD ]
    then
        echo Copying \"$SHOME\"/splus/lib/install/SPLUSRSH to \"$SHOME\"/splus/lib/install/LICRSHCMD
        cp "$SHOME"/splus/lib/install/SPLUSRSH "$SHOME"/splus/lib/install/LICRSHCMD
        chmod a+w "$SHOME"/splus/lib/install/LICRSHCMD
    fi
fi
# end of S_USE_LICENSE_MANAGER=yes )
. "$SHOME"/splus/lib/install/USER.env

# Try to create a temp file to check permissions
if touch tmpfile.$$
then
    rm -f tmpfile.$$
else
    echo
    echo "Error: Unable to create a file in the top Spotfire S+ directory `pwd`"
    echo
    echo "You may have to log in as root or otherwise obtain write"
    echo "permission in this directory in order to run CONFIGURE."
    echo
    exit 1
fi

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

DO_LESS=FALSE
OPT_NUM=6

if [ ! "$1" = "-b" ]
then
    CONTINUE=T
    while [ "$CONTINUE" = "T" ]
    do

	WHICH=99999
	until [ \( $WHICH -le $OPT_NUM \) -a \( $WHICH -ge 0 \) ];
	do
	    cat << EOF

Configuration Options:

1:    About This Menu
2:    Graphics Hard Copy Printing
3:    Help File Printing
4:    Destinations and Name of Splus Script
5:    [There is no option 5]
6:    Installation of Splus man page
EOF
	    if [ $DO_LESS = TRUE ]; then
		cat << EOF
7:    Installation of less, a pager like "more" that allows backward movement
8:    Installation of less UNIX man page
EOF
	    fi
	    cat << EOF
0:    Exit this Menu

EOF

echo "You must enter a number between 0 and $OPT_NUM"
echo ""
echo-n "Enter the number of your choice: "

	    read WHICH;
	done

	case $WHICH in
	    0)    CONTINUE=F;;
	    1)    INTROS CONFIG ;;
	    2)    MENU_PRINT ;;
            3)    SET_VARS PRINT ;;
	    4)    MENU_DEST ;;
	    5)    echo "Obsolete option" ;;
	    6)    SET_OPTIONS splus_man ;;
	    7)    SET_OPTIONS less ;;
	    8)    SET_OPTIONS less_man;;
	    *)    echo "You must enter a number between 0 and $OPT_NUM" ;;
	esac
    done
fi

# Create the files that may have changed
echo
echo "Creating the Spotfire S+ Installation files:"

MAKE_LIB     || exit 1
MAKE_X11     || exit 1
MAKE_HELP    || exit 1

# Source file with Spotfire S+ Environment variables
# (AGAIN in case it has changed)
. "$SHOME"/splus/lib/install/USER.env

# 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
#SED_FILE $INSTALL_DIR/S_chap $SHOME/.S_chapters|| exit 1

if [ "$S_USE_LICENSE_MANAGER" = "yes" ] ; then
    # Create the license server rsh cmd file
    echo Copying $INSTALL_DIR/LICRSHCMD to $SHOME/adm/lic/RSHCMD
    cp "$INSTALL_DIR"/LICRSHCMD "$SHOME"/adm/lic/RSHCMD
fi

# Create an executable version of Splus (startup script) in the $SHOME/cmd
# directory and install it, then create a link to S in the same dir
chmod a+x $SHOME/cmd/Splus
cd "$SHOME"/cmd;
test ! -r S && ln -s Splus S
cd "$SHOME"

touch .configured
trap "" 0
if [ "$S_USE_LICENSE_MANAGER" = "yes" ] ; then
    ./HOSTINFO
fi
echo ""
echo "Configuration complete.  Run ./INSTALL to complete installation."
exit 0



