#****************************************************************************
#  ##   ##         #####   #####  ##     **    NoSQL RDBMS - tabletoshell   *
#  ###  ##        ####### ####### ##     **        $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 single-record table to shell variable assignments.
#
# Usage:  nosql tabletoshell [options] < table
# 
# Options:
#     -t|--truncate
#           Simply truncate the 'value' part of each assignment at the
#           first occurrence of the single-quote char ('), instead of
#           translating the latter to '&#39;'. See explanations below.
#
#     -p|--prefix P
#           Prefix each output variable (column) name with string 'P'.
# 
# Converts a single-record table to shell variable format (VARIABLE='value'),
# handy for grabbing a table row into a shell program. If the table
# contains more than one row of data, then the assignments will be those of
# the last row. Any single-quotes in the 'value' part of the assignment are
# escaped with the ASCII sequence '&#39;', not to cause troubles to the
# invoking shell, or they are used simply as upper boundary of the 'value'
# part of the assignment if '-t' is specified.
# 
# This operator reads a table from STDIN and prints the requested info
# to STDOUT.
# 
# An example of usage of this operator from within a shell script is :
# 
#                  eval $(nosql tabletoshell < table)
# 
########################################################################

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

BEGIN \
{
  NULL = ""; FS = OFS = "\t"
  split( __nosql_args, args, " " )
  while ( args[++i] != NULL )
  {
	if ( args[i] == "-t" || args[i] == "--truncate" ) trunc = 1
	else
	{
	  if ( args[i] == "-p" || args[i] == "--prefix" ) prefix = args[++i]
	}
  }
}

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

# Column names.
NR == 1 { split( $0, c_names ); next; }

# Dashline.
NR == 2 { next }

END \
{
  if ( NR > 2 )
    while ( c_names[++c] != NULL )
	{
	  if ( trunc )
	  {
	    # Truncate at the first single quote, if any.
	    idx = index( $c, "'" )
	    if ( idx > 0 ) out_field = substr( $c, 1, idx-1 )
	    else out_field = $c
	  }
	  else
	  {
		gsub( "'", "\&#39;", $c )
		out_field = $c
	  }
	  print prefix c_names[c] "='" out_field "'"
	}
  else
  {
	while ( c_names[++c] != NULL )
	  print prefix c_names[c] "='" NULL "'"
  }
}

