#! /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: iffincidences,v 1.1 91/02/20 23:31:40 heydon Exp $
#
# Written by Allan Heydon for the Miro project at Carnegie Mellon
#
# SYNTAX
#   iffincidences [ -u num1 ] [ -f num2 ] [ -n | -N ] [ file ] ...
#
# SYNOPSIS
#   Process the IFF file 'file' (or the standard input if 'file' is given as
#   "-") representing an instance picture, and print the number of arrows
#   incident on each box; counts are given for each arrow type and in total.
#
#   By default, incidence information is printed for every box. If "-u num1"
#   is specified, only user boxes having >= 'num1' arrows (in total) incident
#   on them are printed. If 'num1' is 0, *no* user boxes are considered. The
#   "-f num2" option is treated similarly with respect to file boxes.
#
#   Also by default, the boxes are identified by sysname. If "+n" is
#   specified, the names of the boxes are printed. If "+N" is specified, the
#   *full* pathnames of the file boxes are printed (a full pathname is the
#   concatenation of names of boxes on *some* path from the box to a root).
#
# 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/iffincidences.awk	gawk script that does all the work
#
# SEE ALSO
#   fs2iff(1), iff2ciff(1), iffstats(1)

set awkfile = "/usr/miro/libi/awk/iffincidences.awk"

# parse command-line args
set u_flag = ""
set u_min = ""
set f_flag = ""
set f_min = ""
set n_flag = ""
while ( $#argv > 0 )
  switch ( $argv[1] )
    case "-u":
      set u_flag = "+u"
      shift argv
      set u_min = $argv[1]
      breaksw
    case "-f":
      set f_flag = "+f"
      shift argv
      set f_min = $argv[1]
      breaksw
    case "-n":
      set n_flag = "+n"
      breaksw
    case "-N":
      set n_flag = "+N"
      breaksw
    default:
      break
  endsw
  shift argv
end
if ( $u_min == "0" ) set u_min = ""
if ( $f_min == "0" ) set f_min = ""

# invoke gawk script
if ( $#argv < 1 ) then
  echo "=== FILE: stdin ==="
  echo ""
  gawk -f $awkfile $u_flag$u_min $f_flag$f_min $n_flag -
else 
  while ($#argv > 0)
    echo "=== FILE:" $argv[1] "==="
    echo ""
    gawk -f $awkfile $u_flag$u_min $f_flag$f_min $n_flag $argv[1]
    shift argv
    if ($#argv > 0) echo ""
  end
endif
