#!/bin/sh
#@(#)$Revision: 1.75 $  $Date: 03/13/1998 07:12:48 $
#@(#)Copyright(c) 1996 by Rational Software Corporation.
#
#  MODULE SPECIFICATION
#  Name:
#    rinstall
#
#  Description:
#    This install program is intended for both CDROM and FTP installs.
#    For FTP installs, rinstall uses FTP to get Rational products.
#    There are common library functions defined for the do_install program.
#    These functions are placed between the #BEGIN and #END.
#    All functions begin with ri_ so there is not conflict with do_install.
#
#-----------------------------------------------------------------

CMD=`basename $0`
CMD_OPTS="$*"
DIR=`dirname $0`
DIR=`(cd $DIR; pwd)`
FILE=$DIR/$CMD
RM_PRODUCT_LIST="/bin/rm -f"
DOC_LIST=/tmp/$CMD.docs.$$
TMP_DIR=/tmp/$CMD.dir.$$
TMP_FILE=/tmp/$CMD.tmp.$$
INSTALLER=${USER:-$LOGNAME}
CD_LIST=/tmp/$CMD.cd_list.$$
GZIPZ=gzip.1.2.4.tar.Z
PUB_TOOLS=/public/tools
SCRIPTS=$PUB_TOOLS/scripts
Revision=Revision
auto_select_product=no
clear_screen=clear
chg_bg_mode="Change to background.     "
chg_fg_mode="Change to foreground.     "
run_bg_mode="Run in background during FTP transfers."
run_fg_mode="Run in foreground during FTP transfers."
install_only_patch=no
delim="------------------------------------------------------------------------------"

#  Don't allow root to run this script.
if id | grep "uid=0(" >/dev/null
then
    echo
    echo "You cannot be root to run $CMD."
    echo
    exit 1
fi

if [ -n "$HOME" ]
then
    if [ -w $HOME/.Rational ]
    then
        defaults=$HOME/.Rational/$CMD.$INSTALLER
        if [ ! -f $defaults  -a  -f $HOME/.Rational/$CMD ]
        then
            mv $HOME/.Rational/$CMD $defaults
        fi
    elif [ -w $HOME ]
    then
        defaults=$HOME/.$CMD
    else
        defaults=/tmp/$CMD.$INSTALLER
    fi
else
    defaults=/tmp/$CMD.$INSTALLER
fi

#-----------------------------------------------------------------
ri_split_ftp_cmds() {
#-----------------------------------------------------------------

    if echo "$ftp_user_cmnd" | grep ';' >/dev/null
    then
        # Split commands on separate lines.
        set -- `echo "$ftp_user_cmnd" | sed -e "s?;? ; ?g"`
        this_command="this command"
        ftp_user_cmnds=
        while [ $# -gt 0 ]
        do
            if [ "$1" = ';' ]
            then
                this_command="these commands"
                ftp_user_cmnds="$ftp_user_cmnds
"
            else
                ftp_user_cmnds="${ftp_user_cmnds}$1 "
            fi
            shift
        done
    else
        this_command="this command"
        ftp_user_cmnds="$ftp_user_cmnd"
    fi
}

#-----------------------------------------------------------------
ri_get_last_product_info() {
#-----------------------------------------------------------------

    if [ -s $Last_Product ]
    then
        . ./$Last_Product
    fi
}

#-----------------------------------------------------------------
ri_set_defaults() {
#-----------------------------------------------------------------

    this_dir=`pwd`
    #       OLD:                           NEW:
    if [ -s /tmp/$CMD.$INSTALLER  -a  ! -f $defaults ]
    then
        /bin/mv /tmp/$CMD.$INSTALLER $defaults
    fi

    unset bg bgm ftp_background_mode chg_mode
    unset run_mode show_time_delay time_delay

    if [ -s $defaults ]
    then
        . $defaults
        if [ -n "$prod_dir" -a  -d "$prod_dir" ]
        then
            if [ "$prod_dir" != "$this_dir" ]
            then
                set -- 1 `/bin/ls -id $prod_dir 2>/dev/null`
                inode1=$2
                set -- 2 `/bin/ls -id $this_dir 2>/dev/null`
                inode2=$2
                if [ "$inode1" = "$inode2" ]
                then
                    # They are the same.  Use the saved name.
                    prev_prod_dir=$this_dir
                    cd $prod_dir
                else
                    # They are not the same--remember the previous one.
                    prev_prod_dir=$prod_dir
                    prod_dir=$this_dir
                fi
            fi
        else
            prod_dir=$this_dir
        fi
    else
        prod_dir=$this_dir
    fi

    Last_Product=last_product.$CMD
    if [ -s product_info -a ! -f $Last_Product ]
    then
        mv product_info $Last_Product
    fi
    ri_get_last_product_info

    if [ -z "$domain" ]
    then
        domain=`domainname`
    fi

    def_ftp_pass="$INSTALLER@$domain"
    rftp_server=ftp.rational.com
    ftp_command=${ftp_command:-ftp}
    ftp_pass=${ftp_pass:=$def_ftp_pass}
    ftp_user=${ftp_user:-anonymous}
    ftp_server=${ftp_server:-$rftp_server}
    ftp_retry=${ftp_retry:-5}
    email=${email:-$def_ftp_pass}
    explained_log=${explained_log:-no}
    ftp_background_mode=${ftp_background_mode:-no}
    chg_mode="${chg_mode:-$chg_bg_mode}"
    run_mode="${run_mode:-$run_fg_mode}"
    bg=${bg:-bg}
    bgm=${bgm:-fg}
    prod_type=${prod_type:-All}

    # Determine which echo command to use.
    if echo "look for \c" | grep c >/dev/null
    then
        ECHON="echo -n"
        NOCR=
    else
        ECHON=echo
        NOCR='\c'
    fi

    if [ -x /bin/uname ]
    then
        UNAME=/bin/uname
    elif [ -x /usr/5bin/uname ]
    then
        UNAME=/usr/5bin/uname
    else
        echo "Cannot determine the host name. The 'uname' command not found."
        exit 1
    fi
    host=`$UNAME -n`
    sys=`$UNAME -s`
    rel=`$UNAME -r`

    # Determine the install type.  If this is a CDROM install, the current
    # directory contains this program.
    if [ "$CMD" = cdinstall  -o  -x ./$CMD ]
    then
        cd $DIR
        # It will have a directory structure like this:
        all_prod=`/bin/ls */*/*/file0  */*/*/*/file0 2>/dev/null`
        #                 ^ ^ ^        ^ ^ ^ ^
        #                 | | |        | | | \----Version[Patch]
        #                 | | |        | | \----Target
        #                 | | |  -OR-  | \----Host
        #                 | | |        \----Product
        #                 | | |
        #                 | | \----Version[Patch]
        #                 | \----Platform (aix, axp, hp, sgi, sol, sun, etc.)
        #                 \----Product
    fi

    show_only_one=yes
    only_one_prod_plat_ver=no
    if [ -n "$all_prod" ]
    then
        log_file=$defaults.log
        inst_type=CDROM
        product_list=$CD_LIST
        transfer_mode=bulk
        if [ -s README ]
        then
            ri_display_readme -b README
        fi
        if [ `echo "$all_prod" | wc -w` -eq 1 ]
        then
            show_only_one=no
            ri_get_Product
            only_one_prod_plat_ver=yes
        fi
    else
        if [ "$CMD" = cdinstall ]
        then
            echo "Cannot find any products to install from $DIR."
            exit 1
        fi

        log_file=$prod_dir/$CMD.log
        rprod_list_name=rational_list
        lprod_list_name=${CMD}_list.$INSTALLER
        product_list=/tmp/$lprod_list_name
        eval $RM_PRODUCT_LIST $product_list
        inst_type=FTP

        if [ -n "$ftp_user_cmnd" ]
        then
            ri_split_ftp_cmds
        fi
    fi
}

#-----------------------------------------------------------------
ri_save_defaults() {
#-----------------------------------------------------------------
# Save default settings.

    /bin/rm -f $defaults
    echo "
prod_dir='$prod_dir'
product_list='$product_list'
rational_dir='$rational_dir'
VADS_location='$VADS_location'
ftp_pass='$ftp_pass'
ftp_command='$ftp_command'
ftp_user='$ftp_user'
ftp_user_cmnd='$ftp_user_cmnd'
ftp_server='$ftp_server'
ftp_retry='$ftp_retry'
domain='$domain'
email='$email'
transfer_mode='$transfer_mode'
explained_log='$explained_log'
prod_sel='$prod_sel'
rd='$rd'
    " >> $defaults
    chmod 666 $defaults
}

#-----------------------------------------------------------------
ri_save_tmp_defaults() {
#-----------------------------------------------------------------

    echo "
bg='$bg'
bgm='$bgm'
ftp_background_mode='$ftp_background_mode'
chg_mode='$chg_mode'
run_mode='$run_mode'
show_time_delay='$show_time_delay'
time_delay='$time_delay'
    " >> $defaults
}

#-----------------------------------------------------------------
ri_pause() {
#-----------------------------------------------------------------
# Wait for enter to be pressed

    echo
    $ECHON "Press Return to continue. $NOCR"
    read _enter
    unset _enter
}

#BEGIN rinstall shared library functions
# This mark is used for extracting functions shared by the do_install program.
# All functions used to install files from the FTP server should be placed
# Between the marks (#BEGIN & #END ).

#-----------------------------------------------------------------
ri_log() {
#-----------------------------------------------------------------

    case $1 in
          ERR ) sym='***' ; Date= ;;
          NEG ) sym='++*' ; Date= ;;
         NOTE ) sym=''    ; Date= ;;
          aux ) sym=':::' ; Date=`date '+%T'` ;;
          neg ) sym='++*' ; Date=`date '+%T'` ;;
         warn ) sym='!!!' ; Date=`date '+%T'` ;;
          err ) sym='***' ; Date=`date '+%T'` ;;
         note ) sym='---' ; Date=`date '+%T'` ;;
         cont ) sym='...' ;;  # Use previous Date.
    esac
    shift
    echo "$Date $sym $*"
}

#-----------------------------------------------------------------
ri_logn() {
#-----------------------------------------------------------------

    $ECHON "`date '+%T'` --- ${*}$NOCR"
}

#-----------------------------------------------------------------
ri_get_result() {
#-----------------------------------------------------------------
# Get input from the user, providing a prompt value.
#

    while true
    do
        echo
        $ECHON "$1 [$2]  $NOCR"
        read result
        if [ -n "$result" ]
        then
            eval result=\"$result\"
            return
        elif [ -n "$2" ]
        then
            result=$2
            return
        else
            echo "You must enter a value."
        fi
    done
}

#-----------------------------------------------------------------
ri_get_yn() {
#-----------------------------------------------------------------
# Get and validate a yes/no answer.
#

    while true
    do
        ri_get_result "$1" "$2"
        case $result in
            [Yy]* ) yn=yes; return ;;
            [Nn]* ) yn=no;  return ;;
                * ) echo "Please enter 'y' or 'n'." ;;
        esac
    done
}

#------------------------------------------------------------------------
ri_display_readme() {
#------------------------------------------------------------------------
# If a file is more than $LINES or 20 lines, use more.
# Option -b will first display the banner of this program.
# Option -np will avoid calling ri_pause.  This is useful
# when calling this just before a prompt.

    _show_banner=':'
    _pause='ri_pause'
    case "$1" in
    -b )
        _show_banner='ri_banner'
        shift
        ;;
    -np )
        _pause=':'
        shift
        ;;
    esac

    if [ -s $1 ]
    then
        if [ `cat $1 | wc -l` -gt ${LINES:-20} ]
        then
            if [ "$clear_screen" = clear ]
            then
                $clear_screen
                clear_screen=':'
                _reset_cs=clear
            else
                _reset_cs=':'
            fi
            (
            $_show_banner
            cat $1
            ) | more
            clear_screen=$_reset_cs
        else
            (
            $_show_banner
            cat $1
            )
        fi

        eval $_pause
    fi
    unset _show_banner _reset_cs _pause
}

#------------------------------------------------------------------------
ri_start_timer() {
#------------------------------------------------------------------------

    if [ "$1" = now ]
    then
        set -- `date +'%j %H %M %S'`
    fi
    d1=$1
    h1=$2
    m1=$3
    s1=$4
}

#------------------------------------------------------------------------
ri_plural() {
#------------------------------------------------------------------------
# Pluralize arg2 if arg1 is not 1.

    if [ $1 -eq 1 ]
    then
        echo "$1 $2"
    else
        echo "$1 ${2}s"
    fi
}

#------------------------------------------------------------------------
ri_elapse_time() {
#------------------------------------------------------------------------
# Calculate the elapse time in seconds.  (d2,h2,m2,s2 - d1,h1,m1,s1)
# Set sec to this value.

    set -- `date +'%j %H %M %S'`
    d2=$1
    h2=$2
    m2=$3
    s2=$4

    if [ $s2 -lt $s1 ]
    then
        if [ $m2 -gt 0 ]
        then
            m2=`expr $m2 - 1`
        elif [ $h2 -gt 0 ]
        then
            h2=`expr $h2 - 1`
            m2=59
        else
            d2=`expr $d2 - 1`
            h2=23
            m2=59
        fi
        s2=`expr $s2 + 60`
    fi
    s3=`expr $s2 - $s1`

    if [ $m2 -lt $m1 ]
    then
        if [ $h2 -gt 0 ]
        then
            h2=`expr $h2 - 1`
        else
            d2=`expr $d2 - 1`
            h2=23
        fi
        m2=`expr $m2 + 60`
    fi
    m3=`expr $m2 - $m1`

    if [ $h2 -lt $h1 ]
    then
        d2=`expr $d2 - 1`
        h2=`expr $h2 + 24`
    fi
    h3=`expr $h2 - $h1`
    d3=`expr $d2 - $d1`
    sec=`expr $d3 \* 86400 + $h3 \* 1440 + $m3 \* 60 + $s3`
}

#------------------------------------------------------------------------
ri_set_total_bytes() {
#------------------------------------------------------------------------

    if [ -n "$total_bytes" ]
    then
        return
    fi

    if [ -n "$cmp_list" ]
    then
        # Doing an efficient or very_efficient install.
        # Determine total bytes to transfer from component list.
        total_Kbytes=0
        for cn in $cmp_list
        do
            eval _s=\$cmp_${cn}_size
            total_Kbytes=`expr $total_Kbytes + $_s`
        done
        total_bytes=`expr $total_Kbytes \* 1024`
        return
    fi

    total_bytes=0
    for _s in $ftp_sizes_all
    do
        case $_s in
            [0-9]* ) total_bytes=`expr $total_bytes + $_s` ;;
        esac
    done
    total_Kbytes=`expr $total_bytes / 1024 + 1`
}

#------------------------------------------------------------------------
ri_transfer_rate() {
#------------------------------------------------------------------------
# Calculate the transfer rate in bytes/second.  Set bytes_per_sec to this
# value.  Compute it as an average from the total number of bytes transfer
# from the total time.

    total_size=`expr ${total_size:-0} + $exp_size`
    unset file_in_transfer
    total_time=`expr ${total_time:-0} + $sec`
    if [ "$total_time" = 0 ]
    then
        return
    fi
    bytes_per_sec=`expr $total_size / $total_time`
    ri_set_total_bytes
    bytes_to_go=`expr $total_bytes - $total_size`
    if [ "$bytes_per_sec" = 0 ]
    then
        unset bytes_per_sec
        return
    fi
    time_remain=`expr $bytes_to_go / $bytes_per_sec`
}

#------------------------------------------------------------------------
ri_set_dhms() {
#------------------------------------------------------------------------
# Set dhms to the number of days, hours, minutes and seconds from arg1 secs.
# It gets set to something wordy like: 2 days 4 hours 1 minute 15 seconds
# If the times are zero, they are not shown.
# If the times are not equal to 1 the words are pluralized.

    _sec=$1
    unset dhms
    if [ $_sec -ge 86400 ]
    then
        _days=`expr $_sec / 86400`
        _sec=`expr $_sec % 86400`
        dhms="`ri_plural $_days day` "
    fi

    if [ $_sec -ge 3600 ]
    then
        _hrs=`expr $_sec / 3600`
        _sec=`expr $_sec % 3600`
        dhms="${dhms}`ri_plural $_hrs hour` "
    fi

    if [ $_sec -ge 60 ]
    then
        _min=`expr $_sec / 60`
        _sec=`expr $_sec % 60`
        dhms="${dhms}`ri_plural $_min minute` "
    fi

    dhms="${dhms}`ri_plural $_sec second`"
}

#------------------------------------------------------------------------
ri_display_eta() {
#------------------------------------------------------------------------
# Calculate the ETA (estimated time of arrival), given the number of bytes
# and the last transfer rate. Display in days, hours, minutes and seconds.

    if [ -z "$bytes_per_sec" ]
    then
         # Cannot compute until first transfer rate is computed.
         return
    fi

    sec=`expr $exp_size / $bytes_per_sec`

    if [ $sec -eq 0 ]
    then
         sec=1
    fi

    ri_set_dhms $sec

    ri_log note "Estimated time to get file: $dhms"
    eta_set=yes
}

#-----------------------------------------------------------------
ri_say_Got_it() {
#-----------------------------------------------------------------

    if [ "$say_Got_it" = yes ]
    then
        echo "Got it."
        say_Got_it=no
    fi
}

#-----------------------------------------------------------------
ri_display_actual() {
#-----------------------------------------------------------------

    if [ "$eta_set" = yes ]
    then
        say_Got_it=no
        ri_set_dhms $sec
        ri_log note "   Actual time to get file: $dhms"

        if [ -n "$cmp_list" -a -n "$DOING" ]
        then
            # Running from install program so remain time not relevant.
            ri_log note "$DOING now..."
        else
            ri_set_dhms $time_remain
            ri_log note "      Total time remaining: $dhms"
        fi
    else
        ri_say_Got_it
    fi
}

#-----------------------------------------------------------------
ri_update_ftp_sizes() {
#-----------------------------------------------------------------
# Remove first entry from the list of sizes.

    set -- ${ftp_sizes:-0}
    shift
    ftp_sizes="$*"
}

#-----------------------------------------------------------------
ri_check_size() {
#-----------------------------------------------------------------
# Compare size of ftp_dest with size on ftp_sizes list.
# Remove first size from list if size matches expected.
# If arg1 is 'check', then just return status--no messages or anything.

    just_check=${1:-no}
    case "$ftp_name" in
        file[0-9]*|*.tar.gz )
            ;;
        * )
            if [ "$just_check" = check ]
            then
                ri_log note "Already got $ftp_name."
                say_Got_it=no
            else
                ri_say_Got_it
            fi
            return 0
            ;;
    esac

    set -- ${ftp_sizes:-0}
    exp_size=$1

    if [ -f $ftp_dest ]
    then
        set -- `/bin/ls -l $ftp_dest`
        if [ $# -eq 9 ]
        then
            shift
        fi
        actual_size=$4
    else
        echo "File not transferred."
        say_Got_it=no
        return 1
    fi

    if [ "$exp_size" -ne "$actual_size" ]
    then
        if [ "$just_check" = check ]
        then
            return 1
        fi
        echo "File size mismatch."
        say_Got_it=no
        ri_log err  "Size of $compn=$actual_size, expected $exp_size."
        return 1
    elif [ "$just_check" = check ]
    then
        ri_log note "Already got $compn."
        say_Got_it=no
    else
        ri_say_Got_it
        ri_display_actual
    fi

    ri_update_ftp_sizes
    unset actual_size
    return 0
}

#-----------------------------------------------------------------
ri_ftp_out() {
#-----------------------------------------------------------------
    if [ "$ftp_out" = stdout ]
    then
        ${ftp_command:-ftp} -n
    else
        ${ftp_command:-ftp} -n > /dev/null 2>&1
    fi
}

#-----------------------------------------------------------------
ri_ftp_get_file() {
#-----------------------------------------------------------------
# This function needs these shell vars to be set:
# ftp_pass:    FTP password (e.g. user@company.com)
# ftp_retry:   the number of times to try and get a file.
#
# Arg1: file to get
# Arg2: destination directory (default to current directory).
# Arg3: destination name (default to Arg1).

    ftp_file=$1
    ftp_name=`basename $ftp_file`
    case $# in
        1 )
            ftp_dest=$ftp_name
            ;;
        2 )
            ftp_dest=$2/$ftp_name
            ;;
        3 )
            ftp_dest=$2/$3
            ;;
    esac
    ftp_dir=`dirname $ftp_file`
    ftp_server=${ftp_server:-$rftp_server}
    ftp_user=${ftp_user:-anonymous}
    say_Got_it=yes

    set -- ${ftp_sizes:-0}
    exp_size=$1

    case "$transfer_mode" in
        *efficient )
            compn=`echo "$ftp_name" | sed -e "s?file?component #?"`
            ;;
        * )
            compn=$ftp_name
            ;;
    esac

    if [ "$command" = install ]
    then
        ri_logn "Getting component from FTP server, $exp_size bytes..."
        ftp_out=stdout
        do_check=yes

    elif [ -f "$ftp_dest" ]
    then
        Got_it='echo Got it.'
        if ri_check_size check
        then
            return 0
        fi
        ri_log note "Getting $compn, $exp_size bytes, again."
        ri_display_eta
        ftp_out=stdout
        do_check=yes

    else
        Got_it='echo Got it.'
        case "$ftp_dest" in
        *$lprod_list_name )
            ri_logn "Getting product list from $ftp_server..."
            ftp_out=stdout
            do_check=yes
            ;;
        README.patch_info )
            ri_logn "Checking for product patches on $ftp_server..."
            say_Got_it=no
            ftp_out=null
            do_check=no
            ;;
        readme* )
            ri_logn "Getting patch README file #$NP from $ftp_server..."
            ftp_out=null
            do_check=no
            ;;
        install*|*patch* )
            ri_logn "Getting $ftp_name from $ftp_server..."
            ftp_out=null
            do_check=no
            ;;
        $FILE.new )
            ri_logn "Getting newest $CMD from $ftp_server..."
            ftp_out=stdout
            do_check=no
            ;;
        README.* )
            ri_logn "Getting README file from $ftp_server..."
            Got_it='echo'
            ftp_out=null
            do_check=no
            ;;
        readtape )
            ri_logn "Getting readtape program from $ftp_server..."
            ftp_out=stdout
            do_check=no
            ;;
        gzip.* )
            ri_logn "Getting gzip programs from $ftp_server..."
            ftp_out=stdout
            do_check=no
            ;;
        network_install )
            ri_logn "Getting network_install program from $ftp_server..."
            ftp_out=stdout
            do_check=no
            ;;
        file0 )
            ri_logn "Getting file0 (install environment), $exp_size bytes..."
            ftp_out=stdout
            do_check=yes
            ;;
        *.tar.gz )
            say_Got_it=no
            ri_log note "Getting $subprod :" | ri_Sed
            ri_log note "   $compn, $exp_size bytes."
            ri_display_eta
            ftp_out=stdout
            do_check=yes
            ;;
        * )
            say_Got_it=no
            ri_log note "Getting $compn, $exp_size bytes."
            ri_display_eta
            ftp_out=stdout
            do_check=yes
            ;;
        esac
    fi

    r=1
    while [ $r -le $ftp_retry ]
    do
        if [ $r -gt 1 ]
        then
            ri_logn "Retrying $r of $ftp_retry times to get ${ftp_name}..."
        fi

        ri_start_timer now

        file_in_transfer=$ftp_dest
        echo "
            open $ftp_server
            user $ftp_user $ftp_pass
            $ftp_user_cmnds
            bin
            cd $ftp_dir
            get $ftp_name $ftp_dest
        " | ri_ftp_out

        if [ "$do_check" = no  -o  "$ftp_out" = null ]
        then
            ri_say_Got_it
            return
        fi

        ri_elapse_time
        ri_transfer_rate

        if [ -f $ftp_dest ]
        then
            if ri_check_size
            then
                return 0
            fi
        fi

        if [ "$ftp_dest" = "$product_list" ]
        then
            connection_problem=maybe
            ri_log warn "You may have an obsolete version of $CMD."
            ri_log cont "or $CMD cannot connect to FTP server."
            ri_ftp_get_file $SCRIPTS/$CMD $DIR $CMD.new
            if [ -f $FILE.new ]
            then
                unset connection_problem
                ri_log note "Saving old $CMD as $CMD.obs."
                /bin/mv $FILE $FILE.obs
                ri_log note "Installing new $CMD. Will re-run $CMD now."
                /bin/mv $FILE.new $FILE
                chmod +x $FILE
                ri_save_tmp_defaults
                exec $FILE $CMD_OPTS auto_select_product
                exit 0
            fi
        fi

        if [ -n "$connection_problem" ]
        then
            ri_log err  "Cannot connect to FTP server '$ftp_server'."
            ri_log cont "Check the 'f' 'u' 'p' and 'uf' settings."
            exit 1
        fi

        if [ $r -eq $ftp_retry ]
        then
            ri_log err "Failed to get $ftp_name."
        else
            retry_m_1=`expr $ftp_retry - 1`
            minutes=`expr $r \* 2`
            if [ $r -eq $retry_m_1 ]
            then
                tell_wait="Will wait $minutes minutes and try one last time."
            else
                tell_wait="Will wait $minutes minutes and try again."
            fi
            ri_log neg "Failed to get $ftp_name.  $tell_wait"
            sleep `expr $minutes \* 60`
        fi

        r=`expr $r + 1`
    done
    return 1
}

# This mark is used for extracting functions shared by the do_install program.
# All functions used to install files from the FTP server should be placed
# Between the marks (#BEGIN & #END ).
#END rinstall shared library functions

#-----------------------------------------------------------------
ri_get_file_and_check() {
#-----------------------------------------------------------------

    file_to_get=$1
    fail_mesg="$2"
    ri_ftp_get_file $file_to_get
    if [ $? -ne 0 ]
    then
        ri_log err "Failed to get $file_to_get"
        ri_log cont "Aborting $CMD."
        ri_log_activity "$fail_mesg"
        exit 1
    fi
}

#-----------------------------------------------------------------
ri_check_disk_space() {
#-----------------------------------------------------------------

    if [ "$transfer_mode" = very_efficient ]
    then
        return
    fi

    if [ "$prod_sel" = vads ]
    then
        _size=$total_Kbytes
    elif [ "$transfer_mode" = bulk ]
    then
        _size=$total_Kbytes
    elif [ "$1" = defer ]
    then
        return
    else
        _size=$2
    fi

    DF=/bin/df
    avail=avail
    case "${sys}$rel" in
        SunOS5* ) DF="/bin/df -k" ;;
          OSF1* ) DF="/bin/df -k" ;;
         HP-UX* ) DF=/usr/bin/bdf ;;
          IRIX* ) DF="/usr/bin/df -k" ;;
           AIX* ) avail=free ;;
    esac
    echo "
You need $_size Kbytes to download $prod_ver_plat.
Check the space you have '$avail':

% $DF $prod_dir"
    eval $DF $prod_dir

    ri_get_yn "Would you like to continue with the download?" yes
    if [ $yn = no ]
    then
         exit 1
    fi
}

#-----------------------------------------------------------------
ri_mk_dir() {
#-----------------------------------------------------------------
# Input dir - Directory to make.

    echo
    echo $dir not found.
    ri_get_yn "Do you want to make this directory now?" yes
    if [ $yn = yes ]
    then
        # mkdir -p $dir
        _DIR_=/
        for _dir_ in `echo $dir | sed -e 's?/? ?g'`
        do
            if [ "$_dir_" != "/"  -a \
                 "$_dir_" != "."  -a \
                 "$_dir_" != ".."    ]
            then
                _DIR_=${_DIR_}$_dir_
                if [ ! -d $_DIR_ ]
                then
                    mkdir $_DIR_
                    if [ $? -ne 0 ]
                    then
                        unset _DIR_ _dir_
                        return 1
                    fi
                fi
                _DIR_=${_DIR_}/
            fi
        done
        unset _DIR_ _dir_
        return 0
    fi
    return 1
}

#-----------------------------------------------------------------
ri_set_ftp_dirs() {
#-----------------------------------------------------------------

    if [ "$prod_sel" = vads ]
    then
        case "$Prod" in
            *cross ) target=cross ;;
            *works ) target=works ;;
             *self ) target=self ;;
        esac
        ftp_src_dir=/public/vads/releases/$plat/$target/$ver
        ftp_doc_dir=/public/$prod/documents/unix
        return
    fi

    case $prod in
        apexlx ) ftp_src_dir=/public/apex_cross/releases/$plat/lx/$ver
                 ftp_doc_dir=/public/apex_cross/documents/unix/LynxOS
                 ;;
        apexrt ) ftp_src_dir=/public/apex_cross/releases/$plat/rt/$ver
                 ftp_doc_dir=/public/apex_cross/documents/unix/Rexec
                 ;;
        apexvw ) ftp_src_dir=/public/apex_cross/releases/$plat/vw/$ver
                 ftp_doc_dir=/public/apex_cross/documents/unix/VxWorks
                 ;;
             * ) ftp_src_dir=/public/$prod/releases/$plat/$product.$ver
                 ftp_doc_dir=/public/$prod/documents/unix
                 ;;
    esac
}

#-----------------------------------------------------------------
ri_post_patch() {
#-----------------------------------------------------------------

    if grep $patch_program $post_patch >/dev/null 2>&1
    then
        return
    fi

    grep ./share/install_lib $post_patch >/dev/null 2>&1
    if [ $? -ne 0 ]
    then
        (
        echo
        echo 'if [ -z "$ARCH" -o  -z "$install_defaults" ]'
        echo 'then'
        echo '    . ./share/install_lib'
        echo 'fi'
        ) >> $post_patch
    fi

    grep ./.install_defaults $post_patch >/dev/null 2>&1
    if [ $? -ne 0 ]
    then
        (
        echo
        echo 'if [ -z "$prod_base" ]'
        echo 'then'
        echo '    . ./.install_defaults '
        echo 'fi'
        ) >> $post_patch
    fi

    grep ARCH/prod_desc $post_patch >/dev/null 2>&1
    if [ $? -ne 0 ]
    then
        (
        echo
        echo 'if [ -z "$home_var" -o -z "$product_version" ]'
        echo 'then'
        echo '    . $ARCH/prod_desc'
        echo 'fi'
        ) >> $post_patch
    fi

    grep HOME_VAR= $post_patch >/dev/null 2>&1
    if [ $? -ne 0 ]
    then
        (
        echo
        echo 'HOME_VAR=$home_var'
        echo 'set_home_var $HOME_VAR'
        echo 'export HOME_VAR $HOME_VAR'
        if [ $prod = apex ]
        then
            echo 'APEX_PROD_VERSION=${version_patch:-$product_version}'
            echo 'export APEX_PROD_VERSION'
        fi
        ) >> $post_patch
    fi

    if [ "$post_patch" = post_patch ]
    then
        patch_from=\$disk_install_dir
        grep share/disk_install_dir $post_patch >/dev/null 2>&1
        if [ $? -ne 0 ]
        then
            (
            echo
            echo 'if [ -z "$disk_install_dir" ]'
            echo 'then'
            echo '    if [ -s share/disk_install_dir ]'
            echo '    then'
            echo '        . share/disk_install_dir'
            echo '    else'
            echo '        . $TARGET_ARCH/disk_install_dir'
            echo '    fi'
            echo 'fi'
            ) >> $post_patch
        fi
    else
        patch_from=$install_from
    fi

    (
    echo
    echo "if [ -x $patch_from/$patch_program ]"
    echo "then"
    echo "    header 'Installing Patch $patch_id'"
    echo "    (cd $patch_from; ./$patch_program)"
    echo "    if [ -x ./patches/$patch_program ]"
    echo "    then"
    echo "        get_yn 'Would you like to commit patch $patch_id now?' yes"
    echo '        if [ "$result" = yes ]'
    echo "        then"
    echo "            ./patches/$patch_program -commit"
    echo "        fi"
    echo "    fi"
    echo "fi"
    echo
    ) >> $post_patch

    if [ ! -f post_patch_added ]
    then
        patches="post_patch $patches"
        touch post_patch_added
        chmod a+x $post_patch 2>/dev/null
    fi
}

#-----------------------------------------------------------------
ri_ask_install_patch() {
#-----------------------------------------------------------------

    (
    echo
    echo "$NP. Description of patch $patch_id:"
    sed -e '/To install /,$d' -e 's?^?   ?' $patch_readme
    ) > $TMP_FILE
    ri_display_readme -np $TMP_FILE
    /bin/rm -f $TMP_FILE
    ri_get_yn "Would you like to install patch $patch_id?" yes
    if [ $yn = no ]
    then
        return
    else
        NP_list="$NP_list $NP"
    fi
}

#-----------------------------------------------------------------
ri_install_patch() {
#-----------------------------------------------------------------

    if [ -z "$NP_list" ]
    then
        Recommend="Recommended--takes less disk space on the install machine:"
        Alternate="Alternate method:"
        unset no_patches yes_patches
        return
    fi

    if [ -z "$install_dir" ]
    then
        post_patch=post_patch
    elif [ -d $install_dir ]
    then
        post_patch=$install_dir/post_patch
    else
        post_patch=post_patch
    fi

    Enter=Enter
    while [ "$post_patch" = post_patch -a "$install_only_patch" = yes ]
    do
        if [ "$defer_install" = yes ]
        then
            def_install_dir=defer
        fi

        echo "
$Enter the $Prod_ver install directory.
You can enter 'defer' if you are going to install the patches later
or if you plan on installing them on a different machine."

        ri_get_result "$Enter dir or 'defer':" $def_install_dir
        if [ "$result" = defer ]
        then
            install_dir=defer
            break
        elif [ -d "$result" ]
        then
            install_dir=$result
            post_patch=$install_dir/post_patch
        else
            ri_log neg "$result not found."
            echo "You must specify the product install directory."
            ri_get_yn "Would you like to exit $CMD?" no
            if [ $yn = yes ]
            then
                exit 1
            fi
            Enter="Re-enter"
        fi
    done

    echo
    for NP in $NP_list
    do
        eval patch_id=\$patch_id$NP
        eval patch_dir=\$patch_dir$NP
        eval patch_program=\$patch_program$NP
        eval patch_inventory=\$patch_inventory$NP
        eval patch_tarfile=\$patch_tarfile$NP

        for patch_file in $patch_program $patch_inventory $patch_tarfile
        do
            ri_ftp_get_file $patch_dir/$patch_file
            if [ ! -f $patch_file ]
            then
                echo "Failed."
                ri_log neg "Patch $patch_id not available."
                continue 2
            fi
        done
        chmod 775 $patch_program

        ri_log note "Will install patch $patch_id during post_install."
        patches="$patches $patch_program $patch_inventory $patch_tarfile"
        Recommend="Not recommended--there are patches to install:"
        Alternate="Recommended method since you have patches to install:"
        no_patches="(Cannot put patches on this tape)"
        yes_patches="(Can include patches)"
        do_duptape=no

        # Have post_install install the patch
        ri_post_patch
    done

    if [ "$install_only_patch" = yes ]
    then
        if [ "$install_dir" = defer ]
        then
            echo
            echo "The following files have been downloaded:"
            echo
            /bin/ls -CF $patches
            echo
            echo "Copy these files to $prod_ver_plat"
            echo "install directory and run post_patch."
        else
            if [ -x $install_dir/post_patch ]
            then
                echo
                echo "Installing patches now.."
                echo
                (
                echo cd $install_dir
                     cd $install_dir
                echo ./post_patch
                     ./post_patch
                )
                exit $?
            else
                echo "There are no patches to install."
                exit 0
            fi
        fi
    fi
}

#-----------------------------------------------------------------
ri_get_patch_info() {
#-----------------------------------------------------------------

    num_patches=0
    if [ "$inst_type" = CDROM ]
    then
        return
    fi

    unset NP_list patches
    /bin/rm -f README.patch_info
    echo
    ri_ftp_get_file $ftp_src_dir/README.patch_info
    if [ ! -s README.patch_info ]
    then
        echo None.
        ri_pause
        return
    fi

    . ./README.patch_info
    echo $num_patches available.

    NP=1
    while [ $NP -le $num_patches ]
    do
        eval patch_readme=\$patch_readme$NP
        if [ -n "$patch_readme" ]
        then
            eval patch_dir=\$patch_dir$NP
            ri_ftp_get_file $patch_dir/$patch_readme
            if [ -f $patch_readme ]
            then
                eval patch_id=\$patch_id$NP
                ri_ask_install_patch
            fi
        fi
        NP=`expr $NP + 1`
    done
}

#-----------------------------------------------------------------
ri_explain_beta() {
#-----------------------------------------------------------------

    ri_get_yn "Do you have a signed Beta agreement with Rational?"
    if [ $yn = yes ]
    then
        return 0
    fi

    echo "
You must have a signed Beta agreement to install this product.

If you know your Rational sales representative, call them for
a Beta agreement, otherwise contact the Rational Software Corporation
SalesCenter.  In the U.S. and Canada call 1-800-728-1212 or 703-761-4400.
E-mail: product_info@rational.com"

    if [ "$only_one_prod_plat_ver" = yes ]
    then
        exit 0
    fi

    ri_get_yn "Would you like to select a different product?"
    if [ $yn = no ]
    then
        exit 0
    else
        return 1
    fi
}

#-----------------------------------------------------------------
ri_get_prod_readme() {
#-----------------------------------------------------------------

    if [ ! -f README.$Type ]
    then
        echo
        ri_ftp_get_file $ftp_src_dir/README.$Type
    fi

    echo "
You have selected $Prod $ver for $Plat."

    if [ ! -f README.$Type ]
    then
        case "$Type" in
        Older )
            echo "
This is an older release.  A newer release is now available.
" > README.$Type
            ;;
        GA_Candidate|GA_Pending )
            echo "
This release is pending Generally Available (GA) status as it goes
through the final steps of the manufacturing process.  We anticipate
that this will be complete within the next 30 days.  Once this release
reaches GA status, it will be available for shipping.  Until then, it
is being provided on this FTP server for immediate access.
" > README.$Type
            ;;
        GA* )
            echo "
This is the Generally Available (GA) release.
" > README.$Type
            ;;
        Alpha )
            echo "
This is the alpha release.  Unless you have been instructed by a
Rational account representative to install this version of the
product, you should install the generally available (GA) version.
" > README.$Type
            ;;
        Early_Access )
            echo "
This is an early access release.  Unless you have been instructed by a
Rational account representative to install this version of the
product, you should install the generally available (GA) version.

This Early-Access release is being made available to selected customers
or prospects for evaluation or production work.  The goal is to gain
extensive feedback from external users on the basic functionality and
product direction.  Feedback on this Early-Access release may lead to
major changes in the product before the Beta Release.
" > README.$Type
            ;;
        Beta )
            echo "
This is the beta release.  Unless you have been instructed by a
Rational account representative to install this version of the
product, you should install the generally available (GA) version.

Before you use this version of the product, you need to have a
signed Beta agreement with Rational.

All communications to support about this release should be sent
via E-mail to support@rational.com.
" > README.$Type
            ;;
        Update )
            echo "
This is an update release.  Unless you have been instructed by a
Rational account representative to install this version of the
product, you should install the generally available (GA) version.
" > README.$Type
            ;;
        Patch )
            echo "
This is a patch release.  Unless you have been instructed by a
Rational account representative to install this version of the
product, you should install the generally available (GA) version.
" > README.$Type
            ;;
        Special )
            echo "
This is a special release.  There may be known defects in this release.
Unless you have been instructed by a Rational account representative to
install this version of the product, you should install the generally
available (GA) version.
" > README.$Type
            ;;
        Private|K*X )
            echo "
This is a private release.  You should not be installing this release
unless you have been instructed to do so by a Rational account representative.
Otherwise, you should install the generally available (GA) version.
" > README.$Type
            ;;
        * )
            echo "
This is the current release.
" > README.$Type
            ;;
        esac
    fi

    ri_display_readme -np README.$Type

    if [ "$Type" = Beta ]
    then
        if ri_explain_beta
        then
            return
        else
            ri_get_Product
        fi
    else
        ri_get_yn "Are you satisfied with your product selection?"
        if [ $yn = no ]
        then
            if [ "$only_one_prod_plat_ver" = yes ]
            then
                if [ "$prod_type" = All ]
                then
                    exit 0
                else
                    ri_select_prod_type
                fi
            fi
            unset only_one_prod_plat_ver
            ri_get_Product
        fi
    fi

    if [ "$prod_sel" = vads ]
    then
        ri_get_VADS_location auto
    fi
}

#-----------------------------------------------------------------
ri_show_prod_readme() {
#-----------------------------------------------------------------

    do_next_step=':'
    for readme_file in $prod_dir/$prod/$plat/$ver/README*
    do
        if [ -s $readme_file ]
        then
            echo
            echo "Product selected: $prod $ver/$plat" | ri_Sed
            echo

            case $readme_file in
            *BETA|*Beta|*beta )
                Type=Beta
                ri_display_readme $readme_file
                if ri_explain_beta
                then
                    do_next_step='echo ; ri_pause'
                else
                    do_next_step='ri_get_Product'
                    break
                fi
                ;;
            * )
                ri_display_readme $readme_file
                do_next_step='echo'
                ;;
            esac
        fi
    done
    eval $do_next_step
}

#-----------------------------------------------------------------
ri_check_prod_dir() {
#-----------------------------------------------------------------

    if [ "$inst_type" = CDROM ]
    then
        return
    fi

    ri_get_last_product_info

    if [ "$prod_sel" = vads ]
    then
        ifiles=`/bin/ls -C *.tar.gz 2>/dev/null`
    else
        ifiles=`/bin/ls -C file[0-9]* 2>/dev/null`
    fi

    if [ -n "$ifiles" ]
    then
        have_files=yes
    else
        have_files=no
    fi

    if [ "$last_pvp" != "$prod.$ver.$plat"  -a  $have_files = yes ]
    then
        echo "
This program has been run in $prod_dir before.
The last time it was run, $Last_Prod
was selected.  These files are part of that product release:

$ifiles
"

        echo "
These files need to be removed before $Prod $ver for $Plat
can be downloaded from the FTP server."

        ri_get_yn "Remove these files?" no
        if [ $yn = no ]
        then
            echo
            echo "Switching back to $Last_Prod.  You need to"
            echo "remove the files or select a different storage directory (Key: d)."
            prod="$last_prod"
            plat="$last_plat"
            ver="$last_ver"
            Prod="$last_Prod"
            Plat="$last_Plat"
            ri_get_sizes
            ri_pause
            return
        fi

        echo "Removing $Last_Prod files..."
        if [ "$prod_sel" = vads ]
        then
            /bin/rm -f *.tar.gz README.[A-Z]* 2>/dev/null
        else
            /bin/rm -f file[0-9] file[1-9][0-9] README.[A-Z]* 2>/dev/null
        fi
        echo "Done."
        ri_pause
    elif [  "$last_pvp" != "$prod.$ver.$plat" ]
    then
            /bin/rm -f README.[A-Z]* 2>/dev/null
    fi

    /bin/rm -f $Last_Product $DOC_LIST
    echo "
    last_prod='$prod'
    last_Prod='$Prod'
    last_ver='$ver'
    last_plat='$plat'
    last_Plat='$Plat'
    last_pvp='$prod.$ver.$plat'
    Last_Prod='$Prod $ver for $Plat'
    " > $Last_Product
    last_prod="$prod"
    last_plat="$plat"
    last_ver="$ver"
}

#-----------------------------------------------------------------
ri_get_ftp_command() {
#-----------------------------------------------------------------

    echo
    echo "This is the command to use to FTP across the Internet to"
    echo "Rational's FTP server."

    ri_get_result "Enter the FTP command to use:" $ftp_command
    ftp_command=$result

    ri_get_result "Enter the FTP server to use:" $ftp_server
    ftp_server=$result
}

#-----------------------------------------------------------------
ri_get_ftp_user() {
#-----------------------------------------------------------------

    echo
    echo "This is the FTP user.  For regular FTP command, use anonymous."
    echo "For gatekeeper, use anonymous@$rftp_server. For other FTP"
    echo "wrappers, you're on your own."

    ri_get_result "Enter the FTP user:" ${ftp_user:-anonymous}
    ftp_user=$result
}

#-----------------------------------------------------------------
ri_get_ftp_pass() {
#-----------------------------------------------------------------

    echo "
You need to specify the password for the $ftp_user FTP user.
Specify your complete E-mail address as the password.
For example: $def_ftp_pass"

    while true
    do
        ri_get_result "Enter your complete E-mail address:" $ftp_pass
        if [ -n "$ftp_user_cmnd" ]
        then
            # Do not check if using this.
            break
        fi

        if echo "$result" | grep "@.*\." >/dev/null
        then
            break
        fi
        echo "'$result' does not have the proper format."
        echo "Your password should look something like $def_ftp_pass."
    done
    ftp_pass=$result

    if [ "$email" = "$ftp_pass" ]
    then
        ri_get_yn "Can this be used to send you information via E-mail?" yes
    else
        ri_get_yn "Can this be used to send you information via E-mail?" no
    fi

    if [ "$yn" = yes ]
    then
        email=$ftp_pass
        domain=`echo $ftp_pass | awk -F@ '{print $2}'`
        return
    elif [ "$email" = "$ftp_pass" ]
    then
        email=$def_ftp_pass
    fi

    while true
    do
        ri_get_result "Enter an E-mail address that can:" "$email"
        if echo "$result" | grep "@.*\." >/dev/null
        then
            break
        fi
        echo "'$result' does not have the proper format."
        echo "Your E-mail address should look something like $def_ftp_pass."
    done
    email=$result
    domain=`echo $email | awk -F@ '{print $2}'`
}

#-----------------------------------------------------------------
ri_get_ftp_user_cmnd() {
#-----------------------------------------------------------------

    echo "
You can specify a command to the FTP server after logging in, but prior
to getting files.  This is useful if you have an FTP server that you must
log into first before you can reach Rational's FTP server.  It can also
be use as a hook to run commands within FTP.  If you want to specify
multiple commands, you must use the semi-colon (;) as a command separator.

For example, having the FTP login set like this:

  f )  FTP command:            FTP local_gateway
  u )  FTP user:               local_user
  p )  FTP user password:      local_password
  uf)  User FTP command:       user ftp@$rftp_server ${ftp_pass:-$def_ftp_pass}

may allow you to FTP login to your local gateway machine and from their
FTP to Rational's server."

    ri_get_result "Enter a user defined FTP command or 0 to clear:" \
        "${ftp_user_cmnd:-0}"
    if [ "$result" = 0 ]
    then
        unset ftp_user_cmnd ftp_user_cmnds
        return
    fi

    ftp_user_cmnd="$result"
    ri_split_ftp_cmds

    echo "
Will add $this_command to the FTP login:
$ftp_user_cmnds"
    ri_pause
}

#-----------------------------------------------------------------
# Generate a unique list of products, platforms or versions.
#
# For FTP installs, the product_list file is built by the make_product_list
# that runs regularly (every 15 minutes) on the FTP server.
# This file has the format:
#  <prod> <plat> <ver> [<size_of_file0> <size_of_file1> ...]
#
# The next four functions do this.
#

#-----------------------------------------------------------------
ri_filter_prod_list() {
#-----------------------------------------------------------------

    case $prod_type in
    Alpha | Beta | Early_Access | Private | K*X | Special )
        grep " $prod_type " $product_list
        ;;
    * )
        egrep -v " Alpha | Beta | Early_Access | Private | K.*X | Special " $product_list
        ;;
    esac
}

#-----------------------------------------------------------------
ri_get_prod_list() {
#-----------------------------------------------------------------
# Generate a unique list of prods:

    ri_filter_prod_list | awk '{print $1}' | sort -uf
}

#-----------------------------------------------------------------
ri_get_plat_list() {
#-----------------------------------------------------------------
# Generate a unique list of platforms for this prod:

    ri_filter_prod_list | grep "^$prod " | awk '{print $2}' | sort -uf
}

#-----------------------------------------------------------------
ri_get_ver_list() {
#-----------------------------------------------------------------
# Generate a unique list of versions for this prod & plat:

    ri_filter_prod_list | grep "^$prod $plat " | awk '{print $3}' | sort -uf
}

#-----------------------------------------------------------------
ri_show_ppv_type() {
#-----------------------------------------------------------------

    for readme in `/bin/ls $dir/README.[A-Z]* 2>/dev/null`
    do
        if [ -f $readme ]
        then
            type=`echo $readme | sed -e "s?$dir/README.??"`
            echo "$ppv $type"
            return
        fi
    done
    echo "$ppv"
}

#-----------------------------------------------------------------
ri_get_product_list() {
#-----------------------------------------------------------------

    if [ "$inst_type" = CDROM ]
    then
        echo "$all_prod" | sed -e "s?/? ?g" | while read entry
        do
            set -- $entry
            if [ $# -eq 5 ]
            then
                # arg2 and arg3 are a Host->Target combination.
                ppv="$1 $2/$3 $4"
                dir=$1/$2/$3/$4
            else
                ppv="$1 $2 $3"
                dir="$1/$2/$3"
            fi

            ri_show_ppv_type
        done > $product_list
        return 0
    fi

    if [ -z "$ftp_pass" ]
    then
        echo
        echo "You must enter the '$ftp_user' password before you can select a product."
        return 1
    fi

    if [ -f $product_list ]
    then
        return 0
    fi

    if ri_ftp_get_file $SCRIPTS/$rprod_list_name  /tmp $lprod_list_name
    then
        return 0
    fi
    return 1
}

#-----------------------------------------------------------------
ri_Sed() {
#-----------------------------------------------------------------

sed -e "s? [1-9]:? & ?" \
    -e "s? [1-9][0-9]:?& ?" \
    -e "s?rose/rose_apex?Rational Rose/C++ for Apex?" \
    -e "s?rose_apex?Rational Rose/C++ for Apex?" \
    -e "s?ada_analyzer?Ada Analyzer?" \
    -e "s?apexlx?Rational Apex for LynxOS?" \
    -e "s?apexrt?Rational Apex for Rational Exec?" \
    -e "s?apexvw?Rational Apex for VxWorks?" \
    -e "s?apex_ada?Rational Apex Ada?" \
    -e "s?apex_cpp?Rational Apex C/C++?" \
    -e "s?apex_duo?Rational Apex Duo?" \
    -e "s?apex?Rational Apex?" \
    -e "s?\.Rational Apex\.?.apex.?" \
    -e "s? axi ? AXI (Ada/X Interface)?" \
    -e "s?axi?AXI (Ada/X Interface)?" \
    -e "s?cov_analyzer?Coverage Analyzer?" \
    -e "s?oca?OIS Open Client/Ada?" \
    -e "s?rci?The RCI?" \
    -e "s?puretools?Purify/PureCoverage/Quantify?" \
    -e "s?rose/rose_dev?Rational Rose Developer (Rose/C++ for Apex)?" \
    -e "s?rose/rose_cpp?Rational Rose/C++?" \
    -e "s?rose/rose_ada?Rational Rose/Ada?" \
    -e "s?rose/rose_comb?Rational Rose Modeler \& C++?" \
    -e "s?rose/rose?Rational Rose?" \
    -e "s?rose_cpp?Rational Rose/C++?" \
    -e "s?rose_dev?Rational Rose Developer (Rose/C++ for Apex)?" \
    -e "s?rose_ada?Rational Rose/Ada?" \
    -e "s?rose_comb?Rational Rose Modeler \& C++?" \
    -e "s?rose?Rational Rose?" \
    -e "s?screen_machine?OIS Screen Machine?" \
    -e "s?soda?SoDA?" \
    -e "s?spire?Rational Spire?" \
    -e "s?summit?Rational Summit?" \
    -e "s?testmate?Rational TestMate?" \
    -e "s?/alpha?->Alpha AXP?" \
    -e "s?/cpu32?->cpu32 68332/340/360?" \
    -e "s?/hppa?->HP-PA?" \
    -e "s?/hprt?->HP-RT?" \
    -e "s?/x86?->Intel x86/Pentium?" \
    -e "s?/68k?->Motorola 68k?" \
    -e "s?/mips1?->MIPS I?" \
    -e "s?/mips2?->MIPS II?" \
    -e "s?/mips3?->MIPS III?" \
    -e "s?/mips4?->MIPS IV?" \
    -e "s?/mips?->MIPS?" \
    -e "s?/power?->PowerPC?" \
    -e "s?/rh32?->rh32?" \
    -e "s?/rad6000?->rad 6000?" \
    -e "s?/sparc?->SPARC?" \
    -e "s?aix?AIX?" \
    -e "s?axp?Digital Unix?" \
    -e "s?hp?HP-UX?" \
    -e "s?sgi?SGI/IRIX?" \
    -e "s?sunsol?SunOS \& Solaris?" \
    -e "s?sol?Solaris?" \
    -e "s?sun?SunOS?" \
    -e "s?win95?MS Win95/WinNT?" \
    -e "s?win?MS Windows 3.1?" \
    -e "s?GA_?GA ?" \
    -e "s? asm ? Assembler?" \
    -e "s? cca ? Code Coverage Analyzer?" \
    -e "s? issim ? Instruction Set Simulator?" \
    -e "s? mp ? VADSmp add-on to the base compiler?" \
    -e "s? net ? Ethernet download, debug and TCP/IP support?" \
    -e "s? prof ? Statistical Profiler?" \
    -e "s? trace ? Tasking Logic Analyzer?" \
    -e "s? vads ? VADS Ada compiler?" \
    -e "s? vcc ? C/C++ add-on for the Ada compiler?" \
    -e "s? toolkit ? Object format converter and symbol table generator?" \
    -e "s?_? ?g"

}

#-----------------------------------------------------------------
ri_set_select() {
#-----------------------------------------------------------------

    if [ `echo "$1 $2" | wc -c` -gt 78 ]
    then
        _select="$1
$2"
    else
        _select="$1 $2"
    fi
}

#-----------------------------------------------------------------
ri_check_result() {
#-----------------------------------------------------------------

    _lb=$1
    _ub=$2
    case "$result" in
        \!* )
            echo "Illegal range of numbers."
            ;;
        *-* )
            echo "You cannot enter a range of numbers."
            ;;
        *,* )
            echo "You cannot enter a comma-separated list of numbers."
            ;;
        *' '* )
            echo "You cannot enter a space-separated list of numbers."
            ;;
        [1-9]|[1-9][0-9] )
            if [ "$result" -ge $_lb  -a  "$result" -le $_ub ]
            then
                return 0
            fi
            ;;
    esac
    return 1
}

#-----------------------------------------------------------------
ri_get_n_range() {
#-----------------------------------------------------------------

    unset n_range
    for _r in `echo "$result" | sed -e 's?,? ?g'`
    do
        case "$_r" in
            [1-9]*-[0-9]* )
                set -- `echo $_r | sed -e 's?-? ?'`
                _lb=$1
                _ub=$2
                if [ $_ub -lt $_lb ]
                then
                    n_range="$n_range !$_r"
                else
                    while [ $_lb -le $_ub ]
                    do
                        n_range="$n_range $_lb"
                        _lb=`expr $_lb + 1`
                    done
                fi
                ;;
            [1-9]|[1-9][0-9] )
                n_range="$n_range $_r"
                ;;
        esac
    done
}

#-----------------------------------------------------------------
ri_get_var() {
#-----------------------------------------------------------------
# Set the var (prod, plat,...) from the list of items in the product_list file.

    var=$1
    var_name=$2
    shift 2
    if [ $# -lt 2 ]
    then
        eval $var=\"$1\"
        val=`echo $1 | ri_Sed`
        if [ -z "$val" ]
        then
            if [ "$prod_type" = All ]
            then
                echo "*** There are no ${var_name}s to select.  Aborting."
            else
                echo "*** There are no $prod_type ${var_name}s to select.  Aborting."
            fi
            exit 1
        fi

        if [ "$run_via_program" = yes ]
        then
            return
        fi

        if [ "$show_only_one" = yes ]
        then
            echo
            echo "   There is only one $var_name to choose from."
            echo

            auto_selected="has been automatically selected"
            if [ "$prod_type" != All ]
            then
                auto_selected="$auto_selected
   using product type '$prod_type'"
            fi

            if [ `echo "$var_name '$val'" | wc -c` -gt 35 ]
            then
                echo "   The $var_name '$val'"
                echo "   $auto_selected."
            else
                echo "   The $var_name '$val' $auto_selected."
            fi
            ri_pause
        fi

        eval only_one_$var=yes
        if [ "${var}" = ver ]
        then
            if [ "${only_one_prod}${only_one_plat}${only_one_ver}" = yesyesyes ]
            then
                only_one_prod_plat_ver=yes
            fi
        fi
        return
    fi
    eval only_one_$var=no

    (ri_banner)

    _sp="                       "
    case $var in
        prod ) echo "$_sp Product Selection Menu" ;;
        plat ) echo "$_sp $Platform Selection Menu" ;;
        ver  ) echo "$_sp Product Version Selection Menu" ;;
    esac

    echo "$delim

$_select
"

    _in=0
    _defn=
    for _item in $*
    do
        _in=`expr $_in + 1`
        if [ "$_item" = "$_def" ]
        then
            _defn=$_in
            _mark=">>>>"
        else
            _mark="    "
        fi
        eval _item$_in=\"$_item\"
        if [ "$var" = ver ]
        then
            _type=`grep "^$prod $plat $_item " $product_list | awk '{print $4}'`
            if [ -n "$_type" ]
            then
                eval _type$_in=\"$_type\"
                echo "${_mark} ${_in}:" $_item "  ($_type Release)" | ri_Sed
            else
                echo "${_mark} ${_in}:" $_item | ri_Sed
            fi
        else
            echo "${_mark} ${_in}:" $_item | ri_Sed
        fi
    done

    echo
    _ropt=", r"
    case $var in
        prod ) _ropt= ;;
    plat|ver ) echo "      r:  return to the previous menu"  ;;
    esac
    echo "      q:  Quit $CMD"

    while true
    do
        ri_get_result "Enter $var_name number (1 to $_in)$_ropt or q" $_defn
        case "$result" in
            q|Q )
                exit 0
                ;;
            r|R )
                if [ "$var" != prod ]
                then
                    return 99
                fi
                ;;
            pr|pt|PR|PT )
                if [ "$var" = ver ]
                then
                    if ri_select_prod_type
                    then
                        ri_get_var ver "product version" `ri_get_ver_list`
                    fi
                    return
                fi
                ;;
            * )
                if ri_check_result 1 $_in
                then
                    eval $var=\$_item$result
                    eval Type=\$_type$result
                    break
                fi
                echo "'$result' out of range."
                ;;
        esac
        echo "You must enter a number from 1 to ${_in}$_ropt or q."
    done
}

#-----------------------------------------------------------------
ri_select_prod_type() {
#-----------------------------------------------------------------

    sel_status=0
    echo
    echo "Product type is now set to '$prod_type'."
    ri_get_result "Enter new product type (All, Beta, Private, Special or Passwd):" All
    case $result in
    a*|A* )
        prod_type=All
        ;;
    pr*|Pr* )
        prod_type=Private
        ;;
    s*|S* )
        prod_type=Special
        ;;
    b*|B* )
        prod_type=Beta
        ;;
    pa*|Pa* )
        ri_get_result "Enter product password:"
        case $result in
        K*X )
            prod_type=$result
            ;;
        * )
            echo Passwd is not valid.
            prod_type=All
            sel_status=1
            ;;
        esac
        ;;
    * )
        echo "Product type '$result' not recognized."
        prod_type=All
        sel_status=1
        ;;
    esac
    echo
    echo "Product type set to '$prod_type'."
    ri_pause
    return $sel_status
}

#-----------------------------------------------------------------
ri_get_prod() {
#-----------------------------------------------------------------
# Select a product to install via CDROM or FTP.

    ri_get_product_list || return 1

    if [ "$prod_type" = All ]
    then
       one_type=one
    else
       one_type="one $prod_type"
    fi

    _select="Select $one_type product from the list below."
    _def="$last_prod"
    ri_get_var prod product `ri_get_prod_list`
    case "$prod" in
        rose* ) product=rose ;;
            * ) product=$prod ;;
    esac
    Prod=`echo $prod | ri_Sed`
    if [ -n "$Prod" ]
    then
        docs_avail=maybe
        case $prod in
            VADSself )
                prod_sel=vads
                rd=vl
                Platform=Platform
                platform=platform
                ;;
            VADScross|VADSworks )
                prod_sel=vads
                rd=vl
                Platform=Host/Target
                platform="HOST->TARGET combination"
                ;;
            apexlx|apexrt|apexvw )
                prod_sel=other
                rd=rd
                Platform=Host/Target
                platform="HOST->TARGET combination"
                ;;
            * )
                prod_sel=other
                rd=rd
                Platform=Platform
                platform=platform
                ;;
        esac
        return 0
    else
        return 1
    fi
}

#-----------------------------------------------------------------
ri_get_plat() {
#-----------------------------------------------------------------
# Select the product platform.

    ri_set_select "Select $one_type $platform for $Prod" "from the list below."
    _def="$last_plat"
    ri_get_var plat "$platform" `ri_get_plat_list`
    if [ $? -eq 99 ]
    then
        return 99
    fi
    Plat=`echo $plat | ri_Sed`
    if [ -n "$Plat" ]
    then
        return 0
    else
        return 1
    fi
}

#-----------------------------------------------------------------
ri_get_sizes() {
#-----------------------------------------------------------------

    set -- `grep "^$prod $plat $ver " $product_list`
    if [ $# -lt 3 ]
    then
        echo "No entry in product list matching '$prod $plat $ver'"
        return 1
    elif [ $# -lt 5 ]
    then
        echo "No FTP file sizes in product list for '$prod $plat $ver'"
        return 1
    fi
    shift 4

    ftp_sizes="$*"
    ftp_sizes_all="$*"

    unset total_bytes
    ri_set_total_bytes
    return 0
}

#-----------------------------------------------------------------
ri_get_ver() {
#-----------------------------------------------------------------
# Select the product version.

    unset Type
    ri_set_select "Select $one_type version of $Prod/$Plat" "from the list below."
    _def="$last_ver"
    ri_get_var ver "product version" `ri_get_ver_list`
    if [ $? -eq 99 ]
    then
        return 99
    fi
    if [ -z "$ver" ]
    then
        return 1
    fi

    if [ -z "$Type" ]
    then
        Type=`grep "^$prod $plat $ver " $product_list | awk '{print $4}'`
    fi

    if [ "$inst_type" = CDROM ]
    then
        return 0
    fi

    ri_get_sizes
    return $?
}

#-----------------------------------------------------------------
ri_get_ftp_retry() {
#-----------------------------------------------------------------
# Ask installer for the number of FTP retrys.

    echo
    echo "The number of retries can be set to a value from 1 to 10."
    echo "This is the number of times $CMD will try to get a file"
    echo "from the FTP server before giving up.  Between retries, it"
    echo "will wait between and 2 and 20 minutes, respectively."

    while true
    do
        ri_get_result "Enter the number of retries (1 to 10):" $ftp_retry
        case $result in
            [1-9]|10 )
                break
                ;;
            * )
                echo "Please enter a number from 1 to 10."
        esac
    done
    ftp_retry=$result
}

#-----------------------------------------------------------------
ri_get_transfer_mode() {
#-----------------------------------------------------------------
# Ask installer if they want to use batch mode.

    echo "
Transfer mode refers to how the files are downloaded from the FTP server.

Choice b - bulk:
    Get all of the product files before running the install program.
    With this method you will download all of the component files.
    This is required for making product tapes.  The $CMD program
    will retry any failed transfers on a file-by-file basis.

    You should use 'bulk' transfer if one of the following is true:
    - The machine you want to install on does not have FTP connectivity
    - Or, your FTP connection is not that reliable.
    - Or, you wish to make a product release tape.

Choice e - efficient:
    Get only the product files necessary to complete the install.  The
    install program will determine which files are needed and do the FTP
    transfers.  This information is passed back to $CMD to get only
    the files that are needed.  All files needed are transferred at one
    time and may be saved after the transfer for future installs.

    You should use 'efficient' transfer if:
    - You want the install program to get all of components required to do
      a 'tailored' install.  This eliminates transfers of unnecessary files.

Choice v - very_efficient: (Cannot be used with background mode or time delay)
    Similar to efficient transfer mode.  In this case, the install program
    gets the component files as it needs them and deletes each component
    file after it has been used.  This results in the minimum amount of disk
    space being used during the install process.

    You should use 'very_efficient' transfer if all of the following are true:
    - You want the install program to get each component file as it needs
      them.  This is the optimal disk saving way to install products.
    - And, you have FTP connectivity on the install machine.
" | more

    if [ -z "$def_mode"  -a -n "$transfer_mode" ]
    then
        def_mode=`echo $transfer_mode | cut -c1`
    fi

    while true
    do
        ri_get_result "Select the FTP transfer mode to use (b,e,v):" $def_mode
        case $result in
            b*|B* )
                transfer_mode=bulk
                def_mode=b
                return
                ;;
            e*|E* )
                transfer_mode=efficient
                def_mode=e
                return
                ;;
            v*|V* )
                if [ "$ftp_background_mode" = yes ]
                then
                    ri_set_background_mode
                fi

                if [ -n "$time_delay" ]
                then
                    echo "Unsetting time delay.  It cannot be used with this transfer mode."
                    unset time_delay show_time_delay
                fi

                transfer_mode=very_efficient
                def_mode=v
                return
                ;;
            * )
                echo "Please enter b for bulk, e for efficient or v for very_efficient."
        esac
    done
}

#-----------------------------------------------------------------
ri_select_dir() {
#-----------------------------------------------------------------
# Select one of 2 directories or input a new one.
#

    _mesg=$1
    _dir1=$2
    _dir2=$3
    echo "
$_mesg

      0:  Enter a directory of your own.
>>>>  1:  $_dir1
      2:  $_dir2
    "
    $ECHON "Enter 0, 1 or 2: [1]  $NOCR"
    read result
    case "$result" in
        0 ) ri_get_result "Enter a directory:" ;;
     ""|1 ) result=$_dir1 ;;
        2 ) result=$_dir2 ;;
    esac
}

#-----------------------------------------------------------------
ri_get_prod_dir() {
#-----------------------------------------------------------------
# Get/verify the current directory name--important for network access.

    if [ "$inst_type" = CDROM ]
    then
        echo "
Enter the CDROM directory that contains the Rational products.  It is
important to use a path name that is network accessible if you are not
going to complete the installation on this machine."
        Select="Select the CDROM"
    else
        echo "
Enter the directory to store the FTP files used for installing Rational
products.  It is important to use a path name that is network accessible
if you are not going to complete the installation on this machine."
        Select="Select an FTP file"
    fi

    while true
    do
        if [ -z "$prev_prod_dir"  -a  -z "$no_tmp_mnt" ]
        then
            ri_get_result "$inst_type storage directory:" $prod_dir
        else
            if [ -z "$prod_dir" -o  "$prod_dir" = "$prev_prod_dir" ]
            then
                ri_get_result "$inst_type storage directory:" $prev_prod_dir
            else
                ri_select_dir "$Select storage directory:" \
                    $no_tmp_mnt  $prod_dir  $prev_prod_dir
            fi
        fi

        dir=$result
        if [ -d $dir ]
        then
            if [ "$dir" != "$prod_dir" ]
            then
                # Save previous.
                prev_prod_dir=$prod_dir
                prod_dir=$dir
            fi
            break
        elif ri_mk_dir
        then
            prod_dir=$dir
            break
        fi
    done
    cd $prod_dir
    echo Now in $prod_dir
    log_file=$prod_dir/$CMD.log

    ri_get_last_product_info
}

#-----------------------------------------------------------------
ri_get_rational_dir() {
#-----------------------------------------------------------------
# Ask for the rational_dir directory.

    echo "
Please enter the location of the 'rational_dir' directory.  This is
where Rational products will be installed.  If this directory does
not exist, $CMD will ask if you want to create it.  This directory
contains the directories base, config, releases, and symbolic links
to products in the releases directory."

    if [ -z "$def_rational_dir" ]
    then
        # Try to get it from current directory.
        if [ "`basename $prod_dir`" = install ]
        then
            # It is save in this file:
            if [ -f ./.install_defaults ]
            then
                . ./.install_defaults
                def_rational_dir=$rational_dir
            else
                # It is up 3 levels.
                def_rational_dir=$prod_dir
                def_rational_dir=`dirname $def_rational_dir`
                def_rational_dir=`dirname $def_rational_dir`
                def_rational_dir=`dirname $def_rational_dir`
            fi
        else
            # It may be up one level.
            def_rational_dir=`dirname $prod_dir`
        fi
    fi

    rational_dir=${rational_dir:-$def_rational_dir}

    while true
    do
        if [ "$rational_dir" = "$def_rational_dir" ]
        then
            ri_get_result "'rational_dir' directory:" $rational_dir
        else
            ri_select_dir "Select a 'rational_dir' directory:" \
                $rational_dir $def_rational_dir
        fi

        dir=$result
        if [ -d $dir ]
        then
            break
        elif ri_mk_dir
        then
            break
        fi
    done

    if [ "$dir" != "$rational_dir" ]
    then
        def_rational_dir=$rational_dir
        rational_dir=$dir
    fi
}

#-----------------------------------------------------------------
ri_get_VADS_location() {
#-----------------------------------------------------------------
# Ask for the VADS_location directory.

    auto_select="$1"
    if [ -n "$Prod" ]
    then
        if [ -n "$plat" ]
        then
            if [ -n "$ver" ]
            then
                _ppv=`echo "$Prod.$plat.$ver" | sed -e "s?/?.?g"`
            else
                _ppv=`echo "$Prod.$plat" | sed -e "s?/?.?g"`
            fi
        else
            _ppv="$Prod"
        fi
    else
        _ppv=VADS
    fi
    if [ -n "$VADS_location" ]
    then
        def_prod_dir=`dirname $VADS_location`
        def_VADS_location=$def_prod_dir/$_ppv
    else
        def_VADS_location=$prod_dir/$_ppv
    fi

    VADS_location=${VADS_location:-$def_VADS_location}
    if [ "$auto_select" = auto  -a \
         "$VADS_location" = "$def_VADS_location" ]
    then
        if [ -d $VADS_location ]
        then
            return
        fi
    fi

    echo "
Please enter the 'VADS_location' directory.  This is where VADS will be
installed.  If this directory does not exist, $CMD will ask if you want
to create it."

    while true
    do
        if [ "$VADS_location" = "$def_VADS_location" ]
        then
            ri_get_result "'VADS_location' directory:" $VADS_location
        else
            ri_select_dir "Select a 'VADS_location' directory:" \
                $def_VADS_location $VADS_location
        fi

        dir=$result
        if [ -d $dir ]
        then
            break
        elif ri_mk_dir
        then
            break
        fi
    done

    if [ "$dir" != "$VADS_location" ]
    then
        def_VADS_location=$VADS_location
        VADS_location=$dir
    fi
}

#-----------------------------------------------------------------
ri_select_documents() {
#-----------------------------------------------------------------

    unset got_docs
    Documents_available="Documents available"
    while true
    do
        if [ ! -s $DOC_LIST ]
        then
            # No more--got all of them.
            no_documents="no more documents"
            ri_pause
            return
        fi

        set -- `cat $DOC_LIST | wc -l`
        ul=$1
        case $ul in
            1 ) range=1 ;;
            * ) range="1 to $ul" ;;
        esac

        dn=0
        echo
        echo "$Documents_available for $Prod:"
        echo
        cat $DOC_LIST | while read entry
        do
            set -- $entry
            shift `expr $# - 1`
            dn=`expr $dn + 1`
            if [ $dn -lt 10 ]
            then
                echo "  $dn )  $1"
            else
                echo "  $dn)  $1"
            fi
        done
        echo "  r )  return to the main menu
  q )  Quit $CMD

Select the number(s) corresponding to the document(s) to get, quit $CMD
or return to the main menu.  You may enter a range of numbers (e.g. 1-3)."

        ri_get_result "Enter $range, q or" r
        doc_n_list="$result"
        for result in $doc_n_list
        do
          case $result in
            r|R )
                return 1
                ;;
            q|Q )
                exit 0
                ;;
            * )
                ri_get_n_range
                for result in $n_range
                do
                    if ri_check_result 1 $ul
                    then
                        dn=$result
                        ri_get_document
                        if [ $? -ne 0 ]
                        then
                            return 1
                        fi
                    else
                        echo "'$result' out of range."
                        echo "You must enter $range, q or r."
                    fi
                done
                ;;
          esac
        done

        # Remove entry from temporary list.
        for document in $got_docs
        do
            grep -v " $document\$" $DOC_LIST > $DOC_LIST.2
            mv $DOC_LIST.2 $DOC_LIST
        done
        Documents_available="Remaining documents available"
    done
}

#-----------------------------------------------------------------
ri_get_document() {
#-----------------------------------------------------------------

    # Get the selected entry.
    set -- `head -$dn $DOC_LIST | tail -1`
    shift `expr $# - 1`
    document=$1
    if [ -z "$document" ]
    then
        return 1
    fi

    echo
    ri_logn "Getting $document $ftp_server..."
    echo "
        open $ftp_server
        user $ftp_user $ftp_pass
        $ftp_user_cmnds
        bin
        cd $ftp_doc_dir
        get $document
    " | ${ftp_command:-ftp} -n

    if [ -f "$document" ]
    then
        echo "Got it:"
        /bin/ls -l $document
        case $document in
            *.Z )
                echo Attempting to uncompress it...
                uncompress $document
                /bin/ls -l `basename $document .Z`
                ;;
            *.gz )
                echo Attempting to decompress it using gzip...
                gzip -d $document
                /bin/ls -l `basename $document .gz`
                ;;
        esac
        got_docs="$got_docs $document"
        return 0
    else
        echo "Failed to get it."
    fi
    ri_pause
    return 1
}

#-----------------------------------------------------------------
ri_show_documents() {
#-----------------------------------------------------------------

    if [ ! -f $DOC_LIST ]
    then
        no_documents="no documents"
        ri_logn "Getting $Prod document list from $ftp_server..."
        echo "
            open $ftp_server
            user $ftp_user $ftp_pass
            $ftp_user_cmnds
            cd $ftp_doc_dir
            ls *.ps*
        " | ${ftp_command:-ftp} -n | grep '\.ps' > $DOC_LIST

        if [ -f $DOC_LIST ]
        then
            echo "Got it."
        else
            echo "Failed to get it."
            docs_avail=no
            ri_pause
            return
        fi
    fi

    if [ ! -s $DOC_LIST ]
    then
        docs_avail=no
        echo
        echo "There are $no_documents available for $Prod."
        if [ "$no_documents" = "no more documents" ]
        then
            echo "You have already downloaded all of them."
        fi
        ri_pause
        return
    fi

    ri_select_documents
}

#-----------------------------------------------------------------
ri_banner() {
#-----------------------------------------------------------------

    Product=${1:-Product}
    $clear_screen
    xx=
    sp="                                                          "
    title=`echo "Rational $inst_type $Product Install $sp " | cut -c1-62`
    echo "$title $Revision: 1.75 $xx"
    echo "$delim"
}

#-----------------------------------------------------------------
ri_explain_log_activity() {
#-----------------------------------------------------------------

    if [ "$1" = show ]
    then
        (ri_banner)
        echo "
Information to be tracked by $CMD:

       FTP Install Info           Current Settings
       ---------------------      ----------------
   1.  Your E-mail address:       ${email:-Not_selected_yet}
   2.  When:                      Date and time FTP transfer completes.
   3.  Product:                   ${Prod:-Not_selected_yet}
         Version:                   ${ver:-Not_selected_yet}
         Platform:                  ${Plat:-Not_selected_yet}
   4.  FTP transfer mode:         ${transfer_mode:-Not_selected_yet}
   5.  Elapsed time:              Number of seconds to transfer files.
   6.  Bytes transferred:         Number of bytes transferred.
   7.  Transfer status:           PASS, FAIL or TERM
   8.  Background/foreground:     $bgm
   9.  Time delay:                ${time_delay:-none}
        "
        ri_pause
        return
    fi

    if [ "$inst_type" = CDROM  -o  "$explained_log" = yes ]
    then
        return
    fi

    (ri_banner)

    echo "                       U S A G E    A G R E E M E N T
$delim
Use of the $CMD command will result in some information being tracked
by Rational.  The following is a list of information that will be gathered
by $CMD and sent back to Rational via FTP:

    1. Your E-mail address.
    2. The date and time you completed the FTP transfer.
    3. The product, version and platform you selected to install.
    4. The FTP transfer mode you used.
    5. The elapsed time to do the transfer.
    6. The number of bytes transferred.
    7. The completion status (passed, terminated, failed).
    8. The $CMD program mode used (background or foreground).
    9. The time delay feature--used or not.

Rational reserves the right to use your E-mail address and product
information to keep you informed of known problems or new versions as they
become available.  This information will help us to better serve you.

You will be allowed to preview the information before starting the FTP
transfers.  Use the 'si' key to show information that will be tracked."

    ri_get_yn "Do you accept these terms and wish to continue with $CMD?" yes
    if [ $yn = no ]
    then
        exit 0
    fi

    ri_get_yn "Do you want to skip this message in the future?" no
    explained_log=$yn
    ri_save_defaults
}

#-----------------------------------------------------------------
ri_log_activity() {
#-----------------------------------------------------------------

    if [ -z "$start"  -o  "$inst_type" = CDROM ]
    then
        return
    fi

    status=$1
    if [ "$status" = TERM  -a -n "$file_in_transfer" ]
    then
        ri_check_size
        total_size=`expr ${total_size:-0} + $actual_size`
    fi

    ri_start_timer $start
    ri_elapse_time

    audit=$INSTALLER.`date +'%y:%m:%d:%H:%M'`.log
    when=`date +'%y/%m/%d-%H.%M'`
    tmplog=/tmp/$audit
    ftplog=/exchange/incoming/private/$CMD.logs/$audit
    ftp_mode=${def_mode:-`echo $transfer_mode | cut -c1`}
    echo "$prod:$ver:$plat:$email:$ftp_mode:$when:$sec:$total_size:$status:$bgm:$td" > $tmplog

    echo "
        open $ftp_server
        user $ftp_user $ftp_pass
        $ftp_user_cmnds
        put $tmplog $ftplog
    " | ${ftp_command:-ftp} -n

    /bin/rm -f $tmplog

    # Only call once.
    unset start
}

#-----------------------------------------------------------------
ri_set_time_delay() {
#-----------------------------------------------------------------
    if [ "$transfer_mode" = very_efficient ]
    then
        echo "
You cannot use time delay with 'very_efficient' transfer mode.
You can change FTP transfer mode ('t') and then set the time delay ('td')."
        ri_pause
        return
    fi

    echo "
You can set a time-delay to defer the start of the FTP transfers.
The time you specify is assumed to be within the next 24 hour period.

The time-delay format is HH:MM.  Examples: 08:00 (8am), 13:35 (1:35 pm)
"

    while true
    do
        ri_get_result "Enter a time-delay (HH:MM) or 0 to clear" ${time_delay:-0}
        case $result in
            0 )
                unset time_delay show_time_delay
                return
                ;;
            [0-9]:[0-9][0-9] )
                time_delay=0$result
                break
                ;;
            [0-9][0-9]:[0-9][0-9] )
                time_delay=$result
                break
                ;;
            * )
                echo
                echo "You must enter 0 or a number like `date +'%H:%M'."
        esac
    done
    show_time_delay="Start FTP transfers at $time_delay."
}

#-----------------------------------------------------------------
ri_do_time_delay() {
#-----------------------------------------------------------------

    if [ -z "$time_delay" ]
    then
        sleep_seconds=':'
        td=none
        return
    fi

    # Compute the number of seconds to delay the FTP transfer.
    set -- `date +'%H %M %S'`
    hr1=$1
    min1=$2
    sec1=$3
    set -- `echo $time_delay | sed -e "s?:? ?"`
    hr2=$1
    min2=$2
    td="$hr2 $min2"

    if [ $hr2 -ge $hr1 ]
    then
        hr3=`expr $hr2 - $hr1`
    else
        hr3=`expr $hr2 + 24 - $hr1`
    fi

    if [ $min2 -ge $min1 ]
    then
        min3=`expr $min2 - $min1`
    else
        if [ $hr3 -gt 0 ]
        then
            hr3=`expr $hr3 - 1`
        else
            hr3=23
        fi
        min3=`expr $min2 + 60 - $min1`
    fi

    delay_secs=`expr $hr3 \* 3600 + $min3 \* 60 - $sec1`
    ri_set_dhms $delay_secs
    echo "
The time is now $hr1:$min1:$sec1.
Sleeping $dhms ($delay_secs seconds)
Will wake at $time_delay to start FTP transfers."
    sleep_seconds="sleep $delay_secs"
}

#-----------------------------------------------------------------
ri_set_background_mode() {
#-----------------------------------------------------------------

    if [ "$inst_type" = CDROM ]
    then
        echo "You must enter one of: $keys"
        ri_pause
        return
    fi

    if [ "$ftp_background_mode" = yes ]
    then
        bg=bg
        bgm=fg
        ftp_background_mode=no
        chg_mode="$chg_bg_mode"
        run_mode="$run_fg_mode"
        return
    elif [ "$transfer_mode" = very_efficient ]
    then
        echo "
You cannot use background mode with 'very_efficient' transfer mode.
You can change FTP transfer mode ('t') and then set background mode ('bg')."
    else
        bg=fg
        bgm=bg
        ftp_background_mode=yes
        chg_mode="$chg_fg_mode"
        run_mode="$run_bg_mode"
        echo "
Will run $CMD in background during FTP transfers.
You can logout after starting the FTP transfers.
Check status in the log file: $log_file"
    fi
    ri_pause
}

#-----------------------------------------------------------------
ri_enter_background() {
#-----------------------------------------------------------------
# If background mode set, change the output to the log file.
# This can be done in 2 places: right after leaving the menu (bulk mode)
# or after running the install program (efficient mode).

    if [ "$ftp_background_mode" = no ]
    then
        bg_exit=':'
        return
    fi

    README=$prod_dir/README
    /bin/rm -f $log_file

    echo "
All output going to log file: $log_file
Instructions for what to do next will be put in the README file.
You may suspend this program, put it in the background (bg) and logout."

    exec 1> $log_file 2>&1
    bg_exit='exit 0'
}

#-----------------------------------------------------------------
ri_set_def_key() {
#-----------------------------------------------------------------
# Check the required settings and set the default key and prod_ver_plat.
# Arg1: either 'echo' to display message or ':' to skip message.

    _echo=$1
    unset def_key okay
    rd=${rd:-rd}
    if [ "$inst_type" = CDROM ]
    then
        keys="d,${rd},sp,q"
        go_keys=go
    else
        if [ "$prod_sel" = vads ]
        then
            keys="f,u,p,uf,t,d,n,tv,${rd},sp,$bg,td,r,si,q"
        else
            keys="f,u,p,uf,t,d,n,${rd},sp,$bg,td,r,si,q"
        fi
        go_keys="go,st"

        if [ -z "$prod_dir"  -o  ! -w "$prod_dir" ]
        then
            if [ -n "$this_dir"  -a  -w "$this_dir" ]
            then
                prod_dir=$this_dir
                cd $this_dir
            else
                if [ -n "$prev_prod_dir"  -a  -d "$prev_prod_dir" ]
                then
                    prod_dir=$prev_prod_dir
                    cd $prod_dir
                else
                    unset prod_dir
                    def_key=${def_key:-d}
                fi
            fi
        fi
    fi

    case "$prod_dir" in
        /tmp_mnt/* )
            no_tmp_mnt=`echo $prod_dir | sed -e "s?^/tmp_mnt??"`
            def_key=${def_key:-d}
            ;;
        * )
            no_tmp_mnt=
            ;;
    esac

    if [ "$inst_type" = FTP  -a  -z "$ftp_pass" ]
    then
        $_echo "You need to specify the FTP password (Key: p)."
        def_key=${def_key:-p}
        okay=no
    fi

    if [ "$prod_sel" = vads ]
    then
        if [ -n "$VADS_location" -a  -d "$VADS_location" ]
        then
            Rproduct_directory="VADS_location:             $VADS_location"
        else
            Rproduct_directory="VADS_location:             NEED-TO-SPECIFY"
            $_echo "You need to specify the VADS location. (Key: vl)."
            def_key=${def_key:-vl}
            okay=no
        fi
    else
        if [ "$inst_type" = FTP  -a  -z "$transfer_mode" ]
        then
            $_echo "You need to specify the FTP transfer mode. (Key: t)."
            def_key=${def_key:-t}
            okay=no
        fi

        if [ -n "$rational_dir" -a  -d "$rational_dir" ]
        then
            Rproduct_directory="Rational directory:        $rational_dir"
        else
            Rproduct_directory="Rational directory:        NEED-TO-SPECIFY"
            $_echo "You need to specify the Rational directory. (Key: rd)."
            def_key=${def_key:-rd}
            okay=no
        fi
    fi

    if [ -z "$Prod" ]
    then
        select_product="Select a product to install"
        $_echo "You need to specify the Rational product to install. (Key: sp)."
        if [ -z "$def_key"  -o  "$def_key" = vl ]
        then
            def_key=sp
        fi
        okay=no
    else
        Prod_ver="$Prod $ver"
        prod_ver_plat="$Prod $ver for $Plat"
        if [ `echo "$prod_ver_plat" | wc -c` -gt 45 ]
        then
           _pvp="$Prod $ver
                                  for $Plat"
        else
            _pvp="$prod_ver_plat"
        fi
        if [ -n "$total_Kbytes" ]
        then
            select_product="Selected product:          $_pvp
                                  Maximum Kbytes to download: $total_Kbytes KB"
        else
            select_product="Selected product:          $_pvp"
        fi

        if [ "$inst_type" = FTP  -a "$docs_avail" = maybe ]
        then
            select_product="$select_product
  sd)  show available documents"
            if [ "$prod_sel" = vads ]
            then
                def_install_dir=$VADS_location
                keys="f,u,p,uf,t,d,n,tv,${rd},sp,sd,$bg,td,r,si,q"
            else
                def_install_dir=$rational_dir/releases/$prod.$ver/install
                keys="f,u,p,uf,t,d,n,${rd},sp,sd,$bg,td,r,si,q"
            fi
        fi
    fi

    if [ -z "$okay" ]
    then
        keys="$keys,$go_keys"
        okay=yes
        if [ "$ftp_background_mode" = yes ]
        then
            def_key=${def_key:-st}
        else
            def_key=${def_key:-go}
        fi

        if [ -n "$NP_list" ]
        then
            case $def_key in
            go|st )
                if [ -d $def_install_dir ]
                then
                    def_key=ip
                    defer_install=no
                else
                    defer_install=yes
                fi
                keys="$keys,ip"
                ;;
            esac
        fi
    fi
}

#-----------------------------------------------------------------
ri_get_prod_desc() {
#-----------------------------------------------------------------

    if [ -n "$cmps" ]
    then
        return
    fi

    mkdir $TMP_DIR
    cd $TMP_DIR
    prod_desc=`tar -tf $install_from/file0 | grep prod_desc | head -1`
    tar -xf $install_from/file0 $prod_desc
    . $prod_desc
    cd $prod_dir
    release=${product_pver:-$product.$product_version}
    install_dir=$rational_dir/releases/$release/install
    if [ "$vendor" = Rational ]
    then
        vendor="Rational Software Corporation"
    fi
    echo
    echo "Rational's $inst_type install for:"
    echo "    Product:  $Prod, $Plat"
    echo "    Version:  $ver"
    echo "    Date:     $product_date"
    echo "    Copyright(c) $copyright_year by $vendor"
    echo

    if [ -z "$cmps" ]
    then
        cmps=`expr ${required_cmps:-0} + \
                   ${recommend_cmps:-0} + \
                   ${optional_cmps:-0} + \
                   ${auxiliary_cmps:-0}`
    fi
}

#-----------------------------------------------------------------
ri_get_gzip() {
#-----------------------------------------------------------------

    if gzip --help >/dev/null 2>&1
    then
        return 0
    fi

    echo "
The gzip command is not on your search path.
Will get it from $ftp_server and put in
$prod_dir.  Then,
this ^^^ directory will be added to your search path.
"
    public_gzip=$PUB_TOOLS/gzip
    case "${sys}$rel" in
        SunOS4* ) public_gzip=$public_gzip/sun ;;
        SunOS5* ) public_gzip=$public_gzip/sol ;;
          OSF1* ) public_gzip=$public_gzip/axp ;;
           AIX* ) public_gzip=$public_gzip/aix ;;
         HP-UX* ) public_gzip=$public_gzip/hp  ;;
          IRIX* ) public_gzip=$public_gzip/sgi ;;
    esac
    public_gzip=$public_gzip/$GZIPZ
    ri_get_file_and_check $public_gzip "FAIL. $GZIPZ"

    dd if=$GZIPZ 2>/dev/null | uncompress | tar -xvf -
    if [ -x ./gzip ]
    then
        echo "Done; gzip tools installed."
        echo "$GZIPZ no longer needed."
        /bin/rm -i $GZIPZ
        PATH=$prod_dir:$PATH
        export PATH
        return 0
    else
        echo "Could not unpack $GZIPZ."
        return 1
    fi
}

#-----------------------------------------------------------------
ri_list_vads_products() {
#-----------------------------------------------------------------

    prod_ver_plat="$Prod $ver for $Plat"
    echo "
                  Size in  $prod_ver_plat:
  No.  Install?     Bytes  Products available to install
  ---  --------  --------  -----------------------------"

    # Set arg list format: <subprod size>... pairs.
    set -- $ftp_sizes_all

    unset vads_cmps vads_sizes
    total_bytes=0
    dn=0
    while [ $# -gt 0 ]
    do
        subprod=$1
        size=$2
        shift 2
        dn=`expr $dn + 1`
        eval size$dn=$size
        eval inst=\$inst$dn
        if [ -z "$inst" ]
        then
            if [ "$subprod" = vads ]
            then
                inst=yes
            elif [ "$transfer_mode" = bulk ]
            then
                inst=yes
            else
                inst=no
            fi
            eval inst$dn=$inst
        fi

        if [ "$dn" -lt 10 ]
        then
            Dn=" $dn"
        else
            Dn="$dn"
        fi

        if [ "$inst" = yes ]
        then
            Install="yes     "
            total_bytes=`expr $total_bytes + $size`
            vads_cmps="$vads_cmps $subprod"
            vads_sizes="$vads_sizes $size"
        else
            Install="no      "
        fi

        if [ $size -lt 100 ]
        then
            Size="      $size"
        elif [ $size -lt 1000 ]
        then
            Size="     $size"
        elif [ $size -lt 10000 ]
        then
            Size="    $size"
        elif [ $size -lt 100000 ]
        then
            Size="   $size"
        elif [ $size -lt 1000000 ]
        then
            Size="  $size"
        elif [ $size -lt 10000000 ]
        then
            Size=" $size"
        else
            Size="$size"
        fi
        echo "  $Dn)  $Install  $Size  $subprod " | ri_Sed
    done
    total_Kbytes=`expr $total_bytes / 1024 + 1`
    size_set=yes

    ul=$dn
    vn=$dn
    if [ $ul -gt 1 ]
    then
        range="1-$ul"
    else
        range="1"
    fi

    echo "
   f)  select only the first product in the list to install
   a)  select all to install
   r)  return to the main menu
   q)  Quit $CMD

Select or deselect the number(s) corresponding to the product(s) you
wish to install.  You may enter a range of numbers (e.g. 1-3)."
}

#-----------------------------------------------------------------
ri_tailor_vads_install() {
#-----------------------------------------------------------------

    if [ "$prod_sel" != vads ]
    then
        return
    fi

    if [ -z "$prod" ]
    then
        echo "You must select a VADS product before you can tailor the install."
        ri_pause
        return
    fi

    (ri_banner "- Tailor VADS")

    if [ "$1" = show_warn ]
    then
        echo "
Select a product number to select or deselect a product or product component
to download and install.  Each of the items in the list below requires one or
more license keys.  Make sure you have purchased them before downloading,
otherwise you end up consuming more disk space than necessary."

        ri_pause
    fi

    while true
    do
        ri_list_vads_products
        ri_get_result "Enter $range,f,a,q or" r
        for result in $result
        do
          case $result in
            a|A )
                size_set=no
                dn=1
                while [ $dn -le $ul ]
                do
                    eval inst$dn=yes
                    dn=`expr $dn + 1`
                done
                ;;
            f|F )
                size_set=no
                eval inst1=yes
                dn=2
                while [ $dn -le $ul ]
                do
                    eval inst$dn=no
                    dn=`expr $dn + 1`
                done
                ;;
            r|R )
                if [ "$size_set" = no ]
                then
                    # Do this to make sure size is set.
                    ri_list_vads_products >/dev/null
                fi
                return 0
                ;;
            q|Q )
                exit 0
                ;;
            * )
                ri_get_n_range
                for result in $n_range
                do
                    if ri_check_result 1 $ul
                    then
                        size_set=no
                        eval inst=\$inst$result
                        if [ "$inst" = yes ]
                        then
                            eval inst$result=no
                        else
                            eval inst$result=yes
                        fi
                    else
                        echo "'$result' out of range."
                        echo "You must enter $range, q or r."
                        ri_pause
                    fi
                done
                ;;
          esac
        done
        (ri_banner "- Tailor VADS")
    done
}

#-----------------------------------------------------------------
ri_check_tar_pd_errors() {
#-----------------------------------------------------------------
# Check for any tar 'permission denied' errors.

    if [ ! -s tar_err ]
    then
        return
    fi

    grep -i "permission denied" tar_err | sed -e "s?: ? ?g" > tar_pd_errors
    if [ -s tar_pd_errors ]
    then
        # Recognized tar error formats:
        #  tar: can't create t2: Permission denied
        #  tar: t2 - cannot create
        #  tar: t2 - cannot create -- Permission denied
        # So, the file will be the second or fourth item on the line:
        cat tar_pd_errors | while read p1 f2 p3 f4 other
        do
            if [ -f "$f2" ]
            then
                /bin/rm -f $f2
                echo $f2
            elif [ -f "$f4" ]
            then
                /bin/rm -f $f4
                echo $f4
            fi
        done > tar_pd_files 2>/dev/null

        if [ -s tar_pd_files ]
        then
            echo tar -xvf - `cat tar_pd_files`
            # Now do the tar command again with just the files that
            # were found in the tar_pd_errors file.
            gzip -dc $tar_gz_file 2>gz_err |
            tar -xpf - `cat tar_pd_files` 2>tar_err
        fi
    fi
    /bin/rm -f tar_pd_errors tar_pd_files
}

#-----------------------------------------------------------------
ri_complete_vads_install() {
#-----------------------------------------------------------------

    ftp_sizes="$vads_sizes"
    ftp_sizes_all="$vads_sizes"

    if [ "$transfer_mode" != very_efficient ]
    then
        echo
        for subprod in $vads_cmps
        do
            ri_get_file_and_check $ftp_src_dir/$subprod.tar.gz \
                "FAIL. $subprod.tar.gz"
        done
        echo
        echo "Download complete."
        echo "Compressed tar archives stored in: $prod_dir"
    fi

    # Get the patches, if any.
    ri_install_patch

    if [ "$defer_install" = yes ]
    then
        ri_log_activity PASS
        set -- `echo $vads_cmps | wc -w`
        ntapes=$1
        if [ $ntapes -eq 1 ]
        then
            tape=tape
            echo "
The $CMD command can now make product a tape
of $Prod, $Plat.
Will run this command to make the product $tape:

    % gzip -dc $vads_cmps.tar.gz | dd obs=20b of=TAPE_DEVICE
"
        else
            tape=tapes
            echo "
The $CMD command can now make the $ntapes product tapes
(VADS plus each add-on) of $Prod, $Plat.
Will run this command for each tar.gz file to make the $ntapes product tapes:

    % gzip -dc TAR.GZ.FILE | dd obs=20b of=TAPE_DEVICE
"
        fi

        $bg_exit

        ri_get_yn "Do you want $CMD to make the $tape now?" yes
        if [ $yn = yes ]
        then
            ri_get_result "Enter the TAPE_DEVICE: "
            tape_device=$result
            ri_get_gzip
            if [ $? -ne 0 ]
            then
                exit 0
            fi

            for subprod in $vads_cmps END
            do
                if [ "$subprod" = END ]
                then
                    break
                fi

                tar_gz_file=$subprod.tar.gz
                echo
                echo "Will write $subprod  to tape." | ri_Sed
                echo Put a new tape in $tape_device.
                ri_pause

                while true
                do
                    echo "gzip -dc $tar_gz_file | dd obs=20b of=$tape_device"
                    if gzip -dc $tar_gz_file | dd obs=20b of=$tape_device
                    then
                        break
                    fi
                    ri_get_result "r) retry, a) abort or c) continue?"
                    case $result
                    in
                        a*|A* ) exit 0 ;;
                        c*|C* ) break  ;;
                    esac
                done
            done
            if [ -n "$patches" ]
            then
                echo "Will write patches to tape."
                echo Put a new tape in $tape_device.
                ri_pause
                echo tar -cvf $tape_device $patches
                tar -cvf $tape_device $patches
            fi
        fi
        exit 0
    fi

    ri_get_gzip
    if [ $? -ne 0 ]
    then
        echo "
To complete the install, get gzip from $ftp_server in
$public_gzip.  Then gunzip these files and
untar them into $VADS_location:
"
        /bin/ls -CF *.tar.gz
        exit 1
    fi

echo "
Now installing $prod_ver_plat
in ${VADS_location}..."

    cd $VADS_location
    for subprod in $vads_cmps
    do
        tar_gz_file=$prod_dir/$subprod.tar.gz
        Installing=`echo "Installing $subprod ..." | ri_Sed`
        if [ -f $tar_gz_file ]
        then
            $ECHON "   ${Installing}$NOCR"
        else
            cd $prod_dir
            ri_get_file_and_check $ftp_src_dir/$subprod.tar.gz \
                "FAIL. $subprod.tar.gz"
            cd $VADS_location
            ri_logn "$Installing"
        fi

        if gzip -dc $tar_gz_file 2>gz_err | tar -xpf - 2>tar_err
        then
            echo done.
        else
            echo
            # Ignore any tar 'permission denied' errors.
            grep -iv "permission denied" tar_err > tar_err_nopd

            if [ -s gz_err ]
            then
                ri_log err "Failed VADS install:"
                ri_log cont `cat gz_err`
                ri_log_activity "FAIL. Install/gzip of $subprod.tar.gz"
                /bin/rm -f gz_err tar_err tar_err_nopd
                exit 1
            fi

            if [ -s tar_err_nopd ]
            then
                ri_log err "Failed VADS install:"
                ri_log cont `cat tar_err_nopd`
                ri_log_activity "FAIL. Install/tar of $subprod.tar.gz"
                /bin/rm -f gz_err tar_err tar_err_nopd
                exit 1
            fi
        fi
        ri_check_tar_pd_errors

        if [ "$transfer_mode" = very_efficient ]
        then
            /bin/rm -f $tar_gz_file
        fi
    done
    /bin/rm -f gz_err tar_err

    ri_log_activity PASS

    if [ "$transfer_mode" != very_efficient ]
    then
        echo
        echo "$prod_ver_plat loaded from $ftp_server."
        echo "The tar archives are no longer needed:"
        (
        cd $prod_dir
        /bin/ls -C *.tar.gz
        ri_get_yn "Would you like to have these files removed now?"
        if [ $yn = yes ]
        then
            $ECHON "Removing tar archives...$NOCR"
            /bin/rm -f *.tar.gz
            echo done.
        else
            echo "Okay, these files have been spared in $prod_dir."
        fi
        )
    fi

    for NP in $NP_list
    do
        eval patch_program=\$patch_program$NP
        if [ -f $install_from/$patch_program ]
        then
            $install_from/$patch_program -commit
        fi
    done

    echo
    echo "Please read the installation guide to complete the installation."
    exit 0
}

#-----------------------------------------------------------------
ri_set_doing() {
#-----------------------------------------------------------------

    case "$doing" in
        prod )
            doing=END
            ;;
        plat )
            doing=prod
            ;;
        ver )
            if [ "$only_one_plat" = yes ]
            then
                doing=prod
            else
                doing=plat
            fi
            ;;
    esac
}

#-----------------------------------------------------------------
ri_get_Product() {
#-----------------------------------------------------------------

    if [ "$only_one_prod_plat_ver" = yes ]
    then
        echo "There is only one product to select."
        echo "The product to install is $prod_ver_plat."
        ri_pause
        return
    fi

    doing=prod
    while true
    do
        if [ "$doing" = END ]
        then
            break
        fi

        case "$doing" in
            prod ) next=plat ; ri_get_prod ;;
            plat ) next=ver  ; ri_get_plat ;;
            ver  ) next=END  ; ri_get_ver  ;;
        esac
        case $? in
            99 ) ri_set_doing ;;
             0 ) doing=$next  ;;
             * ) ri_pause ; return 1 ;;
        esac
    done

    ri_check_prod_dir
    if [ "$inst_type" = FTP ]
    then
        ri_set_ftp_dirs
        ri_get_prod_readme
    else
        ri_show_prod_readme
    fi
    ri_tailor_vads_install show_warn
}

#========================================================================
# Main
#========================================================================

while [ $# -gt 0 ]
do
    case $1 in
    -p )
        # For CD-ROM install simulation. Used by cdinstall.
        # usage: $CMD -p prod plat ver
        shift
        prod=$1
        Prod=`echo $prod | ri_Sed`
        shift
        plat=$1
        Plat=`echo $plat | ri_Sed`
        shift
        ver=$1
        run_via_program=yes
        ;;
    -pl )
        RM_PRODUCT_LIST=':'
        ;;
    -alpha )
        prod_type=Alpha
        ;;
    -beta )
        prod_type=Beta
        ;;
    -early_access )
        prod_type=Early_Access
        ;;
    -private )
        prod_type=Private
        ;;
    -special )
        prod_type=Special
        ;;
    -passwd|-key )
        case $2 in
        K*X )
            prod_type=$2
            ;;
        * )
            echo "Passwd key '$2' is not valid."
            exit 1
            ;;
        esac
        ;;
    -nocl*|-no_cl* ) # -no_clear
        clear_screen=':'
        ;;
    auto_select_product )
        auto_select_product=yes
        ;;
    -* )
        echo "$CMD: option '$1' not recognized."
        exit 1
        ;;
    esac
    shift
done


trap '/bin/rm -fr $DOC_LIST $TMP_DIR $CD_LIST; exit' 0

trap 'ri_log_activity TERM
      /bin/rm -fr $DOC_LIST $TMP_DIR $CD_LIST
      exit' 2 3 4 15

trap 'echo "----------------------------"
      echo "User logout detected"
      date
      echo "----------------------------"' 1

ri_set_defaults


#-----------------------------------------------------------------
# Get the product list file.
#-----------------------------------------------------------------

if [ "$auto_select_product" = yes ]
then
    ri_get_Product
    ri_save_defaults
    ri_get_patch_info
else
    ri_explain_log_activity exp
fi

while true
do
    ri_set_def_key :

    (ri_banner)

    echo
    echo "Select a 'Key' to set or change the install options."

    if [ "$inst_type" = CDROM ]
    then
        echo "
  Key  CDROM Install Options      Current Settings
  ---  ---------------------      ----------------
  d )  CDROM storage:             $prod_dir
  $rd)  $Rproduct_directory
  sp)  $select_product
  q )  Quit $CMD"

    else

  echo
  echo "  Key  FTP Install Options        Current Settings"
  echo "  ---  ---------------------      ----------------"
  echo "  f )  FTP command:               $ftp_command $ftp_server"
  echo "  u )  FTP user:                  ${ftp_user:-anonymous}"
  echo "  p )  FTP user password:         ${ftp_pass:-NEED-TO-SPECIFY}"
    if [ -n "$ftp_pass"  -a  "$ftp_pass" != "$email" ]
    then
  echo "       Your E-mail address:       $email"
    fi
  echo "  uf)  User FTP command:          ${ftp_user_cmnd:-none}"
  echo "  t )  FTP transfer mode:         ${transfer_mode:-NEED-TO-SPECIFY}"
  echo "  d )  FTP storage directory:     ${prod_dir:-NEED-TO-SPECIFY}"
  echo "  n )  Number of retries:         $ftp_retry"
    if [ "$prod_sel" = vads  -a  -n "$prod" ]
    then
  echo "  tv)  Tailor VADS install"
    fi
  echo "  $rd)  $Rproduct_directory"
  echo "  sp)  $select_product"
  echo "  $bg)  $chg_mode $run_mode"
  echo "  td)  Time delay:                ${show_time_delay:-none}"
  echo "  r )  Reset to factory defaults"
  echo "  si)  Show tracking information"
  echo "  q )  Quit $CMD"
    fi

    if [ "$def_key" = go  -o  "$def_key" = st  -o  "$def_key" = ip ]
    then
      if [ -n "$time_delay" ]
      then
  echo "  go)  go-delayed, start $inst_type install at $time_delay."
        st_option="start-delayed FTP transfer at $time_delay"
      else
  echo "  go)  go, start $inst_type install now."
        st_option="start FTP transfer now"
      fi

      if [ "$inst_type" = FTP  -a  "$transfer_mode" != very_efficient ]
      then
  echo "  st)  $st_option, install later."
      fi

      if [ -n "$NP_list" ]
      then
  echo "  ip)  Install patches only."
  echo "       Do this if you have already installed the selected product."
      fi
    fi

    ri_get_result "Enter Key ($keys):" $def_key

    case $result in
        f|F )
            if [ "$inst_type" = FTP ]
            then
                ri_get_ftp_command
                ri_save_defaults
            else
                echo "You must enter one of: $keys"
                ri_pause
            fi
            ;;
        u|U )
            if [ "$inst_type" = FTP ]
            then
                ri_get_ftp_user
                ri_save_defaults
            else
                echo "You must enter one of: $keys"
                ri_pause
            fi
            ;;
        p|P )
            if [ "$inst_type" = FTP ]
            then
                ri_get_ftp_pass
                ri_save_defaults
            else
                echo "You must enter one of: $keys"
                ri_pause
            fi
            ;;
        uf|UF )
            if [ "$inst_type" = FTP ]
            then
                ri_get_ftp_user_cmnd
                ri_save_defaults
            else
                echo "You must enter one of: $keys"
                ri_pause
            fi
            ;;
        d|D )
            ri_get_prod_dir
            ri_save_defaults
            ri_pause
            ;;
        t|T )
            if [ "$inst_type" = FTP ]
            then
                ri_get_transfer_mode
                ri_save_defaults
            else
                echo "You must enter one of: $keys"
                ri_pause
            fi
            ;;
        tv|TV )
            if [ "$prod_sel" = vads ]
            then
                ri_tailor_vads_install
            else
                echo "You must enter one of: $keys"
                ri_pause
            fi
            ;;
        td|TD )
            ri_set_time_delay
            ;;
        rd|RD )
            if [ "$prod_sel" = vads ]
            then
                echo "You must enter one of: $keys"
                ri_pause
            else
                ri_get_rational_dir
                ri_save_defaults
            fi
            ;;
        vl|VL )
            if [ "$prod_sel" = vads ]
            then
                ri_get_VADS_location
                ri_save_defaults
            else
                echo "You must enter one of: $keys"
                ri_pause
            fi
            ;;
        si|SI )
            if [ "$inst_type" = FTP ]
            then
                ri_explain_log_activity show
            else
                echo "You must enter one of: $keys"
                ri_pause
            fi
            ;;
        sp|SP )
            ri_get_Product
            ri_save_defaults
            ri_get_patch_info
            ;;
        sd|SD )
            if [ -n "$Prod" ]
            then
                ri_show_documents
            else
                echo "You must select a product first."
                ri_pause
            fi
            ;;
        [bBfF][gG] )
            ri_set_background_mode
            ;;
        n|N )
            if [ "$inst_type" = FTP ]
            then
                ri_get_ftp_retry
                ri_save_defaults
            else
                echo "You must enter one of: $keys"
                ri_pause
            fi
            ;;
        go|GO )
            if [ "$okay" = yes ]
            then
                defer_install=no
                break
            fi
            ri_set_def_key echo
            ri_pause
            ;;
        st|ST )
            if [ "$okay" = yes ]
            then
                defer_install=yes
                break
            fi
            ri_set_def_key echo
            ri_pause
            ;;
        ip|IP )
            if [ "$okay" = yes ]
            then
                install_only_patch=yes
                break
            fi
            ri_set_def_key echo
            ri_pause
            ;;
        r|R )
            ri_get_yn "Change the current settings back to the factory defaults?" no
            if [ $yn = yes ]
            then
                /bin/rm -f $product_list
                ftp_background_mode=no
                ftp_command=ftp
                ftp_user=anonymous
                ftp_server=$rftp_server
                ftp_retry=5
                explained_log=no
                chg_mode="$chg_bg_mode"
                run_mode="$run_fg_mode"
                bg=bg
                bgm=fg
                unset ftp_pass email transfer_mode rational_dir Prod
                unset ftp_user_cmnd ftp_user_cmnds
                unset time_delay show_time_delay
                ri_save_defaults
                ri_explain_log_activity exp
            fi
            ;;
        q|Q )
            exit 0
            ;;
        pr|pt|PR|PT )
            ri_select_prod_type
            ;;
        * )
            echo "You must enter one of: $keys"
            ri_pause
            ;;
    esac
done
echo

#-----------------------------------------------------------------
# Set the name of the directory to install from.
#-----------------------------------------------------------------
if [ "$inst_type" = CDROM ]
then
    install_from=$prod_dir/$prod/$plat/$ver
    bg_exit=':'
else
    install_from=$prod_dir
    ri_check_disk_space defer
    if [ "$transfer_mode" = bulk ]
    then
        ri_do_time_delay
        ri_enter_background
        $sleep_seconds
    else
        td=none
        bg_exit=':'
    fi
    ri_start_timer now
    start="$d1 $h1 $m1 $s1"
fi

#-----------------------------------------------------------------
# Get file0 which contains product installation tools and info.
#-----------------------------------------------------------------
if [ "$inst_type" = FTP ]
then
    if [ "$install_only_patch" = yes ]
    then
        /bin/rm -f post_patch post_patch_added
        ri_install_patch
        exit 0
    fi

    if [ "$prod_sel" = vads ]
    then
        ri_complete_vads_install
        # Does not return
    fi

    ri_get_file_and_check $ftp_src_dir/file0  FAIL.0
fi

#-----------------------------------------------------------------
# Read product description file from file0.
#-----------------------------------------------------------------
ri_get_prod_desc

#-----------------------------------------------------------------
# If FTP transfer mode is bulk, get the rest of the files from FTP server.
#-----------------------------------------------------------------
if [ "$inst_type" = FTP  -a  "$transfer_mode" = bulk ]
then
    echo
    ri_log note "There are $cmps tar archives to install $Prod_ver."
    i=1
    while [ $i -le $cmps ]
    do
        ri_get_file_and_check $ftp_src_dir/file$i  FAIL.$i
        i=`expr $i + 1`
    done

    # Get the patches, if any.
    /bin/rm -f post_patch post_patch_added
    ri_install_patch

    ri_ftp_get_file $SCRIPTS/network_install
    chmod 775 network_install 2>/dev/null

    ri_ftp_get_file $SCRIPTS/readtape
    chmod 775 readtape 2>/dev/null
    ./readtape -i | grep -v "Nothing to install"

    if [ "$ftp_background_mode" = yes ]
    then
        exec 1> $README 2>&1
    fi

    ri_log_activity PASS

    if [ "$defer_install" = yes ]
    then
        echo "
You can now make a tape of $Prod, $Plat.
There are 2 ways to do this.  You can put all of the files
beginning with 'file' on a tape along with the network_install program
or you can make a product tape using the duptape program.

To make a product tape:              $no_patches

    # $Recommend
    % ./duptape

Or, to make a network install tape:              $yes_patches

    # $Alternate
    % tar -cvf TAPE_DEVICE network_install $patches file*
"
        $bg_exit

        ri_get_yn "Do you want $CMD to run duptape now?" ${do_duptape:-yes}
        if [ $yn = yes ]
        then
            echo ./duptape
            ./duptape
            exit 0
        fi

        files=`(/bin/ls file?; /bin/ls file??) 2>/dev/null`
        ri_get_yn "Do you want $CMD to do the tar command now?" yes
        if [ $yn = yes ]
        then
            ri_get_result "Enter the TAPE_DEVICE: "
            tape_device=$result
            echo tar -cvf $tape_device network_install $patches $files
            tar -cvf $tape_device network_install $patches $files
        fi
        exit 0
    fi
fi

#-----------------------------------------------------------------
# Unpack file0.  This will create the product release & install
# directory under the rational_dir directory.
#-----------------------------------------------------------------
echo
echo Loading install environment for $release
$ECHON "in $install_dir...$NOCR"
(cd $rational_dir; tar -xf $install_from/file0)
echo done.

if [ ! -d $install_dir ]
then
    ri_log err  "$CMD: The unpacking of file0 failed."
    ri_log cont "    The directory: $install_dir"
    ri_log cont "    did not get created."
    ri_log_activity FAIL1
    exit 1
fi

if [ -f post_patch ]
then
    cat post_patch >> $install_dir/post_patch
    touch post_patch_added
fi

case "$plat" in
    axp* ) ARCH=alpha_osf1 ;;
    aix* ) ARCH=rs6k ;;
     hp* ) ARCH=hppa_hpux ;;
    sgi* ) ARCH=mips_irix5 ;;
    sol* ) ARCH=sun4_solaris2 ;;
    sun* ) ARCH=sun4 ;;
       * ) ARCH=share ;;
esac

disk_install_dir=$install_dir/share/disk_install_dir

/bin/rm -f $disk_install_dir
if [ "$inst_type" = CDROM ]
then
    echo "
disk_install_dir='$install_from'
Dev_Kind='CDROM'
device_kind='network'
rprod_name='$Prod'  # use to match subprod_name
    " > $disk_install_dir

elif [ "$transfer_mode" = bulk ]
then
    echo "
disk_install_dir='$install_from'
device_kind='network'
rprod_name='$Prod'  # use to match subprod_name
    " > $disk_install_dir

else
    echo "
disk_install_dir='$install_from'
device_kind='network'
ftp_sizes_all='$ftp_sizes_all'
ftp_command='$ftp_command'
ftp_user='$ftp_user'
ftp_pass='$ftp_pass'
ftp_server='$ftp_server'
ftp_retry='$ftp_retry'
lprod_list_name='$lprod_list_name'
rprod_name='$Prod'  # use to match subprod_name
transfer_mode='$transfer_mode'
    " > $disk_install_dir

fi

#-----------------------------------------------------------------
# If FTP transfer mode is efficient, run install to compute the files
# needed to do the install.
#-----------------------------------------------------------------
if [ "$transfer_mode" = efficient ]
then
    /bin/rm -f $defaults.cmp_info
    # This runs the install program and tells it not to finish the install,
    # but to return cmp_list and cmps in the cmp_info file.
    echo
    echo "$CMD: cd $install_dir"
    echo "$CMD: ./install"
    (cd $install_dir; ./install -ftp $CMD $defaults.cmp_info)
    if [ ! -f $defaults.cmp_info ]
    then
        ri_log err  "The install program did not compute the list of"
        ri_log cont "components to install."
        ri_log cont "Re-run $CMD using bulk transfer mode."
        echo "transfer_mode='bulk'" >> $defaults
        ri_log_activity FAIL2
        exit 1
    fi

    if [ -f $install_dir/$ARCH/disk_install_dir ]
    then
        # Install may move this to the install/ARCH dir.
        disk_install_dir=$install_dir/$ARCH/disk_install_dir
    fi

    # Sets cmp_list, resets cmps.
    . $defaults.cmp_info
    Prod_ver=${def_subprod_desc:-$Prod_ver}
    if [ -n "$cmp_list" ]
    then
        echo "cmp_list='$cmp_list'" >> $disk_install_dir
        echo "total='$total'" >> $disk_install_dir

        for cn in $cmp_list
        do
            echo "cmp_${cn}_inst=yes"
        done >> $disk_install_dir
    fi
    ri_check_disk_space total $total

    echo
    ri_log note "Need to get" `echo "$cmp_list" | wc -w` "tar archives from the FTP server to install"
    ri_log note "$Prod_ver."

    ri_do_time_delay
    ri_enter_background
    $sleep_seconds

    n=1
    for i in $cmp_list
    do
        while [ $n -lt $i ]
        do
            ri_log note "Skipping file$n, not needed for this install."
            ri_update_ftp_sizes
            n=`expr $n + 1`
        done

        ri_get_file_and_check $ftp_src_dir/file$i  FAIL.$i
        n=`expr $n + 1`
    done

    # Get the patches, if any.
    ri_install_patch

    # Tell the install that it cannot tailor the install--must install what
    # has already been selected by the previous run of ./install.
    echo "product_selected=yes" >> $disk_install_dir
    echo "total_cmp=$cmps" >> $disk_install_dir

    if [ "$defer_install" = yes ]
    then
        ri_ftp_get_file $SCRIPTS/network_install
        chmod 775 network_install 2>/dev/null

        cp $disk_install_dir file.install
        cat $defaults.cmp_info >> file.install

        if [ "$ftp_background_mode" = yes ]
        then
            exec 1> $README 2>&1
        fi

        ri_log_activity PASS

        echo "
You can now make a network install tape of $prod_ver_plat.
Put all of the files beginning with 'file' on a tape along with the
network_install program:

    % tar -cvf TAPE_DEVICE network_install $patches file*
"
        $bg_exit

        files=`(/bin/ls file?; /bin/ls file??) 2>/dev/null`
        ri_get_yn "Do you want $CMD to do the tar command now?" yes
        if [ $yn = yes ]
        then
            ri_get_result "Enter the TAPE_DEVICE: "
            tape_device=$result
            echo tar -cvf $tape_device network_install $patches $files
            tar -cvf $tape_device network_install $patches $files
        fi
        exit 0
    fi
    ri_log_activity PASS

elif [ "$transfer_mode" = very_efficient ]
then
    # Extract the functions between #BEGIN and #END in this program.
    # Create a CMD_lib file in the install/share directory.
    /bin/rm -f $install_dir/share/rinstall_lib
    sed -e '1,/^#BEGIN /d' -e '/^#END /,$d' $FILE \
        > $install_dir/share/rinstall_lib
    echo FILE=$FILE >> $install_dir/share/rinstall_lib

    # Tell the install where to get the tar archives to install the product.
    echo "ftp_src_dir=$ftp_src_dir" >> $disk_install_dir

    # Get the patches, if any.
    ri_install_patch

    # Cleanup
    /bin/rm -f file0
    ri_log_activity PASS
fi

#-----------------------------------------------------------------
# Explain the next install step.
#-----------------------------------------------------------------
if [ -f $install_dir/install_patch ]
then
    run_install="./install_patch"
else
    run_install="./install"
fi

if [ "$ftp_background_mode" = yes ]
then
    exec 1> $README 2>&1
    install_now=no
elif [ ! -x install ]
then
    install_now=no
else
    # Check if in install directory.  If so, run install now."
    set -- `/bin/ls -id $install_dir`
    i_node1=$1
    set -- `/bin/ls -id .`
    i_node2=$1
    if [ "$i_node1" = "$i_node2" ]
    then
        install_now=yes
    else
        install_now=no
    fi
fi

Prod_ver=${Prod_ver:-$prod_ver_plat}
if [ "$install_now" = yes ]
then
    # In install directory!  Run install program.
    echo
    echo "Continuing with the installation of"
    echo "$Prod_ver--running $run_install now."
    echo
    exec $run_install
else
    echo
    echo "Do the following to continue with the installation of"
    echo "$Prod_ver:"
    echo
    echo "    % cd $install_dir"
    echo "    % $run_install"
    echo
fi

