# match_arrays - Match strings with arrays of strings.
#
# Version 1.3
#
# Pre:
#    admin_dir - directory containing administration scripts
#    array_names - array of names of string arrays to be matched
#                  all string arrays must be the same length
#    string_data - array of strings to match, one per string array name
# Post:
#    array_index - index in string arrays matching string_data (0 if no match)
#
# The array strings and match strings may contain spaces and file
# metacharacters.
#
# The match strings and array strings are compared as follows:
#  1. Match string vs. filename-substituted array element.
#  2. Array element vs. filename-substituted match string.
#
# Two comparisons are performed to allow use of file metacharacters in all
# strings. However, using metacharacters in both the string arrays and
# match strings may yield unexpected results.
#
# This routine is sourced from csh.
#
# It uses the following utility scripts:
#   abort - abort and exit the top csh script
#
# 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: 950314 - jag : Set onintr to abort only if enable_intr set

# 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 noglob and turn it on.
set ma_noglobset = $?noglob
set noglob

# Initialize the return index to indicate no match.
set array_index = 0

# Get the length of the string arrays.
eval @ ma_n = '$#'$array_names[1]

# Get the number of string arrays.
@ ma_na = $#array_names

# Go through the string array elements.
@ ma_i = 0
while ($ma_i < $ma_n)
  @ ma_i ++

  # Set the flag to indicate that all elements matched.
  set ma_flag = 1

  # Check each string array for a match at this index.
  @ ma_ia = 0
  while ($ma_ia < $ma_na)
    @ ma_ia ++

    # Get the string array element and match string.
    eval set ma_array = '"$'$array_names[$ma_ia]'[$ma_i]"'
    set ma_string = "$string_data[$ma_ia]"

    # Clear the flag and quit for this index if there is no match.
    #
    # We could use two !~ tests, instead of nested switch statements,
    # except that either side of the test can have file metacharacters
    # and/or spaces. Quotes are necessary to protect spaces, but they
    # disable filename substitution.

    # Compare the match string to the filename-substituted array element.
    switch ("$ma_string")

      case "$ma_array":
        # Match - continue.
        breaksw

      default:
        # No match.
        # Compare the array element to the filename-substituted match string.
        switch ("$ma_array")

          case "$ma_string":
            # Match - continue.
            breaksw

          default:
            # No match - quit.
            set ma_flag = 0
            break

        endsw

    endsw
    
  end

  # Save the index and quit if all elements matched.
  if ($ma_flag) then
    set array_index = $ma_i
    break
  endif

end

# Restore the state of noglob.
if (! $ma_noglobset) unset noglob

# Return.
exit

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

source $admin_dir/abort
