# set_mode - Set the permissions mode of files in a path.
#
# Version 1.3
#
# Pre:
#    admin_dir - directory containing administration scripts
#    set_path - file or directory path to change
#
# Permissions are set as follows:
#   go+rx - directories, executable files
#   go+r  - plain files
#
# 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: 950121 - jag
# Modified: 950314 - jag : Set onintr to abort only if enable_intr set
# Modified: 950422 - jag : Remove -f option from chmod

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

# Disable. Let tar, cp, and umask control the permissions.
exit

# Quit if the path does not exist.
# This test includes a link to a nonexistent file.
if (! -e $set_path) exit

# If the path is a directory, get the real path after resolving links.
# Quit if we cannot change to the directory - probably could not find anyway.
set sm_path = $set_path
if (-d $sm_path) then
  pushd $sm_path >& /dev/null
  if ($status) exit
  set sm_path = `pwd`
  popd >& /dev/null
endif

# Get a list of all files in the path, excluding links.
# Quit if the find failed - probably an unreadable directory.
set sm_files = `find $sm_path ! -type l -print`
if ($status) exit

# Go through each file in the list.
foreach sm_file ($sm_files)

  # Skip if the file does not exist (e.g., a link to a nonexistent file).
  if (! -e $sm_file) continue

  # Set the permissions mode of the file.
  # Use go+rx for a directory or executable file, go+r for anything else.
  if (-d $sm_file || -x $sm_file) then
    chmod go+rx $sm_file
  else
    chmod go+r $sm_file
  endif

end

# Return.
exit

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

source $admin_dir/abort
