#****************************************************************************
#  ##   ##         #####   #####  ##     **     NoSQL RDBMS - listtotable   *
#  ###  ##        ####### ####### ##     **        $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.                                *
#****************************************************************************
#
#  Converts a table from 'list' to 'table' format.
#
#  Warning: if the input list contains duplicated entries, i.e. multiple
#  rows with the same label (column name), the resulting table may
#  be broken.
#
#  This NoSQL operator reads a table from STDIN and writes an
#  table to STDOUT.
#
##########################################################################

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

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

  # Get program options. 
  split( __nosql_args, args, " " )

  if ( args[1] == "-t" || args[1] == "--tabsize" )
  {
	tab_size = args[2]
	if ( tab_size !~ /[0-9]+/ ) tab_size = 4
	tab_size = "%" tab_size "s"
	padding = sprintf( tab_size, " " )
  }
}

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

NR == 1 { next }

$0 ~ /^[A-Za-z]/ && got_header == 0 \
{
  header == NULL ? header = $1 : header = header OFS $1
}

NF > 0  \
{
  if ( $0 ~ /^[A-Za-z]/ )
  {
	# This is a field name.
	if ( NF == 1 ) out_rec = out_rec OFS
	else
	{
	  # Remove field name.
	  sub( /^[^ \t]+[ \t]+/, NULL )

	  # Replace any tabs in the data part with tab_size blanks.
	  gsub( /\t/, padding )

	  out_rec = out_rec OFS $0
	}
  }
  else
  {
	# This is a continuation line.

	# Remove any leading tabs and/or spaces from continuation lines.
	# sub( /^[ \t]+/, NULL )
	# Replace any tabs in the data part with tab_size blanks.
	gsub( /\t/, padding )

    out_rec = out_rec " " $0 
  }
}

NF == 0 \
{
  if ( ! got_header )
  {
    got_header = 1
  	# Remove possible extra trailing tab from out_rec.
  	sub( /\t$/, NULL, header )
    print header
    gsub( /[^\t]/, "-", header )
    print header
  }

  # Remove possible extra leading tab from out_rec.
  sub( /^\t/, NULL, out_rec )

  # print out_rec unless it contains only tabs.
  if ( out_rec !~ /^\t+$/ ) print out_rec

  out_rec = NULL
}

