#!/bin/sh
#
# Prints various types of info taken from an rdbtable header.
#

RCS_ID='$Id$'

my_name=$(basename $0)

while [ $# -ge 1 ] ; do
  case $1 in
    -h*) cat <<_EOH_

        NoSQL operator: ${my_name}

Usage:  ${my_name}  [options]  [rdbtable]

Options:
    -w    Fields are printed on one sigle horizontal line.
          Default is to print them one per line of output.
    -h    Print this help info.
    -b    Escape blank spaces in column descriptions by changing them
          into the ASCII sequence "&#32;", to make sure each field is one
          single token to the shell.
    -n    Strip header comments from output.
    -t X  Type of information requested. 'X' can be one of:

          c : Print column names.
          D : Print the whole column definition line.
          d : Print column documentation fields only.
          l : Print field lengths only.
          n : Print only the No. of columns in the input table.
          t : Print field types only.

Prints various pieces of data, extracted from the table header lines.
Default is to print the whole table header to STDOUT.

This operator reads an rdbtable from a file and prints the requested info
to STDOUT. If no rdbtable is specified on the command line, then the input
table is read from STDIN.

$RCS_ID

            ----------------------
NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.
This program comes with ABSOLUTELY NO WARRANTY; for details
refer to the GNU General Public License.

You should have received a copy of the GNU General Public License
along with this program;  if not, write to the Free Software
Foundation, Inc., 59 Temple Place Suite 330, Boston, MA 02111-1307
USA.
            ----------------------

_EOH_
        exit 0
        ;;
    -w)    shift; one_line=1; no_hdr=1          ;;
    -n)    shift; no_cmts=1                     ;;
    -b)    shift; esc_blanks=1                  ;;
    -t)    shift; info_type=$1; no_hdr=1; shift ;;
    *)     break     ;;
  esac
done

case ${1} in
  -*)
    echo "Usage: ${my_name} [options] [rdbtable]" >&2
    exit 1
    ;;
esac

case ${info_type} in
  D)	;;
  d)	doc_only=1		;;
  l)	lengths_only=1	;;
  n)	count_only=1; c_names=1	;;
  t)	types_only=1	;;
  c)	c_names=1		;;
  -*)
	echo "Usage: ${my_name} [options] [rdbtable]" >&2
	exit 1
	;;
esac

awk 'BEGIN { NULL=""; FS="\t"; OFS=FS;
  if('${no_hdr:-0}' && !'${one_line:-0}') OFS="\n"
}
# Table comments.
r==0 && $0 ~ /^ *#/ {
  if(!'${no_cmts:-0}' && !'${no_hdr:-0}')
	print
    next
}
# Column names.
r==0 {
  if(!'${no_hdr:-0}') print
  else if('${c_names:-0}') {
	if('${count_only:-0}') print split($0,a)
    else {
	  if(!'${one_line:-0}') gsub(FS,"\n",$0)
      print
	}
    exit
  }
  r++; next;
}
# Column definitions.
r == 1 {
  if(!'${no_hdr:-0}') {print;exit;}
  else {
    split($0,c_defs )
    while(c_defs[++c] != NULL) {
	  a1 = substr(c_defs[c],1,index(c_defs[c]," "))
	  if( a1 == NULL) {
		a1 = c_defs[c]
		# Documentation fields should not be empty anyway,
		# otherwise "set - ..." from the shell will not
		# set the corresponding positional parameters.
		a2 = ":"
	  }
	  else a2 = substr(c_defs[c],index(c_defs[c]," "))
	  if('${lengths_only:-0}') {
	    gsub(/[^0-9]+/,NULL,a1)
	    out_rec = out_rec OFS a1
	  }
	  else if('${types_only:-0}') {
        sub(/[0-9]+ */,NULL,a1)
        gsub(/[<> ]/,NULL,a1)
	    if(a1==NULL) a1="S"
        out_rec = out_rec OFS a1
      }
      else if('${doc_only:-0}') {
	    sub(/^ */,NULL,a2)
		if('${esc_blanks:-0}') gsub(" ","\&#032;",a2)
	    out_rec = out_rec OFS a2
	  }
	  # Default is the whole definition line.
      else out_rec = out_rec OFS c_defs[c]
    }
    sub(/^[\t\n]/,NULL,out_rec)
    print out_rec
    exit
  }
}' ${1}

exit $?

