# get_links - Get a list of link paths matching a base path.
#
# Version 1.3
#
# Pre:
#    admin_dir - directory containing administration scripts
#    link_base - base path to match
#    link_version - product version for path suffixes
#    link_product - product to match if version is extracted
#                   Only used if link_version is not specified.
# Post:
#    link_version - product version extracted if not specified (default: "0.0")
#    link_path - array of paths matching $link_base
#    link_target - array of link targets for $link_path ("0" for non-link)
#    link_next - next available path matching $link_base
#                Use the last possible if none are available.
#
# The following possible matches are considered:
#   $link_base
#   $link_base.old
#   $link_base-$link_version$sfx ($sfx = "", a, b, ..., z)
#
# This routine is sourced from csh.
#
# It uses the following utility scripts:
#   abort - abort and exit the top csh script
#   file_info - get file information
#   match_arrays - match strings with arrays of strings
#
# Copyright (c) 1995 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: 950116 - jag
# Modified: 950206 - jag : Restrict version extraction to Confluent links
# Modified: 950309 - jag : Remove use of link_oshead
# Modified: 950314 - jag : Set onintr to abort only if enable_intr set
# Modified: 970110 - jag : Quote glob arguments

# If enable_intr is set, set onintr to abort; otherwise, ignore interrupts.
if ($enable_intr) then
  onintr cleanup_mark
else
  onintr -
endif

# Save the state of nonomatch and turn it on.
set gl_nonomatchset = $?nonomatch
set nonomatch

# If the product version was not specified, extract it from the target of
# the base path of the form:
#   .../product-version/oshead/bin/cmd (old form)
#   .../product-version/share/bin/cmd  (new form)
# Require that links match one of the above forms.
# Use "0.0" if the version cannot be extracted.
if ("$link_version" == "") then
  set file_name = $link_base
  source $admin_dir/file_info

  # The target must be a link.
  if ("$file_link" != "") then

    # Convert the path into an array of path elements.
    set gl_temp = (`echo $file_link | sed -e 's;/; ;g'`)

    if ($#gl_temp >= 5) then

      # Extract the last 4 path elements.
      @ gl_i = $#gl_temp - 3
      set gl_temp = ($gl_temp[$gl_i-])

      # The first element must be of the form product-version and the
      # third elements must match.
      set gl_pv = (`echo $gl_temp[1] | sed -e 's/-/ /'`)
      if ($#gl_pv == 2 && "$gl_temp[3]" == "bin") then

        # The product must match.
        set gl_prod = `echo $link_product | sed -e 's/-.*$//'`
        if ("$gl_pv[1]" == "$gl_prod") then
          set link_version = $gl_pv[2]
        endif

      endif

    endif

  endif

  if ("$link_version" == "") set link_version = "0.0"
endif

# Set the regular expression for matching paths.
# Paths are of the following form:
#   $link_base
#   $link_base.old
#   $link_base-$link_version$sfx ($sfx = "", a, b, ...)
set gl_pathexp = "$link_base"'{,.old,-'"$link_version"'{,[a-z]}}'

# Clear the expression if there are no matches.
# glob returns the filename substitution pattern if there are no matches
# because nonomatch is set.
set gl_glob = `glob "$gl_pathexp"`
if ("$gl_glob" == "$gl_pathexp") set gl_pathexp = ""

# Set up the path and target lists.
# Nothing will be done if the path expression is empty.
set gl_paths = ""
set gl_targets = ""
foreach file_name ($gl_pathexp)

  # Get the directory information.
  source $admin_dir/file_info

  # Skip if the path does not exist.
  if ($file_inode == 0) continue

  # Add to the path list.
  set gl_paths = "$gl_paths $file_name"

  # Add to the target list.
  if ("$file_link" == "") then
    set gl_targets = "$gl_targets 0"
  else
    set gl_targets = "$gl_targets $file_link"
  endif
end

# Save the path and target lists.
set link_path = ($gl_paths)
set link_target = ($gl_targets)

# Determine the next available matching path.
# Use the last if none are available.
set array_names = ("link_path")
foreach gl_sfx ("" a b c d e f g h i j k l m n o p q r s t u v w x y z)
  set string_data = ("$link_base-$link_version$gl_sfx")
  source $admin_dir/match_arrays
  if ($array_index == 0) break
end
set link_next = "$string_data[1]"

# Restore the state of nonomatch.
if (! $gl_nonomatchset) unset nonomatch

# Return.
exit

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

source $admin_dir/abort
