# create_dir - Create a directory path.
#
# Version 1.3
#
# Pre:
#    admin_dir - directory containing administration scripts
#    dir_wanted - directory that you want to have created
# Post:
#    return_result - return result:
#      0 = success (directory created)
#      1 = directory just below root doesn't exist (e.g., "/foobar" was specd)
#      2 = tried to create a directory and failed
#
# This routine is silent, using the return result code to allow the client
# to determine what to say to the user in case of an error.
#
# This routine is sourced from csh.
#
# It uses the following utility scripts:
#   abort - abort and exit the top csh script
#   set_mode - set the permissions mode of files in a path
#
# 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: 950121 - jag : Change chmod to set_mode
# 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

# Set the assumed return result.
set return_result = 0

# Copy the wanted directory.
set cd_dir = $dir_wanted

# Move up the path that is to be created from the outermost leaf,
# testing for the first directory that doesn't exist.
# Be careful, the head of "/foo" is "" and the head/tail of "foo" is "foo".
set cd_found = 0
set cd_tail = ()
while (! $cd_found && ($cd_dir != ""))
  if (-e $cd_dir) then
    set cd_found = 1
  else
    set cd_tail = ($cd_dir:t $cd_tail)
    if ($cd_dir == $cd_tail[1]) then
      set cd_dir = "."
    else
      set cd_dir = $cd_dir:h
    endif
  endif
end

# Does the directory just below the root path not exist?
# If so, error (e.g., "/foo/bar" was specified, but /foo doesn't exist).
# This script should probably not be adding things to the root directory.
if (! $cd_found) then
  set return_result = 1
  exit
endif

# Use the tail list to start creating directories.  Don't change directories.
# cd_dir points to the last existing directory.
# cd_tail contains the components of the path to be made.
foreach cd_word ($cd_tail)

  # Tack the word onto 'cd_dir', which holds the deepest existing directory.
  set cd_dir = $cd_dir/$cd_word

  # Skip if the directory already exists.
  # This would occur in the following cases:
  #  1. $cd_word is "." or ".."
  #  2. The component was created earlier in the path and ".." was used
  #     to return to it.
  #  3. The component already existed and ".." was used to return to it.
  if (-e $cd_dir) continue

  # Try to create the directory.
  mkdir $cd_dir

  # Test to make sure it was created.
  if (! -e $cd_dir) then
    set return_result = 2
    exit
  endif

  # Set the permissions mode of the directory.
  set set_path = $cd_dir
  source $admin_dir/set_mode

end

# Success.  Return.
exit

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

source $admin_dir/abort
