#!/bin/sh
# @(#)CONFIGURE version 3.14 created 2/3/95
# @(#)Copyright (c), 1987, 1995 StatSci, Inc.  All rights reserved.
# This program automates much of the S-PLUS 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 S-PLUS 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 S-PLUS directory"
    exit 1
fi 

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

# Source file with S-Plus 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
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
. $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 S-PLUS directory
    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 S-PLUS; exit 1" 1 2 3

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

Configuration Options:

1:    Graphics Hard Copy Printing
2:    Help File Printing
3:    Destinations for Installation Files
4:    Libraries for Dynamic Loading with dyn.load2 and Static Loading
5:    License management
6:    Installation of S-PLUS UNIX man page
7:    Installation of less, a pager like "more" that allows backward movement
8:    Installation of less  UNIX man page
9:    About This Menu
0:    Exit this Menu 

EOF
echo-n "Enter the number of your choice: "
    read WHICH
    case $WHICH in
        0)    CONTINUE=F;;
        1)    MENU_PRINT ;;
        2)    MENU_HELP ;;
        3)    MENU_DEST ;;
        4)    MENU_LIBS ;;
        5)    MENU_LIC ;;
        6)    SET_OPTIONS splus_man;;
        7)    SET_OPTIONS less ;;
        8)    SET_OPTIONS less_man;;
        9)    INTROS CONFIG ;;
        *)    echo "You must enter a number between 0 and 9" ;;
        esac
    done
fi

# Create the files that may have changed
echo 
echo "Creating the S-PLUS Installation files:"

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

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

# Create the S-Plus file from the S-Plus.pre file stored in $INSTALL_DIR
SED_FILE $INSTALL_DIR/Splus.pre $SHOME/Splus || exit 1

# 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

# Create an executable version of S-Plus as Splus in the $SHOME/cmd
# directory and install it, then create a link to S in the same dir
chmod a+x $SHOME/Splus
echo Copying $SHOME/Splus to $SHOME/cmd/Splus
cp $SHOME/Splus $SHOME/cmd/Splus || exit 1
cd $SHOME/cmd; ln -s Splus S
cd $SHOME

touch .configured
trap "" 0
exit 0
