#****************************************************************************
#  ##   ##         #####   #####  ##     **       NoSQL RDBMS - repair      *
#  ###  ##        ####### ####### ##     **      $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.                                *
#****************************************************************************
#
#  Append missing trailing tabs to data rows of a table.
#
#  Takes table rows that may lack trailing empty fields, and pads them
#  with OFS characters up to the No. of fields in the header.
#
#  Such broken tables are often the result of manipulating tables
#  with common spreadsheet programs.
#
# Options:
#     -F|--filler S
#           Instead of using NULL fields for padding, use whatever
#           is specified in string 'S'. This will make added fields
#           easy to spot visually.
#
#  This NoSQL operator reads a table from STDIN and writes the padded
#  table to STDOUT.
#
########################################################################

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

BEGIN \
{
  NULL = ""; FS = OFS = "\t";
  split( __nosql_args, args, " " )
  if ( args[1] == "-F" || args[1] == "--filler" ) filler = args[2]
}

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

NR == 1 \
{
  num_cols = NF
  print; gsub( /[^\t]/, "-" ); print; next
}

NR == 2 { next }

{
  printf( "%s", $0 )

  if ( NF < num_cols )
  {
	NF == 0 ? num_tabs = num_cols - 1 : num_tabs = num_cols
	for ( i = NF+1; i <= num_tabs; i++ ) printf( "\t%s", filler )
  }

  printf( "\n" )
}

