#****************************************************************************
#  ##   ##         #####   #####  ##     **      NoSQL RDBMS - setfirst     *
#  ###  ##        ####### ####### ##     **      $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.                                *
#****************************************************************************
#
#  Moves a specified column to the first position in table.
#
#  Takes a specified column and moves it to the first (leftmost)
#  position in table. The column that was in the first position is
#  exchanged with the former. If an invalid target column is specified,
#  then the input table is printed unchanged to STDOUT. The same
#  happens if no target column is specified. If option '-S string' is
#  specified, then the column moved to the 1st position is then
#  'normalized' as a potential key column. Normalization consists in
#  stripping leading and trailing blanks, and replacing multiple middle
#  blanks between words with the content of 'string'. This will usually
#  be one single character, like ':', ';' '/', etc., but it must not
#  contain TAB nor newline characters, or the resulting output table will
#  be broken. Characters that are special to the Unix shell must be
#  quoted or escaped in 'string'.
#
#  This NoSQL operator reads a table from STDIN and writes a
#  table to STDOUT.
#
########################################################################

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

BEGIN \
{
  NULL = ""; FS = OFS = "\t";
  num_args = split( __nosql_args, args, " " )
  for ( i = 1; i <= num_args; i++ )
  {
	if ( args[i] == "-S" || args[i] == "--subsep" )
	{
	  separator = args[++i]
	  key = 1
	}
	else col_name = args[i]
  }

  # Default separator is blank.
  if ( separator == NULL ) separator = " "
}

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

NR == 1 \
{
  # Load the column position array.
  while ( ++p <= NF )
  {
	# Make sure we pick the first occurrence of duplicated column
	# names (it may happen after a join).
	if ( P[$p] == NULL ) { P[$p] = p; N[p] = $p; num_cols++ }
  }

  # Now exchange the first column with the target column.
  for ( i = 1; i <= num_cols; i++ )
  {
	if ( N[i] == col_name )
	{
	  N[i] = N[1]
	  N[1] = col_name
	  break
	}
  }

  # Print the new table header. 
  out_rec = $P[ N[1] ]
  for ( i = 2; i <= num_cols; i++ ) out_rec = out_rec OFS $P[ N[i] ]

  print out_rec; gsub( /[^\t]/, "-", out_rec ); print out_rec; next
}

# Dashline
NR == 2 { next }

{ 
  out_rec = $P[ N[1] ]

  # Normalize the column to make it a key field.
  if ( key )
  {
	# Replace multiple middle blanks with the requested separator.
	gsub( / +/, separator, out_rec )

	# Strip leading and trailing blanks.
	sub( /^ +/, NULL, out_rec )
	sub( / $+/, NULL, out_rec )
  }

  for ( i = 2; i <= num_cols; i++ ) out_rec = out_rec OFS $P[ N[i] ]
  print out_rec
}

