# conf_init - Initialize configuration arrays with product installation
#             information.
#
# Version 1.3
#
# Pre:
#    admin_dir - directory containing administration scripts and old
#                configuration files
#    config_top - directory to search for product installation directories
#    config_sfx - suffix for configuration arrays (represented as $s)
#    config_old - 1 => include old installations, using old configuration
#                      files in place of missing config directories
# Post:
#    fmt_product - string format for "product_d  "
#    fmt_version - string format for "version_d  "
#    fmt_machine - string format for "machine_d  "
#    fmt_system - string format for "osdescr  "
#    fmt_command - string format for "commands  "
#    config_dir$s - directories containing configuration scripts
#    config_init$s - list of 0's for initializing arrays
#    config_dist$s - 1 => distribution directory containing admin/distribution
#    configuration arrays - configuration information (e.g., product$s) 
#
# This routine is sourced from csh.
#
# It uses the following utility scripts:
#   abort - abort and exit the top csh script
#   file_date - get the modification date/time for a file
#
# It must remain backward compatible by initializing arrays that are no
# longer used.
#
# 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: 950313 - jag : Add setup of fmt_command
# Modified: 950314 - jag : Set onintr to abort only if enable_intr set
# Modified: 970110 - jag : Quote glob arguments

# 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 nonomatch and turn it on.
set ci_nonomatchset = $?nonomatch
set nonomatch

# Configuration information set by sourcing product.dat:
#  product      : product name and version abbreviation (e.g., "vt-1.1")
#  product_d    : product name (e.g., "Visual Thought")
#  version      : product version abbreviation (not used) (e.g., "1.1")
#  version_d    : product version (e.g., "1.1")
#  required     : required product installations (e.g., "util-1.1")
#  top_level    : whether available for installation (e.g., "0" or "1")
#  commands     : command scripts to modify and link (e.g., "vthought")
#  man_pages    : man pages to link (e.g., "xgrabsc.n")
#  documents    : document directories/files (e.g., "examples/vthought.txt")

# Configuration information set by sourcing system.dat:
#  oshead       : system head of directory path (e.g., "sunos-4.1")
#  machine      : machine name regexp for 'uname -m' (e.g., "sun4*")
#  machine_d    : full machine name (e.g., "Sun SPARC")
#  osname       : system name regexp for 'uname -s' (e.g., "SunOS")
#  osrel        : system release regexp for 'uname -r' (e.g., "4.*")
#  osdescr      : full system name and release (e.g., "SunOS 4.x")
#  date_install : installation date (e.g. "12/31/94 12:00")
#  bin_install  : installed executable dir/inode (e.g., "/usr/local/bin 123")
#  man_install  : installed man pages dir/inode (e.g., "/usr/local/man 123")

# Set up the list of configuration array names.
set ci_names = ("product" "product_d" "version" "version_d" "required" \
                "top_level" "commands" "man_pages" "documents" \
                "oshead" "machine" "machine_d" "osname" "osrel" "osdescr" \
                "date_install" "bin_install" "man_install")

# Set field widths for string formatting.
set fmt_product = "-16.14"
set fmt_version = "-12.10"
set fmt_machine = "-11.9"
set fmt_system = "-13.11"
set fmt_command = "-13.11"

# Set the regular expression for matching config directories.
# Directories are of the form $config_top/$product/$oshead/config.
set ci_direxp = "$config_top"/'*-*.*/*-*'/config

# Clear the expression if there are no matches.
# glob returns the filename substitution pattern if there are no matches
# because nonomatch is set.
set ci_glob = `glob "$ci_direxp"`
if ("$ci_glob" == "$ci_direxp") set ci_direxp = ""

# Old installations (e.g., vt-1.0) can be of the following form:
#  1. Installed from a 1.0 distribution:
#       $product/$oshead/config
#  2. Installed from a 1.0 distribution and a >1.0 distribution:
#       $product/share/config/product.dat (from >1.0 distribution)
#       $product/$oshead/config (from 1.0 distribution)
#  3. Installed from a >1.0 distribution:
#       $product/share/config/product.dat
#       $product/$oshead/config/system.dat
# Cases 1 and 2 must be configured with $admin_dir/old.
# Case 3 is configured normally.
set ci_olddirs = ""
set ci_date = ""

# If old installations are included.
if ($config_old) then

  # Get the modification date/time of the uninstall script, if it exists.
  # If it does not exist, file_date returns a default date/time.
  # This date is used to replace empty dates obtained from $admin_dir/old.
  set file_name = $config_top/uninstall
  source $admin_dir/file_date
  set ci_date = "$file_date"

  # Set the regular expression for matching old config directories.
  # Directories are of the form $admin_dir/old/$product/$oshead/config.
  set ci_oldexp = "$admin_dir"/old/'*-*.*/*-*'/config

  # Clear the expression if there are no matches.
  # glob returns the filename substitution pattern if there are no matches
  # because nonomatch is set.
  set ci_glob = `glob "$ci_oldexp"`
  if ("$ci_glob" == "$ci_oldexp") set ci_oldexp = ""

  # Set up the list of old config directories to use.
  # For each old config directory:
  #  1. Convert the path from $admin_dir/old/... to $config_top/...
  #  2. Skip if $config_top/$product/$oshead does not exist.
  #     The product installation directory does not exist.
  #  3. Skip if $config_top/$product/$oshead/config/system.dat exists.
  #     Configuration will be handled normally.
  # For each directory in the list, $product/$oshead exists in both
  # $admin_dir/old and $config_top and $product/share may not exist in
  # $config_top (use the version in $admin_dir/old).
  foreach ci_olddir ($ci_oldexp)
    set ci_dir = `echo $ci_olddir | sed -e "s;^$admin_dir/old;$config_top;"`
    if (! -d $ci_dir:h) continue
    if (-e $ci_dir/system.dat) continue
    set ci_olddirs = "$ci_olddirs $ci_olddir"
  end
endif

# Set up the directory list and array initialization string.
# Included directories must satisfy the following:
#  1. The file $product/$oshead/config/system.dat must exist.
#  2. The file $product/share/config/product.dat must exist.
#  3. The directory must be of the form .../ $product/$oshead/config.
# Nothing will be done if the directory expressions are empty.
set ci_exit = 1
set ci_dirs = ""
set ci_init = ""
foreach ci_dir ($ci_direxp $ci_olddirs)

  # Skip if the system file does not exist.
  set ci_file = $ci_dir/system.dat
  if (! -e $ci_file) continue

  # Check the system portion of the directory.
  source $ci_file
  set ci_base = $ci_dir:h
  if ("$ci_oshead" != $ci_base:t) continue

  # Skip if the product file does not exist.
  set ci_base = $ci_base:h
  set ci_file = $ci_base/share/config/product.dat
  if (! -e $ci_file) continue

  # Check the product portion of the directory.
  source $ci_file
  if ("$ci_product" != $ci_base:t) continue

  # Add to the directory list and array initialization string.
  set ci_dirs = "$ci_dirs $ci_dir"
  set ci_init = "$ci_init 0"
end

# Determine whether this is a distribution directory.
set ci_dist = 0
if (-e $config_top/admin/distribution) set ci_dist = 1

# Save the directory list, array initialization string, and distribution flag.
set config_dir${config_sfx} = ($ci_dirs)
set config_init${config_sfx} = "$ci_init"
set config_dist${config_sfx} = $ci_dist

# Initialize the size of the configuration arrays.
foreach ci_name ($ci_names)
  eval set $ci_name$config_sfx = '($ci_init)'
end

# Get the configuration information.
# Nothing will be done if the directory list is empty.
set ci_exit = 0
@ ci_i = 0
foreach ci_dir ($ci_dirs)
  @ ci_i ++

  # Initialize the temporary variables in case they are not set.
  foreach ci_name ($ci_names)
    set ci_$ci_name = 0
  end

  # Get the information for one product and system.
  source $ci_dir/system.dat
  set ci_base = $ci_dir:h
  source $ci_base:h/share/config/product.dat

  # If the installation date is empty, set it to the modification date
  # of the uninstall script.
  if ("$ci_date_install" == "") set ci_date_install = "$ci_date"

  # Save the information in the configuration arrays.
  foreach ci_name ($ci_names)
    eval set $ci_name$config_sfx'[$ci_i] = "$'ci_$ci_name'"'
  end
end

# Restore the state of nonomatch.
if (! $ci_nonomatchset) unset nonomatch

# Return.
exit

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

source $admin_dir/abort
