# match_path - Match a path with an array of paths using hardpaths.
#
# Version 1.3
#
# Pre:
#    admin_dir - directory containing administration scripts
#    file_path - path to match against file_array elements
#    file_array - array of file paths to match
# Post:
#    file_index - index in file_array matching file_path (0 if no match)
#
# The file paths are compared after converting each to hardpaths.
# The content of file_path and file_array may be directory or file paths.
#
# 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: 950312 - 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 hardpaths and turn it on.
set mp_hardpathsset = $?hardpaths
set hardpaths

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

# Get the length of the file array.
@ mp_n = $#file_array

# Convert file_path to a hardpath if it exists.
if (-e "$file_path") then
  if (-d "$file_path") then
    set mp_dir = $file_path
    set mp_file = ""
  else
    set mp_dir = $file_path:h
    set mp_file = "/$file_path:t"
  endif
  pushd $mp_dir >& /dev/null
  set mp_dirs = (`dirs -l`)
  popd >& /dev/null
  set mp_file_path = $mp_dirs[1]$mp_file
else
  set mp_file_path = "$file_path"
endif

# Go through the file array elements.
@ mp_i = 0
while ($mp_i < $mp_n)
  @ mp_i ++

  # Convert the file array path to a hardpath if it exists.
  set mp_temp = $file_array[$mp_i]
  if (-e "$mp_temp") then
    if (-d "$mp_temp") then
      set mp_dir = $mp_temp
      set mp_file = ""
    else
      set mp_dir = $mp_temp:h
      set mp_file = "/$mp_temp:t"
    endif
    pushd $mp_dir >& /dev/null
    set mp_dirs = (`dirs -l`)
    popd >& /dev/null
    set mp_file_array = $mp_dirs[1]$mp_file
  else
    set mp_file_array = "$mp_temp"
  endif

  # Save the index and quit if the file paths match.
  if ("$mp_file_path" == "$mp_file_array") then
    set file_index = $mp_i
    break
  endif

end

# Restore the state of hardpaths.
if (! $mp_hardpathsset) unset hardpaths

# Return.
exit

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

source $admin_dir/abort
