#! /bin/csh -f
#
#/*****************************************************************************
#                Copyright Carnegie Mellon University 1992
#
#                      All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of CMU not be
# used in advertising or publicity pertaining to distribution of the
# software without specific, written prior permission.
#
# CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
# CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.
#*****************************************************************************/
#
#
# $Header: iffstats,v 1.4 91/07/25 01:32:16 heydon Exp $
#
# Written by Allan Heydon for the Miro project at Carnegie Mellon
#
# SYNTAX
#   iffstats [-a|-A] [-g|-G] [-t box-type-file] [file] ...
#
# SYNOPSIS
#   Reads each of the specified IFF file(s) (or stdin if none is specified),
#   and prints statistics for each one in an easy-to-read format. See the awk
#   file iffstats.awk for details on which statistics are printed. If the '-a'
#   flag is specified, statistics on arrow incidence on user and file boxes is
#   printed. If the '-g' flag  is specified, statistics on box "geometry"
#   (in-degree, out-degree, and depth) are printed. The capitalized versions
#   of these options cause "histograms" of the respective data to be printed
#   as well.
#
#   The '-t' option can be used to specify the name of an IFF file containing
#   information on box-types. It defaults to the file 'unix-types.iff' named
#   below. If the specified box-type file is a relative path, both the current
#   directory and the "/usr/miro/libi/iff" directory are searched. The 'type'
#   attribute of every BOX entry must name one of the box-types named in the
#   box-type file.
#
# BUGS
#   The input IFF files must have all entries on one line each. Also, strings
#   and identifiers cannot contain ';' or '=' characters. All BOX entries must
#   appear in the file before any ARROW or INSIDE relations that refer to them.
#
# FILES
#   /usr/miro/libi/awk/iffstats.awk	gawk script that does all the work
#   /usr/miro/libi/iff			directory to search for box-type file
#   /usr/miro/libi/iff/unix-types.iff	default box-type file
#
# SEE ALSO
#   fs2iff(1), iff2ciff(1)

set awkfile = "/usr/miro/libi/awk/iffstats.awk"
set typedir = "/usr/miro/libi/iff/"
set typefile = "${typedir}unix-types.iff"

# parse command-line args
set a_flag = ""
set g_flag = ""
set error = 0
while ( $#argv > 0 )
  switch ( $argv[1] )
    case "-a":
      set a_flag = "+a"
      breaksw
    case "-A":
      set a_flag = "+A"
      breaksw
    case "-g":
      set g_flag = "+g"
      breaksw
    case "-G":
      set g_flag = "+G"
      breaksw
    case "-t":
      set typefile = $argv[2]
      shift argv
      breaksw
    case "-*":
      echo "unknown command-line flag '$argv[1]'"
      @ error++
      breaksw
    default:
      break;   # break out of while loop
  endsw
  shift argv
end

# process errors
if ( $error > 0 ) then
  echo 'SYNTAX: iffstats [-a|-A] [-g|-G] [-t box-type-file] [file] ...'
  exit(-1)
endif

# search for type file
if ( ! -e $typefile ) then
  set origfile = $typefile
  set error_flag = 1
  if ( $typefile !~ /* ) then
    set typefile = ${typedir}$typefile
    if ( -e $typefile ) set error_flag = 0
  endif
  if ( $error_flag == 1 ) then
    echo "Box-type file '${origfile}' could not be found."
    exit(-1)
  endif
endif

# invoke gawk script
if ( $#argv < 1 ) then
  echo "=== FILE: stdin ==="
  echo ""
  gawk -f $awkfile $a_flag $g_flag $typefile donetypes=1 -
else 
  while ($#argv > 0)
    echo "=== FILE:" $argv[1] "==="
    echo ""
    gawk -f $awkfile $a_flag $g_flag $typefile donetypes=1 $argv[1]
    shift argv
    if ($#argv > 0) echo ""
  end
endif
