#****************************************************************************
#  ##   ##         #####   #####  ##     **      NoSQL RDBMS - maketable    *
#  ###  ##        ####### ####### ##     **        $Revision: 2.1 $			*
#  #### ##        ###     ##   ## ##     ************************************
#  #######  ####  #####   ##   ## ##     **      Carlo Strozzi (c) 1998     *
#  ####### ######   ##### ## # ## ##     ************************************
#  ## #### ##  ##     ### ##  ### ##     **           Written by            *
#  ##  ### ###### ####### ######  ###### **          Carlo Strozzi          *
#  ##   ##  ####   #####   #### # ###### **     e-mail: carlos@linux.it     *
#****************************************************************************
#   NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.                          *
#   This program comes with ABSOLUTELY NO WARRANTY; for details             *
#   refer to the GNU General Public License.                                *
#****************************************************************************
#
#  Builds a valid table header from a template file.
#
#  This NoSQL operator reads a template file from STDIN and prints the
#  corresponding table header to STDOUT.
#  A template file is very similar to a NoSQL 'list' file, except that
#  it only contains column names, without the associated values. Each
#  column name may optionally be preceeded by its positional number
#  in the table header, i.e.:
#
#  ##################################################
#  # Optional table comments
#  ##################################################
#  1  Name1     Any comments ...
#  2  Name2     Any comments ...
#  3  Name3     Any comments ...
#  ...
#
#  Field numbers are optional, and if they are present they must be
#  separated by the column names by blanks or tabs.
#  Column documentation comments are optional, and must be separated
#  by the column name by blanks or tabs.
#
########################################################################

########################################################################
# BEGIN block
########################################################################

BEGIN { NULL = ""; OFS = "\t"; }

########################################################################
# Main loop
########################################################################

# Skip empty and comment lines.
$0 ~ /^[ \t]*$/ || $0 ~ /^[ \t]*#/ { next }

{
  if ( $1 ~ /^[0-9]+$/ ) out_rec = out_rec OFS $2
  else out_rec = out_rec OFS $1
}

END \
{
  # Remove the extra leading tab.
  sub( /^\t/, NULL, out_rec )
  print out_rec
  gsub( /[^\t]/, "-", out_rec )
  print out_rec
}

