#****************************************************************************
#  ##   ##         #####   #####  ##     **        NoSQL RDBMS - rmcol      *
#  ###  ##        ####### ####### ##     **      $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.                                *
#****************************************************************************
#
#  Remove one or more columns from a table.
#
#  Removes the specified columns from a table. Nonexisting columns
#  specified on the command line are silently ignored. If only invalid
#  columns are specified, then nothing is deleted.
#
#  This NoSQL operator reads a table from STDIN and writes an
#  table to STDOUT.
#
########################################################################

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

BEGIN \
{
  NULL = ""; FS = OFS = "\t";
  split( __nosql_args, command_cols, " " )
}

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

NR == 1 \
{
  # Start building the back-end awk program.

  awkpgm = "'BEGIN{FS=OFS=\"\\t\";}"

  # 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; good_cols++ }
  }

  # Now remove unwanted columns.
  for ( i in command_cols )
  {
	if ( P[ command_cols[i] ] != NULL )
	{
	  delete P[ command_cols[i] ]
	  # Exit if all columns have been removed.
      if ( --good_cols == 0 )  exit
	}
  }

  # Build the list of columns, in awk format.
  for ( i = 1; i <= NF; i++ )
  {
	if ( P[$i] != NULL )
	{
	  out_rec = out_rec OFS $i
	  fieldlist = fieldlist $i "=$(" P[$i] ");"
	  out_list = out_list "," $i
	}
  }

  # Remove leading extra comma from out_list.
  sub( /^,/, NULL, out_list )

  # Remove leading extra OFS from out_rec, then print header and dashline.
  sub( /^\t/, "", out_rec ); print out_rec
  gsub( /[^\t]/, "-", out_rec ); print out_rec

  # Make sure the header is printed before calling awk(1) again.
  fflush()

  awkpgm = awkpgm "{" fieldlist "print " out_list ";}'"

  unix_cmd = "${NSQAWK:-awk} " awkpgm
  next
}

# Dashline
NR == 2 { next }

{ print |unix_cmd }

