#! /bin/sh
#
# @(#)dispcfgtab	1.2	LPS_UNX_COM	02/19/95
#
# Copyright 1994   Digital Equipment Corporation, Maynard, MA
#
# dispcfgtab
#
# This script presents a formatted display on stdout of the specified
# table of configuration variables.
#
# NOTE:  Due to the special display requirements, there is a
#        VERY TIGHT BINDING between this script and the format
#        of variable display as done by the showobjvars script!
#
# Parameters:
#    $1 - Path of the file containing the configuration variables,
#         typically as extracted from a printer configuration file.
#    $2 - Type of table to display ("communities" or "traps")
#    $3 - Optional boolean value to display a header ("true" or "false")
#
# Global variables:
#    TMPDIR
#
# Notes:
#
#    If the path is null, or if the file specified by the path is empty,
#    then a special display is produced; in either case, this is still
#    considered success.
#
# Exit values:
#    0 - Success, the table (or equivalent) was displayed on stdout.
#   >0 - Failure of some kind; should be rather rare, such as a failure
#        to write a file to the $TMPDIR directory, etc.
###

TABLEFILE=$1
TABLETYPE=$2
HEADINGS=$3

# Determine if the path is null or the file is empty

NOTABLE=true

if [ "$TABLEFILE" -a -f "$TABLEFILE" ]
then
    if [ `cat $TABLEFILE | wc -l` -ne 0 ]
    then
	NOTABLE=false
    fi
fi

# The awk command definition used to display the table
# in "indexed" form, where the index value is a lowercase
# alpha char starting with "a".

awkcmd='
BEGIN { entries = 0 ; max1 = 0 ; max2 = 0 ; pfmt = "NotSetYet" }

/^[ \t]*#/ { next }  # Skip comment lines
/^$/       { next }  # and blank lines

{ name = $1
  addr = $2
  if ( type == "communities") access = $3 ; else access = ""

  if (pass == 1)
  {
      i = length(name) ; if (i > max1) { max1 = i }
      i = length(addr) ; if (i > max2) { max2 = i }
  }
  else
  { 
      if (pfmt == "NotSetYet")	# Only do this stuff ONCE
      {
	  if (headings == "true")
	  {
	      hdr1 = "Community"
	      hdr2 = "IP Address"

	      i = length(hdr1) ; if (i > max1) { max1 = i }
	      i = length(hdr2) ; if (i > max2) { max2 = i }

	      hfmt = "            %-" max1 "s  %-" max2 "s  %-s\n"

	      if (type == "communities")
	      {
		  hfmt    = "            %-" max1 "s  %-" max2 "s  Access\n"
		  ndashes = max1 + 2 + max2 + 8  # include "  Access"
	      }
	      else  # for "traps"
	      {
		  hfmt = "            %-" max1 "s  %-" max2 "s\n"
		  ndashes = max1 + 2 + max2
	      }
	      printf (hfmt, hdr1, hdr2)

	      hfmt = ""

	      for (i = ndashes; i > 0; i--) hfmt = hfmt "-"
	      printf ("            %s\n", hfmt)

	      pfmt = "        %2d)  %-" max1 "s  %-" max2 "s  %-s\n"
	  }
	  else  # Simple display, no extra stuff
	  {
	      pfmt = "        %-" max1 "s  %-" max2 "s  %-s\n"
	  }

      }

      if (headings == "true")
      {
	  entries++

	  printf (pfmt, entries, $1, $2, $3)
      }
      else
      {
	  printf(pfmt, $1, $2, $3)
      }
  }
}
'

# If we have nothing to display, then display that fact;
# otherwise, hack on the contents of the file to produce
# a "blocked" formatted and display it.

if $NOTABLE
then
    echo "          <none specified>"
else
    awk "$awkcmd" headings=$HEADINGS type=$TABLETYPE \
	pass=1 $TABLEFILE pass=2 $TABLEFILE
fi

exit 0
