#! /bin/csh -f
#
# uninstall - Script for uninstalling Confluent products from CD-ROM.
#
# Version 1.3
#
# This script must be executed from a valid distribution directory, such
# as the top-level directory of the CD-ROM.  A valid distribution directory
# contains the admin directory and top-level product directories
# (e.g., util-1.1, vt-1.1).
#
# This script must reside in a directory with other scripts from the
# admin directory.
#
# For example, if the CD-ROM is mounted as /cdrom, the following
# uninstall procedures are valid and equivalent:
#
#  1. Execute this script directly from the CD-ROM:
#       cd /cdrom
#       admin/uninstall
#
#  2. Copy the directory containing this script and execute the copy of
#     this script:
#       cp -r /cdrom/admin /tmp/admin_tmp
#       cd /cdrom
#       /tmp/admin_tmp/uninstall
#
# This routine uses the following utility scripts:
#   abort - abort and exit the top csh script
#   admin_check - check for required files in admin directory
#   conf_init - initialize configuration arrays with product information
#   exit - exit the top csh script
#   file_info - get file information
#   format_line - format strings with specified string formats
#   get_links - get a list of link paths matching a base path
#   inst_init - initialize for install and uninstall
#   match_arrays - match strings with arrays of strings
#   match_path - match a path with an array of paths using hardpaths
#   prompt - get the directory and actions in case of failure
#
# The following working arrays are used:
#   keep:        1 => no uninstall choice (required by another product)
#   map:         indices of products available for uninstall
#   uninstall:   1 => uninstall
#   rm_product:  1 => remove product directory
#   rm_bin_link: 1 => remove bin links
#   rm_man_link: 1 => remove man links
#
# Copyright (c) 1994 by
#
#    Confluent, Inc.
#    400 Spear St., Suite 207
#    San Francisco, CA 94105
#
# Voice: 415-764-1000
# Fax: 415-764-1008
# Internet: info@confluent.com
#
# Created: 940916 - wpt
# Modified: 950111 - jag : Move to admin and rewrite
# Modified: 950314 - jag : Use match_path to compare file paths
#                          Disable interrupts during file uninstall
# Modified: 950422 - jag : Remove -f option from chmod
# Modified: 950625 - jag : Keep install directory if not empty after uninstall
# Modified: 950827 - jag : Set admin_link=0 for admin_check
#                          Move introduction to beginning

#----------------------------------------------------------------------
# Switch to a temporary copy of this script
#----------------------------------------------------------------------

#set echo

# Set up paths for temporary copies of the exit and uninstall scripts that
# will not be removed if the entire installation directory is removed.
# The process number ($$) is retained by exec.
set exit_tmp = /tmp/confluent_exit.$$
set uninstall_tmp = /tmp/confluent_uninstall.$$

if ("$0" == "$uninstall_tmp") then

  # This is the temporary copy.

  # Get the admin directory from the first argument.
  set admin_dir = $1

else

  # This is the original copy.

  # Set the admin directory for sourcing csh scripts to the directory
  # containing this script.
  # It may be relative or absolute and is set to "." if there are no
  # leading path components.
  set admin_dir = $0
  set admin_dir = $admin_dir:h
  if ($admin_dir == $0) set admin_dir = "."

  # Create a temporary copy of uninstall.
  cp $0 $uninstall_tmp

  # Make sure the temporary copy is available.
  if (! -e $uninstall_tmp) then
    echo ""
    echo "The uninstall script copies itself to /tmp and executes the copy to"
    echo "avoid removing the script while it is being executed."
    echo ""
    echo "The following file could not be created:"
    echo ""
    echo "   $uninstall_tmp"
    echo ""
    echo "You may not have write permission to the /tmp directory."
    echo ""
    echo "Aborting."
    exit (0)
  endif

  # Execute the temporary copy of uninstall.
  exec $uninstall_tmp $admin_dir
endif

#----------------------------------------------------------------------
# Check admin directory
#----------------------------------------------------------------------

# Check that the admin directory contains the required files.
# If this directory contains uppercase ISO9660-named files, it is a
# distribution directory and we should fail here.
set file = $admin_dir/admin_check
if (-r $file) then
  set admin_link = 0
  source $file
else
  echo ""
  echo "The following file is not available:"
  echo ""
  echo "   $file"
  echo ""
  echo "Aborting."
  exit (0)
endif

#----------------------------------------------------------------------
# Basic initialization
#----------------------------------------------------------------------

# Define the list of temporary files to be removed by exit.
set exit_files = "$uninstall_tmp"

# Set up the interrupt label.
# This should be done after $admin_dir is set and the admin directory is
# checked for required files.
onintr cleanup_mark

# Initialize.
source $admin_dir/inst_init

# Create a temporary copy of exit and add to the list of temporary files.
set exit_files = "$exit_tmp $exit_files"
cp $admin_dir/exit $exit_tmp

# Set install_dir to the current directory.
set install_dir = $current_dir

#----------------------------------------------------------------------
# Introduction
#----------------------------------------------------------------------

# Banner and intro.
if ($paging) clear
echo ""
echo "----------------------------------------------------------------------"
echo "Confluent Uninstaller ($install_version_d)"
echo "----------------------------------------------------------------------"
echo ""
echo "This script will uninstall Confluent products from the installation"
echo "directory (the current directory when this script was started):"
echo ""
echo "   $current_dir"
echo ""
echo "It will ask you a series of questions.  Your system will not"
echo "be changed until this script says it will do so."
echo ""
echo "Default answers to questions will appear in brackets before"
echo "the prompt and will be used if you just press <return>."
echo ""
echo "You may type 'exit' at any prompt to exit the uninstall script."
echo "Please press <return> to continue, anything else to exit."
set udefault = ""
source $admin_dir/prompt
if ("$userin" != "") goto cleanup_mark

#----------------------------------------------------------------------
# Get configuration of installation directory
#----------------------------------------------------------------------

# Get configuration information (e.g., $product) for product installation
# directories in the installation directory.
# Old installations are included.
set config_top = "."
set config_sfx = ""
set config_old = 1
source $admin_dir/conf_init
@ n_product = $#product

# The installation directory must contain at least one product installation
# directory.  If not, abort.
if ($n_product == 0) then
  echo ""
  echo "The current directory"
  echo ""
  echo "   $current_dir"
  echo ""
  echo "does not contain any valid product installation directories."
  echo "Please change to a top-level installation directory and type:"
  echo ""
  echo "   admin/uninstall"
  echo ""
  goto cleanup_mark
endif

# The current directory must not be a distribution directory.
if ($config_dist) then
  echo ""
  echo "The current directory"
  echo ""
  echo "   $current_dir"
  echo ""
  echo "is a top-level distribution directory.  You cannot uninstall"
  echo "products from this directory.  Please change to a top-level"
  echo "installation directory and type:"
  echo ""
  echo "   admin/uninstall"
  echo ""
  goto cleanup_mark
endif

#----------------------------------------------------------------------
# Mark products required by other products
#----------------------------------------------------------------------

set keep = ($config_init)

keep_mark:

# Mark products required by other products.
# These are not available for direct uninstall by the user.
# Repeat the process until all required products are marked.
set found = 0
set array_names = ("oshead" "product" "keep")
@ i = 0
while ($i < $n_product)
  @ i ++

  # Go through each required product.
  foreach id ($required[$i])

    # If the required product is not yet marked, indicate that one was
    # found and mark it.
    set string_data = ("$oshead[$i]" "$id" "0")
    source $admin_dir/match_arrays
    if ($array_index > 0) then
      set found = 1
      set keep[$array_index] = 1
    endif

  end

end

# If additional products were marked, go through again.
if ($found) goto keep_mark

#----------------------------------------------------------------------
# List products available for uninstall
#----------------------------------------------------------------------

# Check whether any top-level products are available for uninstall.
set array_names = ("top_level")
set string_data = ("1")
source $admin_dir/match_arrays

# Set up a map to convert product IDs to array indices.
set map = ($config_init)

# List the top-level products in the installation directory.
# Count those that must be kept and those available for uninstall.
@ n_keep = 0
@ n_uninstall = 0
if ($array_index > 0) then
  echo ""
  echo "The following products are installed in the current directory:"
  echo ""
  echo "   Directory: $current_dir"
  echo ""
  $admin_dir/format_line "   ID  " "" \
                         "product" $fmt_product \
                         "version" $fmt_version \
                         "machine" $fmt_machine \
                         "system" $fmt_system \
                         "date"
  $admin_dir/format_line "   --  " "" \
                         "-------" $fmt_product \
                         "-------" $fmt_version \
                         "-------" $fmt_machine \
                         "------" $fmt_system \
                         "----"

  # List the products.
  @ i = 0
  while ($i < $n_product)
    @ i ++

    # Consider only top-level products.
    if (! $top_level[$i]) continue

    # List an ID and add it to the map only for products that are not
    # required.
    if ($keep[$i]) then
      @ n_keep ++
      set idstr = ""
    else
      @ n_uninstall ++
      set idstr = $n_uninstall
      set map[$n_uninstall] = $i
    endif

    $admin_dir/format_line "$idstr" 5 "  " "" \
                           "$product_d[$i]" $fmt_product \
                           "$version_d[$i]" $fmt_version \
                           "$machine_d[$i]" $fmt_machine \
                           "$osdescr[$i]" $fmt_system \
                           "$date_install[$i]"
  end
endif

# Explain entries without IDs as required products.
if ($n_keep > 0) then
  echo ""
  echo "Products in the above list without an ID number are required by"
  echo "other installed products.  You must uninstall the requiring product"
  echo "before the required product will be available to be uninstalled."
endif

# If there are no products to uninstall, there is nothing left to do.
if ($n_uninstall == 0) then
  echo ""
  echo "There are no products available for uninstall in the installation"
  echo "directory:"
  echo ""
  echo "   $current_dir"
  echo ""
  source $admin_dir/exit
endif

# Explain entries with IDs.
echo ""
echo "Products in the above list with an ID number can be uninstalled."

#----------------------------------------------------------------------
# Get products to uninstall
#----------------------------------------------------------------------

uninstall_mark:

# Find out which products the user wishes to uninstall.
echo ""
echo "Please type a list of the ID numbers (separated by spaces) for the"
echo "products you wish to uninstall, or 'exit' to exit."
set udefault = ""
source $admin_dir/prompt
if ("$userin" == "") goto uninstall_mark

# Check each ID against the map.
# If it is not in the map, prompt again for the ID list.
# Otherwise, mark the product for uninstall.
set uninstall = ($config_init)
foreach id ($userin)
  # Use a switch to safely test only numbers greater than 0.
  switch ($id)
    case [1-9]:
    case [1-9][0-9]:
    case [1-9][0-9][0-9]:
      if ($id <= $n_uninstall) breaksw
    default:
      goto uninstall_mark
  endsw

  set uninstall[$map[$id]] = 1
end

echo ""
echo -n "Please wait a moment while I determine what to remove..."

#----------------------------------------------------------------------
# Mark products required by products being uninstalled
#----------------------------------------------------------------------

required_mark:

# Mark for uninstall non-top-level products required by products being
# uninstalled.
# Repeat the process until all required products are marked.
set found = 0
set array_names = ("oshead" "product" "top_level" "uninstall")
@ i = 0
while ($i < $n_product)
  @ i ++

  # Consider only products marked for uninstall.
  if (! $uninstall[$i]) continue

  # Go through each required product.
  foreach id ($required[$i])

    # If the required product is not yet marked for uninstall,
    # indicate that one was found and mark it.
    set string_data = ("$oshead[$i]" "$id" "0" "0")
    source $admin_dir/match_arrays
    if ($array_index > 0) then
      set found = 1
      set uninstall[$array_index] = 1
    endif

  end

end

# If additional products were marked for uninstall, go through again.
if ($found) goto required_mark

echo -n "."

#----------------------------------------------------------------------
# Unmark products required by products not being uninstalled
#----------------------------------------------------------------------

clear_mark:

# Unmark for uninstall products required by products not being uninstalled.
# Repeat the process until all required products are marked.
set found = 0
set array_names = ("oshead" "product" "uninstall")
@ i = 0
while ($i < $n_product)
  @ i ++

  # Consider only products not marked for uninstall.
  if ($uninstall[$i]) continue

  # Go through each required product.
  foreach id ($required[$i])

    # If the required product is marked for uninstall,
    # indicate that one was found and unmark it.
    set string_data = ("$oshead[$i]" "$id" "1")
    source $admin_dir/match_arrays
    if ($array_index > 0) then
      set found = 1
      set uninstall[$array_index] = 0
    endif

  end

end

# If additional products were unmarked for uninstall, go through again.
if ($found) goto clear_mark

echo -n "."

#----------------------------------------------------------------------
# Mark products being uninstalled with need to remove product directory
#----------------------------------------------------------------------

# Set rm_product for the last system marked for uninstall if a product
# is being uninstalled for all systems.
# This is used to determine when to uninstall the product directory.
set rm_product = ($config_init)
set array_names = ("product" "uninstall")
@ i = 0
while ($i < $n_product)
  @ i ++

  # Consider only products marked for uninstall.
  if (! $uninstall[$i]) continue

  # Skip if the product is not being uninstalled for some system.
  set string_data = ("$product[$i]" "0")
  source $admin_dir/match_arrays
  if ($array_index > 0) continue

  # Set uninstall to 2 so we can identify the last system.
  set uninstall[$i] = 2

  # If this is the last system marked for uninstall of this product,
  # set rm_product.
  # We are at the last system if uninstall is 2 for all systems.
  set string_data = ("$product[$i]" "1")
  source $admin_dir/match_arrays
  if ($array_index == 0) set rm_product[$i] = 1

end

# Restore uninstall to 1 for all products.
@ i = 0
while ($i < $n_product)
  @ i ++
  if ($uninstall[$i] == 2) set uninstall[$i] = 1
end

echo -n "."

#----------------------------------------------------------------------
# Mark products being uninstalled with need to remove executable links
#----------------------------------------------------------------------

# Set rm_bin_link for the last system marked for uninstall if a product
# is being uninstalled for all systems with the same executable directory.
# This depends on old distributions having only a single system with
# links to $install_dir/$product/$oshead/bin.
# This is used to determine when to remove executable links.
set rm_bin_link = ($config_init)
set array_names = ("product" "bin_install" "uninstall")
@ i = 0
while ($i < $n_product)
  @ i ++

  # Consider only products marked for uninstall.
  if (! $uninstall[$i]) continue

  # Skip if there are no command links.
  if ("$commands[$i]" == "") continue

  # Skip if the executable directory is empty.
  if ("$bin_install[$i]" == "") continue

  # Skip if the product is not being uninstalled for some system with the
  # same executable directory.
  set string_data = ("$product[$i]" "$bin_install[$i]" "0")
  source $admin_dir/match_arrays
  if ($array_index > 0) continue

  # Set uninstall to 2 so we can identify the last system.
  set uninstall[$i] = 2

  # If this is the last system marked for uninstall of this product
  # with the same executable directory, set rm_bin_link.
  # We are at the last system if uninstall is 2 for all systems.
  set string_data = ("$product[$i]" "$bin_install[$i]" "1")
  source $admin_dir/match_arrays
  if ($array_index == 0) set rm_bin_link[$i] = 1

end

# Restore uninstall to 1 for all products.
@ i = 0
while ($i < $n_product)
  @ i ++
  if ($uninstall[$i] == 2) set uninstall[$i] = 1
end

echo -n "."

#----------------------------------------------------------------------
# Mark products being uninstalled with need to remove man page links
#----------------------------------------------------------------------

# Set rm_man_link for the last system marked for uninstall if a product
# is being uninstalled for all systems with the same man pages directory.
# This depends on old distributions having only a single system with
# links to $install_dir/$product/$oshead/man.
# This is used to determine when to remove man page links.
set rm_man_link = ($config_init)
set array_names = ("product" "man_install" "uninstall")
@ i = 0
while ($i < $n_product)
  @ i ++

  # Consider only products marked for uninstall.
  if (! $uninstall[$i]) continue

  # Skip if there are no man page links.
  if ("$man_pages[$i]" == "") continue

  # Skip if the man pages directory is empty.
  if ("$man_install[$i]" == "") continue

  # Skip if the product is not being uninstalled for some system with the
  # same man pages directory.
  set string_data = ("$product[$i]" "$man_install[$i]" "0")
  source $admin_dir/match_arrays
  if ($array_index > 0) continue

  # Set uninstall to 2 so we can identify the last system.
  set uninstall[$i] = 2

  # If this is the last system marked for uninstall of this product
  # with the same man pages directory, set rm_man_link.
  # We are at the last system if uninstall is 2 for all systems.
  set string_data = ("$product[$i]" "$man_install[$i]" "1")
  source $admin_dir/match_arrays
  if ($array_index == 0) set rm_man_link[$i] = 1

end

# Restore uninstall to 1 for all products.
@ i = 0
while ($i < $n_product)
  @ i ++
  if ($uninstall[$i] == 2) set uninstall[$i] = 1
end

echo -n "."

#----------------------------------------------------------------------
# Check for removal of the installation directory and product directories
#----------------------------------------------------------------------

# Set rm_install if all products are being uninstalled.
set array_names = ("uninstall")
set string_data = ("0")
source $admin_dir/match_arrays
@ rm_install = ($array_index == 0)

# Set rm_top if any product directories are being removed.
set array_names = ("rm_product")
set string_data = ("1")
source $admin_dir/match_arrays
@ rm_top = ($array_index > 0)

echo -n "."

#----------------------------------------------------------------------
# Check that directories are writable for products being uninstalled
#----------------------------------------------------------------------

set dirs_w = ""

# Make a list of directories to check in the installation directory.
set dirs = ""

# Add the parent of the installation directory if it is being removed.
if ($rm_install) set dirs = "$dirs .."

# Add the installation directory if any product directories are being removed.
if ($rm_top) set dirs = "$dirs ."

# Add the product directories.
set array_names = ("product" "uninstall")
@ i = 0
while ($i < $n_product)
  @ i ++

  # Consider only products marked for uninstall.
  if (! $uninstall[$i]) continue

  # If this is the first system marked for uninstall for this product,
  # add the product directory.
  set string_data = ("$product[$i]" "1")
  source $admin_dir/match_arrays
  if ($array_index == $i) set dirs = "$dirs $product[$i]"
end

# Find any directories in the list that are not writable.
# These are existing directories that were found during configuration.
foreach dir ($dirs)
  switch ($dir)
    case "..":
      set file = $install_dir:h
      breaksw
    case ".":
      set file = $install_dir
      breaksw
    default:
      set file = $install_dir/$dir
  endsw
  if (! -w $file) set dirs_w = "$dirs_w $file"
end

echo -n "."

#----------------------------------------------------------------------
# Check that directories exist and are writable for links being removed
#----------------------------------------------------------------------

set dirs_d = ""

# Make a list of directories and inodes to check for link removal.
set dirs = ()
set inodes = ()

# Add the executable directories.
set array_names = ("bin_install" "rm_bin_link")
@ i = 0
while ($i < $n_product)
  @ i ++

  # Consider only products for which executable links are being removed.
  if (! $rm_bin_link[$i]) continue

  # If this is the first system marked for link removal for this directory,
  # add the executable directory.
  set string_data = ("$bin_install[$i]" "1")
  source $admin_dir/match_arrays
  if ($array_index == $i) then
    set temp = ($bin_install[$i])
    set dirs = ($dirs $temp[1])
    set inodes = ($inodes $temp[2])
  endif
end

# Add the man pages directories.
# This actually adds the parent of the directories containing the links.
# However, they probably have the same permission.
set array_names = ("man_install" "rm_man_link")
@ i = 0
while ($i < $n_product)
  @ i ++

  # Consider only products for which man page links are being removed.
  if (! $rm_man_link[$i]) continue

  # If this is the first system marked for link removal for this directory,
  # add the man pages directory.
  set string_data = ("$man_install[$i]" "1")
  source $admin_dir/match_arrays
  if ($array_index == $i) then
    set temp = ($man_install[$i])
    set dirs = ($dirs $temp[1])
    set inodes = ($inodes $temp[2])
  endif
end

# Find any paths in the list that are not directories or are not writable.
@ n = $#dirs
@ i = 0
while ($i < $n)
  @ i ++

  # Get the inode of the path.
  set file_name = $dirs[$i]
  source $admin_dir/file_info

  # If the path exists, but the inode to match is zero, this is an
  # old installation for which the inode was not available.
  # Assume that existence implies a match.
  set inode = $inodes[$i]
  if ($file_inode > 0 && $inode == 0) set inode = $file_inode

  # Add the path to the list of unavailable directories if:
  #  1. The path inode does not match the inode when the link was created.
  #  2. The path inode matches, but the path is not a directory.
  #
  # Add the path to the list of unwritable directories if:
  #  1. The path is a directory, but is not writable.
  if ($file_inode == $inode) then
    if (-d $file_name) then
      if (! -w $file_name) set dirs_w = "$dirs_w $file_name"
    else
      set dirs_d = "$dirs_d $file_name"
    endif
  else
    set dirs_d = "$dirs_d $file_name"
  endif

end

echo "."

#----------------------------------------------------------------------
# Indicate unwritable directories
#----------------------------------------------------------------------

if ("$dirs_w" != "") then
  echo ""
  echo "You do not have write permission to the following directories:"
  echo ""
  foreach dir ($dirs_w)
    echo "   $dir"
  end
  echo ""
  echo "Make sure these paths are writable directories and restart the"
  echo "uninstall."
  echo ""
  goto cleanup_mark
endif

#----------------------------------------------------------------------
# Recapitulate settings
#----------------------------------------------------------------------

# Banner.
if ($paging) clear
echo ""
echo "----------------------------------------------------------------------"
echo "Uninstall Configuration Summary"
echo "----------------------------------------------------------------------"

# List top-level products marked for uninstall.
echo ""
echo "Products being uninstalled:"
echo ""
$admin_dir/format_line "   " "" \
                       "product" $fmt_product \
                       "version" $fmt_version \
                       "machine" $fmt_machine \
                       "system"
$admin_dir/format_line "   " "" \
                       "-------" $fmt_product \
                       "-------" $fmt_version \
                       "-------" $fmt_machine \
                       "------"
@ i = 0
while ($i < $n_product)
  @ i ++
  if (! $uninstall[$i] || ! $top_level[$i]) continue
  $admin_dir/format_line "   " "" \
                         "$product_d[$i]" $fmt_product \
                         "$version_d[$i]" $fmt_version \
                         "$machine_d[$i]" $fmt_machine \
                         "$osdescr[$i]"
end

# List unavailable directories.
if ("$dirs_d" != "") then
  echo ""
  echo ""
  echo "*** WARNING *** WARNING *** WARNING *** WARNING *** WARNING ***"
  echo ""
  echo "Links may remain in the following directories after the uninstall"
  echo "because these directories are not available or have been recreated"
  echo "since the install:"
  echo ""
  foreach dir ($dirs_d)
    echo "   $dir"
  end
endif

# List products being completely removed.
if ($rm_top) then
  echo ""
  echo ""
  echo "*** WARNING *** WARNING *** WARNING *** WARNING *** WARNING ***"
  echo ""
  echo "The following product directories in the current directory"
  echo ""
  echo "   $current_dir"
  echo ""
  echo "will be completely removed from your system, including any additional"
  echo "files you may have placed in these directories:"
  echo ""
  $admin_dir/format_line "   " "" \
                         "product" $fmt_product \
                         "version" $fmt_version \
                         "directory"
  $admin_dir/format_line "   " "" \
                         "-------" $fmt_product \
                         "-------" $fmt_version \
                         "---------"
  @ i = 0
  while ($i < $n_product)
    @ i ++
    if (! $rm_product[$i]) continue
    $admin_dir/format_line "   " "" \
                           "$product_d[$i]" $fmt_product \
                           "$version_d[$i]" $fmt_version \
                           "$product[$i]"
  end
endif

# Warn about removal of the installation directory.
if ($rm_install) then
  echo ""
  echo ""
  echo "*** WARNING *** WARNING *** WARNING *** WARNING *** WARNING ***"
  echo ""
  echo "The following files and directories in the current directory"
  echo ""
  echo "   $current_dir"
  echo ""
  echo "will be completely removed from your system, including any additional"
  echo "files you may have placed in these directories:"
  echo ""
  foreach dir ($top_files $top_dirs)
    echo "   $dir"
  end
  echo ""
  echo "The current directory will be removed if it does not contain any"
  echo "additional files or directories."
endif

echo ""
echo "----------------------------------------------------------------------"
echo ""
echo "Up to this point, no files have been removed from your file system."
echo "Once the uninstall begins, you will not be able to interrupt it."
echo "Type 'y' to begin the uninstall, anything else to exit."

set udefault = ""
source $admin_dir/prompt
if ("$userin" != "y") goto cleanup_mark

# Disable interrupts.
set enable_intr = 0
onintr -

if ($paging) clear

#----------------------------------------------------------------------
# Uninstall products
#----------------------------------------------------------------------

# Go through each product.
@ i = 0
while ($i < $n_product)
  @ i ++

  # Consider only products marked for uninstall.
  if (! $uninstall[$i]) continue

  echo ""
  echo "Uninstalling $product_d[$i] $version_d[$i] for $osdescr[$i]"

  # Set up the installation product, system, and share directory paths.
  set product_inst = $product[$i]
  set system_inst = $product_inst/$oshead[$i]
  set share_inst = $product_inst/share

  # Remove the system directory.
  if (-e $system_inst) then
    echo "  Removing $system_inst..."
    chmod -R +w $system_inst
    rm -rf $system_inst
  endif

  # Remove links from the executable directory.
  if ($rm_bin_link[$i]) then

    # Get the executable directory and its desired and actual inode.
    set temp = ($bin_install[$i])
    set bin_dir = $temp[1]
    set inode = $temp[2]
    set file_name = $bin_dir
    source $admin_dir/file_info

    # If the directory exists, but the inode to match is zero, this is an
    # old installation for which the inode was not available.
    # Assume that existence implies a match.
    if ($file_inode > 0 && $inode == 0) set inode = $file_inode

    # Remove links if the directory is valid with the same inode.
    if (-d $bin_dir && $file_inode == $inode) then

      # Get the expected link target directories for old and new forms
      # of installations.
      set dir_old = $install_dir/$system_inst/bin
      set dir = $install_dir/$share_inst/bin

      set link_version = $version[$i]

      set first = 1
      foreach cmd ($commands[$i])

        # Set up the possible link targets to match.
        set file_array = ($dir/$cmd $dir_old/$cmd $dir_old/$cmd.csh)

        # Get a list of links matching the command in the executable directory.
        set link_base = $bin_dir/$cmd
        source $admin_dir/get_links

        # Remove matching links.
        set first_link = 1
        @ n = $#link_path
        @ j = 0
        while ($j < $n)
          @ j ++

          # Skip if the link target does not match.
          set file_path = $link_target[$j]
          source $admin_dir/match_path
          if ($file_index == 0) continue

          if ($first) then
            echo "  Removing links from the executable directory:"
            echo "    $bin_dir"
            echo -n "     "
            set first = 0
          endif

          if ($first_link) then
            echo -n " $cmd"
            set first_link = 0
          endif

          # Remove the link.
          rm -f $link_path[$j]

        end

      end
      if (! $first) echo ""

    endif

  endif

  # Remove links from the man pages directory.
  if ($rm_man_link[$i]) then

    # Get the man pages directory and its desired and actual inode.
    set temp = ($man_install[$i])
    set man_dir = $temp[1]
    set inode = $temp[2]
    set file_name = $man_dir
    source $admin_dir/file_info

    # If the directory exists, but the inode to match is zero, this is an
    # old installation for which the inode was not available.
    # Assume that existence implies a match.
    if ($file_inode > 0 && $inode == 0) set inode = $file_inode

    # Remove links if the directory is valid with the same inode.
    if (-d $man_dir && $file_inode == $inode) then

      # Get the expected link target directories for old and new forms
      # of installations.
      set dir_old = $install_dir/$system_inst/man
      set dir = $install_dir/$share_inst/man

      set first = 1
      foreach man ($man_pages[$i])

        # Get the section number.
        set section = $man:e

        # Set up the link path.
        set file_name = $man_dir/man$section/$man

        # Get the link target.
        source $admin_dir/file_info

        # Skip if the link target does not match.
        set file_path = $file_link
        set file_array = ($dir/$man $dir_old/$man)
        source $admin_dir/match_path
        if ($file_index == 0) continue

        if ($first) then
          echo "  Removing links from the man pages directory:"
          echo "    $man_dir"
          echo -n "     "
          set first = 0
        endif
        echo -n " $man"

        # Remove the link.
        rm -f $file_name
      end
      if (! $first) echo ""

    endif

  endif

  # If the product directory is being removed.
  if ($rm_product[$i]) then

    # Remove the share directory.
    if (-e $share_inst) then
      echo "  Removing $share_inst..."
      chmod -R +w $share_inst
      rm -rf $share_inst
    endif

    # Remove the installation product directory.
    if (-e $product_inst) then
      chmod -R +w $product_inst
      rm -rf $product_inst
    endif
  endif

end

#----------------------------------------------------------------------
# Uninstall the installation directory
#----------------------------------------------------------------------

# Remove files and directories in the installation directory.
if ($rm_install) then
  echo ""
  echo "Removing files and directories in the installation directory..."
  foreach file ($top_files $top_dirs)
    set dir = $install_dir/$file
    chmod -R +w $dir
    rm -rf $dir
  end
endif

# If the installation directory is not empty, disable removal.
set temp = (`ls -a $install_dir`)
if ($#temp > 2) @ rm_install = 0

# Remove the installation directory.
# Need to cd out of the installation directory to remove it.
if ($rm_install) then
  echo ""
  echo "Removing the installation directory..."
  cd ..
  rmdir $install_dir
endif

#----------------------------------------------------------------------
# Exit
#----------------------------------------------------------------------

# Done.
echo ""
echo "Done."
echo ""
echo "You have successfully uninstalled Confluent products."

# If the installation directory was removed, indicate how to get back to
# a valid directory.
if ($rm_install) then
  echo ""
  echo "The current directory"
  echo ""
  echo "   $current_dir"
  echo ""
  echo "was removed during the uninstall.  To connect to an existing"
  echo "directory, please type:"
  echo ""
  echo "   cd"

  # Unset current_dir so exit will not cd to it.
  unset current_dir
endif

# Execute the temporary copy of exit in case the original has been removed.
source $exit_tmp
exit (0)

#----------------------------------------------------------------------
# Cleanup
#----------------------------------------------------------------------
cleanup_mark:

source $admin_dir/abort
