#****************************************************************************
#  ##   ##         #####   #####  ##     **    NoSQL RDBMS - shelltotable   *
#  ###  ##        ####### ####### ##     **        $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 list of shell variable assignments into a one-record table.
#
# Usage:  nosql shelltotable < varlist
#
# Converts a list of shell variable assignments (VARIABLE=value), with
# multiple statements separated by newlines, into a NoSQL table.
# 
# This operator reads a newline separated list of VARIABLE=value pairs
# from STDIN and produces the corresponding rdbtable to STDOUT.
# 
# Example:         env | nosql shelltotable
# 
########################################################################

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

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

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

# Skip invalid lines.
{ 
  i = index( $0, "=" )
  if ( i < 2 ) next
  c_names[++c] = substr( $0, 1, --i )
  c_values[c] = substr( $0, i+2 )
}

END \
{
  c = 0
  out_rec = c_names[++c]
  while ( c_names[++c] != NULL ) out_rec = out_rec OFS c_names[c]
  if ( out_rec != NULL ) print out_rec
  else exit
  gsub( /[^\t]/, "-", out_rec ); print out_rec
  c = 0
  out_rec = c_values[++c]
  while ( c_names[++c] != NULL ) out_rec = out_rec OFS c_values[c]
  print out_rec
}

