#!/bin/sh
# +--------------------------------------------------------------------------+
# | Copyright 1994 - 2002 Rational Software Corporation. All Rights Reserved.|
# | This software contains proprietary and confidential information of       |
# | Rational and its suppliers. Use, disclosure or reproduction is           |
# | prohibited without the prior express written consent of Rational.        |
# +--------------------------------------------------------------------------+
# @(#)$Revision: 1.49 $  $Date: 06/06/2002 14:31:41 $

command=`basename $0`
args="$*"

# Extract user id from the id command.  The id command produces this type
# of output: uid=123(userid) gid=321(groupid)
# This is more accurate than depending on vars USER or LOGNAME.
# If someone does 'su user', the USER or LOGNAME does not reflect
# their new id, but the id command does.  The whoami command is
# not available on all UNIX platforms supported by this script.
USER=`id | sed -e 's?).*??g' -e 's?.*(??g'`
export USER

set +a

#------------------------------------------------------------------------
ni_same_file_or_dir() {
#------------------------------------------------------------------------
# Usage: ni_same_file_or_dir <file_or_dir1> <file_or_dir2>
#
# If the 2 args (files or directories) have the same i-node number then
# they are the same so return true.

    if [ "$1" = "$2" ]
    then
        return 0
    fi

    set -- 1 `/bin/ls -id $1 $2 2>/dev/null`
    test "$2" = "$4"
    return $?
}

#------------------------------------------------------------------------
# Set working directory. Remove /tmp_mnt prefix if it resolves to
# the same directory.
#------------------------------------------------------------------------
workd=`pwd`
case $workd in
/tmp_mnt/* )
    _workd=`echo $workd | sed -e "s?/tmp_mnt??"`
    if ni_same_file_or_dir $workd $_workd
    then
        workd=$_workd
    fi
    unset _workd
    ;;
esac

#------------------------------------------------------------------------
# Set defaults file and load.
#------------------------------------------------------------------------
old_defaults=/tmp/$command.$USER
if [ -n "$HOME" ]
then
    if [ -w $HOME/.Rational ]
    then
        defaults=$HOME/.Rational/$command
    elif [ -w $HOME ]
    then
        defaults=$HOME/.$command
    else
        defaults=/tmp/$command.$USER
    fi
else
    defaults=/tmp/$command.$USER
fi

# This file will serve as a list of rational_dir locations.
rational_dir_list=$defaults.list

# Move old file to new file if relocating it.
if [ -s $old_defaults  -a  "$defaults" != "$old_defaults" ]
then
    if [ -f $defaults ]
    then
        # Just remove old file.
        /bin/rm -f $old_defaults
    else
        # Relocate defaults.
        mv $old_defaults $defaults
    fi
fi

if [ -s $defaults ]
then
    . $defaults
fi

#------------------------------------------------------------------------
# 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

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

    if ${First_prompt:-true}
    then
        echo
        echo "Enter q at any prompt to quit $command."
        First_prompt=false
    fi

    while true
    do
        echo
        if [ -n "$2" ]
        then
            $ECHON "$1 [$2]  $NOCR"
        else
            $ECHON "$1 $NOCR"
        fi
        read result </dev/tty
        if [ -n "$result" ]
        then
            eval result=\"$result\"
        elif [ -n "$2" ]
        then
            result=$2
        else
            echo "You must enter a value."
            continue
        fi

        case "$result" in
        q|quit|Q|Quit|QUIT )
            exit 0
            ;;
        esac
        break
    done
}

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

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

#------------------------------------------------------------------------
ni_select_dir() {
#------------------------------------------------------------------------
# Select one directory from the rdlist.

    echo "
Select a 'rational_dir' directory:

      0 - Enter a directory of your own.
$out_list
      q - quit $command"

    while true
    do
        ni_get_input "Enter 0..$rdn or q:" 1
        case "$result" in
        0 )
            ni_get_input "Enter a directory:"
            return
            ;;
        [1-9]* )
            if [ "$result" -ge 1 -a "$result" -le $rdn ]
            then
                eval result=\$rd$result
                break
            else
                echo "You must enter a number from 0 to $rdn or q."
            fi
            ;;
        q|Q )
            exit 0
            ;;
        /* )
            return
            ;;
        * )
            echo "You must enter a number from 0 to $rdn or q."
            ;;
        esac
    done
}

#------------------------------------------------------------------------
ni_mkdir_p() {
#------------------------------------------------------------------------
#  mkdir -p does not work correctly on all Unix systems.

    if [ -d $1 ]
    then
        return
    fi

    _DIR_=/
    for _dir_ in `echo $1 | 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_
}

#------------------------------------------------------------------------
ni_get_tar_file() {
#------------------------------------------------------------------------

    echo "Checking for tar archives or product files in"
    echo "  $workd"
    set -- 1 `/bin/ls -1d *.tar.gz *.tar.Z *.tar 2>/dev/null |
              grep -v "^gzip\."`
    shift
    _N=$#
    if [ $_N -eq 1 ]
    then
        dir=$workd
        tar_file=$1
        return
    elif [ $_N -gt 1 ]
    then
        dir=$workd
    fi

    if [ $_N -eq 0 ]
    then
        if [ -d "$dir" ]
        then
            echo "Checking for tar archives or product files in"
            echo "  $dir"
            cd $dir
            set -- 1 `/bin/ls -1d file0 *.tar.gz *.tar.Z *.tar 2>/dev/null |
                      grep -v "^gzip\."`
            shift
            _N=$#
            if [ $_N -eq 1 ]
            then
                tar_file=$1
                return
            elif [ $_N -eq 0 ]
            then
                echo "No tar archives or product files found to install."
                echo "Aborting $command."
                exit 1
            fi
        else
            echo "No tar archives or product files found to install."
            echo "Aborting $command."
            exit 1
        fi
    fi

    echo
    echo "There are more than one tar archive files to choose from."
    echo "Select the tar archive you wish to install from:"
    /bin/ls -1d file0 *.tar.gz *.tar.Z *.tar 2>/dev/null |grep -v "^gzip\."
    while true
    do
        ni_get_input "Enter tar archive file name:"
        tar_file=$result
        if [ -f $tar_file ]
        then
            break
        fi
        echo "$tar_file is not in the list."
    done
}

#------------------------------------------------------------------------
ni_tell_get_gzip() {
#------------------------------------------------------------------------
# Tell user how to get gzip tool from Rational.

    echo
    echo "The gzip command must be on your search path to use $command."
    echo "You can get gzip from Rational by ftp'ing to ftp.rational.com"
    echo "as user 'anonymous' or 'ftp' and get the file:"
    echo "    /public/tools/gzip/<OS>/gzip.<REV>.tar.Z."
    echo "where <OS> is sol, sun, aix, sgi, hp or axp"
    echo "and <REV> is the gzip revision number (e.g. 1.2.4)."
    exit 1
}

#------------------------------------------------------------------------
ni_get_product_info() {
#------------------------------------------------------------------------

    test -n "$LAST" && return

    if [ -f $dir/file0 ]
    then
        ni_get_prod_info_from_file0
        return
    fi

    if [ -f extras/install_data/suite_description ]
    then
        ni_get_prod_info_from_suite
        return
    fi

    echo
    echo "No file0 or extras/install_data/suite_description"
    echo "found in $dir."
    echo "Please verify that you have ftp'ed the tar file."
    exit 1
}

#------------------------------------------------------------------------
ni_get_prod_info_from_suite() {
#------------------------------------------------------------------------

    echo
    echo Extracting product information from suite_description file...
    suite_description=extras/install_data/suite_description
    eval `grep rs_Suite_Name= $suite_description`
    eval `grep rs_Suite_Ver= $suite_description`
    eval `grep rs_Date= $suite_description`
    eval `grep rs_copyright= $suite_description`
    eval `grep rs_vendor_name= $suite_description`
    echo
    echo "Network install setup for:"
    echo "    Product:  $rs_Suite_Name"
    echo "    Version:  $rs_Suite_Ver"
    if [ -n "$rs_Date" ]
    then
    echo "    Date:     $rs_Date"
    fi
    echo "    $rs_copyright by $rs_vendor_name"
    echo
}

#------------------------------------------------------------------------
ni_get_prod_info_from_file0() {
#------------------------------------------------------------------------

    echo
    echo Extracting product information from tar file:
    echo "  $dir/file0"
    tmp_dir=/tmp/$command.$$
    mkdir $tmp_dir
    cd $tmp_dir
    prod_desc=`tar -tf $dir/file0 | grep prod_desc | head -1`
    tar -xf $dir/file0 $prod_desc
    . $prod_desc
    cd $dir
    /bin/rm -fr $tmp_dir
    release=${product_pver:-$product.$product_version}
    if [ "$vendor" = Rational ]
    then
        vendor="Rational Software Corporation"
    fi
    if [ -n "$release_type" ]
    then
        banner_version="$product_version$patch_level ($release_type)"
    else
        banner_version="$product_version$patch_level"
    fi

    echo
    echo "Network install setup for:"
    echo "    Product:  $product_name"
    echo "    Version:  $banner_version"
    echo "    Date:     $product_date"
    echo "    Copyright(c) $copyright_year by $vendor"
    echo

    if [ -n "$subprods" ]
    then
        LAST=${cmps:-0}
    else
        LAST=`expr ${required_cmps:-0} + ${recommend_cmps:-0} + \
                   ${optional_cmps:-0} + ${auxiliary_cmps:-0}`
    fi
}
#------------------------------------------------------------------------
ni_filter () {
#------------------------------------------------------------------------
sed -e "s?\.? ?" \
    -e "s?rose_apex?Rational Rose/C++ for Apex?" \
    -e "s?ada_analyzer?Ada Analyzer?" \
    -e "s?apex_clearcase?Apex/ClearCase?" \
    -e "s?apex_embedded?Rational Apex Embedded?" \
    -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?" \
    -e "s?rci?RCI?" \
    -e "s?puretools?Purify/PureCoverage/Quantify?" \
    -e "s?purify?Purify?" \
    -e "s?quantify?Quantify?" \
    -e "s?purecov?PureCoverage?" \
    -e "s?rose_cpp?Rational Rose/C++?" \
    -e "s?rose_dev?Rational Rose Developer?" \
    -e "s?rose_ada?Rational Rose/Ada?" \
    -e "s?rose98unix?Rational Rose 98 for UNIX?" \
    -e "s?rose 6\.0\.?Rational Rose 98i for UNIX 6.0.?" \
    -e "s?rose?Rational Rose?" \
    -e "s?screen_machine?OIS Screen Machine?" \
    -e "s?soda?SoDA?" \
    -e "s?testmate?Rational TestMate?" \
    -e "s?rx?Rational Exec?" \
    -e "s?rt?Rational Exec?" \
    -e "s?lx?LynxOS?" \
    -e "s?vw?VxWorks?" \
    -e "s?mips\.?MIPS ?" \
    -e "s?power\.?PowerPC ?" \
    -e "s?ppc\.?PowerPC ?" \
    -e "s?i386\.?Intel/Pentium ?" \
    -e "s?m686\.?Motorola 68K ?" \
    -e "s?/install??g" \
    -e "s?_? ?g" \
    -e "s?^?    ?"

}

#------------------------------------------------------------------------
ni_list_pv() {
#------------------------------------------------------------------------
    (
    cd $ratl_dir/releases
    list=`find * -type d -name \*.\* -print -prune`
    if [ -n "$list" ]
    then
        unset new_list
        for pv in $list
        do
            case $pv in
            apex_embedded.* )
                for aei in apex_embedded.*/*/install
                do
                    test -d $aei && new_list="$new_list $aei"
                done
                ;;
            * )
                new_list="$new_list $pv"
                ;;
            esac
        done
        /bin/ls -1d $new_list | ni_filter
    fi
    )
}

#------------------------------------------------------------------------
redefine_forbid_root() {
#------------------------------------------------------------------------
# If installer wants to run as root, update the forbid_root function.
# Otherwise, install scripts will not run.

    cd $install_dir
    # Check if a share.tar.Z or .gz file exists.
    if [ -f share.tar.Z ]
    then
        echo "Uncompressing install tools..."
        uncompress share.tar.Z
    elif [ -f share.tar.gz ]
    then
        echo "Gunzip'ing install tools..."
        gunzip share.tar.gz
    fi

    # Unpack the share.tar file to create the share directory.
    if [ -f share.tar ]
    then
        echo "Reading install tools from tar archive..."
        tar -xf share.tar
        /bin/rm -f share.tar
        chmod -R g+w share 2>/dev/null
    fi

    # Check if this has already been done.
    forbid_comment="New forbid_root function: Allows root."
    if grep "$forbid_comment" share/install_lib >/dev/null
    then
        cd $workd
        return
    fi

    # Redefine the forbid_root function to just return.
    (
    _d1="------------------------------------"
    echo
    echo "#${_d1}${_d1}"
    echo 'forbid_root() {'
    echo "#${_d1}${_d1}"
    echo "# $forbid_comment"
    echo
    echo '    return'
    echo '}'
    echo
    ) >> $install_dir/share/install_lib
    cd $workd
}

#------------------------------------------------------------------------
ni_set_owner_of_dir() {
#------------------------------------------------------------------------
# If the owner of a directory is 8 or more characters,
# there is a possibility that the name is truncated.  In that
# case, will use the ypcat command and the numeric user id to
# look up the owner's login name.

    set -- `$LSL -d $_rd`
    owner=$3

    # Determine the numeric user id (arg 3).
    set -- `$LSN $_rd`
    uid=$3

    _len=`echo "$owner" | wc -c`
    test $_len -lt 9 && return

    # Lookup the user name using the numeric user id, if it was set.
    test -z "$uid" && return
    set -- 1 `ypcat passwd 2>/dev/null |
              awk -F: '{print $1, $3}' | grep " $uid\$"`
    test $# -gt 2 && owner=$2
}

#------------------------------------------------------------------------
ni_check_user_is_owner() {
#------------------------------------------------------------------------
# This function is called with 2 args.  The first is a keyword
# to control what is done in the event that the user is not the
# owner of the directory (arg 2).
# Keyword:   Meaning:
#    quiet - Just check ownership and return 0 if owner, 1 if not.
#  verbose - Prompt user if they want to use this directory anyway,
#            but only if they have write permission on it.

    _keyword=$1
    _rd=$2
    chmod u+w $_rd 2>/dev/null && return 0
    test "$_keyword" = quiet && return 1

    ni_set_owner_of_dir
    if [ -n "$uid" ]
    then
        run_CMD_as="run $CMD as '$owner' (uid=$uid)"
    else
        run_CMD_as="run $CMD as '$owner'"
    fi

    echo "
You should not install products in:
  $_rd

Your user ID ('$USER') does not match that of the original
installer ('$owner') into this area.
You should $run_CMD_as
or select a different Rational directory.

Rational strongly recommends that you install products
in a common Rational directory as the same user.
You may run into permission problems if you do not consistently
install products as the same installer in the same Rational directory.
"
    if [ -w $_rd ]
    then
        ni_get_yn "Do you want to use this directory anyway?" no
        test $result = yes && return 0
    fi
    return 1
}


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

# Check if installer is root.
if [ "$USER" = root ]
then
    ni_get_yn "NOTE: You are logged into `uname -n` as root.
      On many UNIX systems, when installing as root, the files end up being
      owned by the person that created the tar files, not as root.  This is
      one reason why you should not install products as root.

Do you want to continue $command as root?" no
    test $result = no && exit 0
fi

LS=/bin/ls
LSL="$LS -l"
LSN="$LS -nd"
case "`uname -sr`" in
SunOS4* )
    LSL="$LS -lg"
    LSN="/usr/5bin/ls -nd"
    ;;
Linux* )
    LS=ls
    LSL="$LS -l"
    LSN="$LS -lnd"
    ;;
esac

#------------------------------------------------------------------------
# Set gzip command to use.
#------------------------------------------------------------------------
if [ -f ./gzip ]
then
    # Fully qualify in case . is not in search path
    GZIP=$workd/gzip
else
    # Use search path to locate
    GZIP=gzip
fi

#------------------------------------------------------------------------
# Get the tar file name.
#------------------------------------------------------------------------
for=for
Do_gzip=false
Do_uncomp=false
if [ -f file0 ]
then
    dir=$workd
    tar_file=file0
    type=no_tar
    ni_get_product_info
fi

if [ -z "$type" ]
then
    ni_get_tar_file
    case $tar_file in
    file0 )
        tar_file=file0
        type=no_tar
        ni_get_product_info
        ;;
    *.patch.tar.gz )
        type=patch
        Do_gzip=true
        for="to patch"
        suffix=.tar.gz
        ;;
    *.patch.tar.Z )
        type=patch
        Do_uncomp=true
        for="to patch"
        suffix=.tar.Z
        ;;
    *.patch.tar )
        type=patch
        for="to patch"
        suffix=.tar
        ;;
    *.tar.gz )
        type=regular
        Do_gzip=true
        suffix=.tar.gz
        ;;
    *.tar.Z )
        type=regular
        Do_uncomp=true
        suffix=.tar.Z
        ;;
    *.tar )
        type=regular
        suffix=.tar
        ;;
    * )
        echo
        if [ -n "$missing" ]
        then
            echo "
No tar file found in $dir.
There is a file0 which indicates a partial $command took place.
Expected file0..file$LAST but some of the files are missing.
Missing:$missing
Please verify that you have ftp'ed the tar file
and delete these files:"
            /bin/ls -C file? file?? 2>/dev/null
        else
            echo "No tar file or file0 found in $dir."
            echo "Please verify that you have ftp'ed the tar file."
        fi
        exit 1
    esac
    echo "Found tar file: $tar_file"
fi

#------------------------------------------------------------------------
# Verify the current directory name--important for network access.
#------------------------------------------------------------------------
echo
echo "Enter the location of the $tar_file file.  It is important to"
echo "use the permanent path name that is network accessible if you are"
echo "not going to complete the installation on this machine."

while true
do
    ni_get_input "$tar_file location:" $dir
    if [ -f $result/$tar_file ]
    then
        dir=$result
        break
    else
        echo $result/$tar_file not found.
    fi
done

# Compute the default rational_dir directory.
# Try to get it from current directory.
if [ "`basename $dir`" = install ]
then
    # It is save in this file:
    if [ -f ./.install_defaults ]
    then
        . ./.install_defaults
    else
        # It is up 3 levels.
        def_rational_dir=$dir
        def_rational_dir=`dirname $def_rational_dir` # remove /install
        def_rational_dir=`dirname $def_rational_dir` # remove /prod.vers
        def_rational_dir=`dirname $def_rational_dir` # remove /releases
    fi
else
    # It may be up one level.
    def_rational_dir=`dirname $dir`
    if [ ! -d $def_rational_dir/releases ]
    then
        unset def_rational_dir
    fi
fi

rational_dir=${rational_dir:-$def_rational_dir}

if [ "$rational_dir" = "$def_rational_dir" ]
then
    unset def_rational_dir
fi

rdn=0
for rd in $rational_dir $def_rational_dir
do
    if [ -d $rd ]
    then
        if ni_check_user_is_owner quiet $rd
        then
            rdlist="$rdlist $rd"
            rdn=`expr $rdn + 1`
        fi
    fi
done

if [ -s $rational_dir_list ]
then
    First=true
    for rd in `cat $rational_dir_list`
    do
        test "$rd" = "$rational_dir" && continue
        test -d $rd || continue
        ni_check_user_is_owner quiet $rd || continue

        # Make the first dir in the list top the list. This will
        # be the default for ni_select_dir.
        if $First
        then
            First=false
            rdlist="$rd $rdlist"
        else
            rdlist="$rdlist $rd"
        fi
        rdn=`expr $rdn + 1`
    done
fi

# Create a list of all existing directories.
if [ $rdn -gt 1 ]
then
    set -- $rdlist
    n=1
    while [ $# -gt 0 ]
    do
        if [ -d $1 ]
        then
            if [ $n -eq 1 ]
            then
                rdlist="$1"
                out_list=">>>>> 1 - $1"
            else
                rdlist="$rdlist $1"
                out_list="$out_list
      $n - $1"
            fi
            eval rd$n=$1
            n=`expr $n + 1`
        fi
        shift
    done
fi

echo
echo "Please enter the location of the 'rational_dir' directory.  This is"
echo "where Rational products will be installed.  This directory must"
echo "already exist.  It will contain the directories base, config,"
echo "releases, and symbolic links to products in the releases directory."

while true
do
    if [ $rdn -le 1 ]
    then
        ni_get_input "'rational_dir' directory:" $rational_dir $rdlist
    else
        ni_select_dir
    fi

    ratl_dir=$result
    if [ -d $ratl_dir ]
    then
        ni_check_user_is_owner verbose $ratl_dir || continue
        if [ -d $ratl_dir/releases ]
        then
            pv_list=`ni_list_pv`
            if [ -z "$pv_list" ]
            then
                break
            fi

            echo "
You have selected $ratl_dir as the 'rational_dir' directory.
These products appear to be installed there:

$pv_list"
            ni_get_yn "Is this okay so far?" yes
            if [ $result = yes ]
            then
                break
            else
                ni_get_yn "Do you want to continue with $command?" yes
                if [ $result = no ]
                then
                    exit 0
                fi
            fi
        else
            break
        fi
    else
        echo
        echo "$ratl_dir does not exist."
        ni_get_yn "Do you want to create it?" no
        if [ $result = yes ]
        then
            if ni_mkdir_p $ratl_dir
            then
                break
            fi
        fi
    fi
done
rational_dir=$ratl_dir

#------------------------------------------------------------------------
# Save default settings.
#------------------------------------------------------------------------
echo
if [ -f $defaults ]
then
    /bin/rm -f $defaults
    echo Updating: $defaults
else
    echo Creating: $defaults
fi
echo "          (This 'file0' location and 'rational_dir' directory)"
echo dir=$dir > $defaults
echo rational_dir=$rational_dir >> $defaults
chmod 666 $defaults

if [ -s $rational_dir_list ]
then
    echo Updating: $rational_dir_list >&2
    # Make the new rational_dir top the list.  Make the list no more than 10.
    echo $rational_dir
    grep -v "^$rational_dir\$" $rational_dir_list | head -9
else
    # Create the rational_dir list.
    echo Creating: $rational_dir_list >&2
    echo "$rational_dir"
fi > $rational_dir_list.new
echo "          (List of 'rational_dir' directories)"
/bin/rm -f $rational_dir_list
/bin/cp $rational_dir_list.new $rational_dir_list
/bin/rm -f $rational_dir_list.new

#------------------------------------------------------------------------
# Unpack the tar file
#------------------------------------------------------------------------
if $Do_uncomp
then
    echo
    echo "Unpacking compressed tar file: $tar_file"
    dd if=$tar_file | uncompress | tar -xf -
    if [ $? -ne 0 ]
    then
        echo "$command: Problem encountered while unpacking $tar_file."
        exit 1
    fi
elif $Do_gzip
then
    while true
    do
       eval $GZIP --help >/dev/null 2>&1
       if [ $? -eq 0 ]
       then
          break
       else
          gzip_tar_file=`ls -1d gzip.*.tar.Z 2>/dev/null`
          if [ -z "$gzip_tar_file" ]
          then
             ni_tell_get_gzip
          else
             echo
             echo The gzip command is not on your search path.
             echo "You have a tar archive of the gzip command.  Will unpack..."
             dd if=$gzip_tar_file | uncompress | tar -xvf -
             if [ -f ./gzip ]
             then
                echo "Done. $gzip_tar_file no longer needed."
                ni_get_yn "Remove $gzip_tar_file? (y/n)"
                if [ $result = yes ]
                then
                    echo "Okay, removing $gzip_tar_file."
                    /bin/rm -f $gzip_tar_file
                else
                    echo "Will not remove $gzip_tar_file."
                fi
                GZIP=$dir/gzip
             else
                echo "Could not unpack $gzip_tar_file."
                ni_tell_get_gzip
             fi
          fi
       fi
    done

    echo
    echo "Unpacking gzip'ed tar file: $tar_file"
    echo "  $GZIP -dc $tar_file | tar -xvf - "
    if $GZIP -dc $tar_file | tar -xvf -
    then
        echo "The $GZIP command has completed."
        echo "The $tar_file file is no longer needed for this installation."
        ni_get_yn "Remove $gzip_tar_file? (y/n)"
        if [ $result = yes ]
        then
            echo "Okay, removing $tar_file."
            /bin/rm -f $tar_file
        else
            echo "Will not remove $tar_file."
        fi
    else
        echo "$command: Problem encountered while unpacking $tar_file."
        exit 1
    fi
elif [ "$tar_file" != file0 ]
then
    echo
    echo "Unpacking tar file: $tar_file"
    tar -xvf $tar_file
    if [ $? -ne 0 ]
    then
        echo "$command: Problem encountered while unpacking $tar_file."
        exit 1
    fi
fi

if [ -x ./rs_install ]
then
    ni_get_product_info
    echo
    echo "Running ./rs_install now..."
    exec ./rs_install
else
    subdir=`basename $tar_file $suffix`
    if [ -x $subdir/rs_install ]
    then
        cd $subdir
        ni_get_product_info
        echo
        echo "To continue with this installation, do the following:"
        echo
        echo "   cd $subdir ; ./rs_install"
        echo
        exit 0
    fi
fi

if [ ! -f $dir/file0 ]
then
    echo "$command: No file0 found in the $tar_file file."
    exit 1
fi

ni_get_product_info
install_dir=$rational_dir/releases/$release/install

# Create the product release & install directory under the rational_dir
echo
echo Installing $release
$ECHON "in $install_dir...$NOCR"
(cd $rational_dir; tar -xf $dir/file0)
echo done.

# Add post_patch file to install.
if [ -f post_patch -a ! -f post_patch_added ]
then
    cat post_patch >> $install_dir/post_patch
    touch post_patch_added
fi

if [ -d $install_dir ]
then
    /bin/rm -f $install_dir/share/disk_install_dir
    if [ -f file.install ]
    then
        # File created by rinstall for efficient ftp installs.
        # Reset the disk install directory.
        sed -e "s?disk_install_dir=.*?disk_install_dir=$dir?" file.install \
           > $install_dir/share/disk_install_dir
    else
        echo "disk_install_dir=$dir" >  $install_dir/share/disk_install_dir
        echo "device_kind=network"   >> $install_dir/share/disk_install_dir
    fi
else
    echo "$command: The unpacking of $dir/file0 failed."
    echo "          The directory: $install_dir"
    echo "          did not get created."
    exit 1
fi

#------------------------------------------------------------------------
# Ask if installer would like to cleanup tar file.
#------------------------------------------------------------------------
if [ "$tar_file" != file0 ]
then
    echo
    echo "The $tar_file file is no longer needed $for the installation."
    ni_get_yn "Remove $tar_file file? (y/n)"
    if [ $result = yes ]
    then
        echo Okay, removing $tar_file
        /bin/rm -f $tar_file
    else
        echo "Will not remove $tar_file."
    fi
fi

#------------------------------------------------------------------------
# If installer wants to run as root, update the forbid_root function:
#------------------------------------------------------------------------
if [ "$USER" = root ]
then
    redefine_forbid_root
fi

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

touch $install_dir/.test.$$
if [ -f .test.$$ ]
then
    # In install directory!  Run install program.
    /bin/rm -f $install_dir/.test.$$
    echo
    echo "Continuing with the installation of $release $inst_type."
    echo "Running:  $run_install"
    exec $run_install
else
    /bin/rm -f $install_dir/.test.$$
    # Tell user: Must cd to install directory, run install program.
    echo
    echo "To continue with the installation of $release $inst_type,"
    echo "do the following:"
    echo
    echo "  cd $install_dir"
    echo "  $run_install"
    echo
fi

if [ -f ./gzip ]
then
    echo "REMINDER: You should move gzip, gunzip & zcat to a directory"
    echo "          on your search path."
    echo
fi

