# get_dir - Get a directory path, create if necessary, check for exist/write.
#
# Version 1.3
#
# Pre:
#    admin_dir - directory containing administration scripts
#    get_dir_def - default directory path
#    get_dir_type - type of directory (used for messages)
# Post:
#    get_dir - chosen directory path
#    return_result - return result:
#      0 = success (directory exists or was created)
#      1 = directory just below root doesn't exist (e.g., "/foobar" was specd)
#      2 = tried to create a directory and failed
#      3 = directory path does not exist and create was not requested
#      4 = path is not a directory
#      5 = no write permission for the directory
#
# This routine is sourced from csh.
#
# It uses the following utility scripts:
#   abort - abort and exit the top csh script
#   create_dir - create a directory if it does not exist
#   prompt - get the directory and actions in case of failure
#
# 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

# Get the directory.
set udefault = "$get_dir_def"
source $admin_dir/prompt
set get_dir = "$userin"

# Exists?
if (! -e "$get_dir") then
  echo ""
  echo "The following $get_dir_type directory does not exist:"
  echo ""
  echo "   $get_dir"
  echo ""
  echo "Shall I create it for you?  Press <return> to let me create it for"
  echo "you now, 'exit' and <return> to exit this script, and anything else"
  echo "if you wish to specify the $get_dir_type directory again."
  set udefault = "y"
  source $admin_dir/prompt
  if ("$userin" == "y") then
    echo ""
    echo -n "Creating $get_dir... "

    # Set up and jump to the directory-creating subroutine.
    # This will set return_result to 0, 1, or 2.
    set dir_wanted = "$get_dir"
    source $admin_dir/create_dir

    # Check the return value.
    if ($return_result == 0) then
      # Successful.
      echo "success."
    else
      # Unsuccessful.
      echo "FAILURE."
      echo ""
      echo "Sorry, I encountered an error trying to create the directory."
      echo "Please press <return> to re-specify the $get_dir_type directory,"
      echo "or type 'exit' and <return> to exit this script."
      set udefault = ""
      source $admin_dir/prompt
      exit
    endif
  else
    # Directory does not exist and should not be created.
    set return_result = 3
    exit
  endif
endif

# Is directory?
if (! -d "$get_dir") then
  set return_result = 4
  echo ""
  echo "The following $get_dir_type path is not a directory:"
  echo ""
  echo "   $get_dir"
  echo ""
  echo "If you mistyped it, please press <return> to try setting it again."
  echo "Otherwise, type 'exit' and <return> to exit this script, make sure"
  echo "it is a directory, and then restart the installation."
  set udefault = ""
  source $admin_dir/prompt
  exit
endif

# Write permission?
if (! -w "$get_dir") then
  set return_result = 5
  echo ""
  echo "You do not have write permission for the $get_dir_type directory:"
  echo ""
  echo "   $get_dir"
  echo ""
  echo "If you mistyped it, please press <return> to try setting it again."
  echo "Otherwise, type 'exit' and <return> to exit this script, make sure"
  echo "you have write permission for the directory, and then restart the"
  echo "installation."
  set udefault = ""
  source $admin_dir/prompt
  exit
endif

# Convert the path to absolute form.
pushd $get_dir >& /dev/null
set gd_dirs = (`dirs -l`)
popd >& /dev/null
set get_dir = $gd_dirs[1]

# Indicate success and return.
set return_result = 0
exit

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

source $admin_dir/abort
