# admin_check - Check that the admin directory contains required files.
#
# Version 1.3
#
# Pre:
#    admin_dir - directory containing administration scripts
#    admin_link - 1 => create $admin_link_dir with links to files in $admin_dir
#                      and check $admin_link_dir instead of $admin_dir
#    admin_link_dir - directory with links to 8.3-named files in $admin_dir
#                     This should be a new directory to be created in an
#                     existing directory (e.g., /tmp/confluent_tmp.$$).
#    name_iso - command to translate correct file names to ISO9660 format
#    name_case - command to translate correct file names to upper case
#    name_ver - file name version suffix
#
# 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: 950121 - jag : Add set_mode
# Modified: 950312 - jag : Add match_path
# Modified: 950827 - jag : Create $admin_link_dir with links and check links
#                          Change install_init to inst_init
#                          Change config_init to conf_init
# Modified: 970219 - jag : Remove use of :q modifier

onintr cleanup_mark

# Set the list of required source files in the admin directory.
set ac_source = (abort admin_check conf_init create_dir exit file_date \
                file_info get_dir get_links get_version inst_init \
                match_arrays match_path prompt set_mode)

# Set the list of required execute files in the admin directory.
set ac_exec = (format_line install modify_docs uninstall)

# Set up the directory to check for required files.
# If a link directory is used, set up links to uppercase 8.3-named files.
if ($admin_link) then
  # Create the link directory.
  mkdir $admin_link_dir

  # Make sure the link directory was created.
  if (! -e $admin_link_dir) then
    echo ""
    echo "Links are created to files in the following directory:"
    echo ""
    echo "   $admin_dir"
    echo ""
    echo "The following temporary directory for the links could not be created:"
    echo ""
    echo "   $admin_link_dir"
    echo ""
    echo "You may not have write permission to the parent directory."
    echo ""
    goto cleanup_mark
  endif

  # Convert the admin directory path to absolute form.
  pushd $admin_dir >& /dev/null
  set ac_dirs = (`dirs -l`)
  popd >& /dev/null
  set ac_dir = $ac_dirs[1]

  # Create the links from the admin directory to the link directory.
  foreach ac_link ($ac_source $ac_exec distribution)
    set ac_file = `echo $ac_link | tr "$name_case[1]" "$name_case[2]"`
    set ac_file = `echo $ac_file | sed -e "$name_iso"`$name_ver
    ln -s $ac_dir/$ac_file $admin_link_dir/$ac_link
  end

  set ac_dir = $admin_link_dir
else
  set ac_dir = $admin_dir
endif

# Check that all required files have proper permissions in the directory.
set ac_missing = ""
foreach ac_link ($ac_source)
  if (! -r $ac_dir/$ac_link) set ac_missing = "$ac_missing $ac_link"
end
foreach ac_link ($ac_exec)
  if (! -x $ac_dir/$ac_link) set ac_missing = "$ac_missing $ac_link"
end

# Indicate any problem files.
if ("$ac_missing" != "") then
  echo ""
  echo "This script resides in the following directory:"
  echo ""
  echo "   $admin_dir"
  echo ""
  echo "The following files are either missing from this directory or you"
  echo "do not have read or execute permission for them:"
  echo ""
  foreach ac_link ($ac_missing)
    if ($admin_link) then
      set ac_file = `echo $ac_link | tr "$name_case[1]" "$name_case[2]"`
      set ac_file = `echo $ac_file | sed -e "$name_iso"`$name_ver
    else
      set ac_file = $ac_link
    endif
    echo "   $ac_file"
  end
  echo ""
  goto cleanup_mark
endif

# Return.
exit

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

# Remove the link directory if it was created.
if ($admin_link) then
  if (-e $admin_link_dir) rm -rf $admin_link_dir
endif

# Perform the equivalent of abort.
echo "Aborting."
exec sleep 0
