#!/bin/sh
#
# Selects everything but the specified columns from an rdbtable.
#
# Author: Carlo Strozzi <carlos@linux.it>

RCS_ID='$Id$'

my_name=$(basename $0)

while [ $# -ge 1 ] ; do
  case $1 in
    -h*) cat <<_EOH_

        NoSQL operator: ${my_name}

Usage:  ${my_name}  [options] [expression]

Options:
    -help   Print this help info.
    -n      Strip header from output.
    -bs "XXX" Prepend "XXX" to each row of the output table body.
              For instance, "XXX" could be the "<TR><TD>" HTML tag.
    -ms "YYY" Use "YYY" to separate each column of the output table body.
              For instance, "XXX" could be the "</TD><TD>" HTML tag.
    -es "ZZZ" Append "ZZZ" to each row of the output table body.
              For instance, "XXX" could be the "</TD></TR>" HTML tag.

NOTE: Specifying non-null strings for either "-bs", "-ms" or "-es"
      will produce an invalid rdbtable on output, so "-n" should 
      be specified as well.

Takes a list of column names that are NOT to be selected and prints
the others to STDOUT.  Chars that are special to the UNIX shell must
be quoted. 

Column names are in the form 'column_1 column_2 ...'.
For example, to select everything except columns 'NAME' and 'JOB' from
the input rdbtable the statement is:

                        'NAME  JOB' 

This operator reads an rdbtable via STDIN and writes an rdbtable
via STDOUT.  If no column is specified on the command line, the whole
table is written to STDOUT. Non existen columns are silently ignored.


$RCS_ID

            ----------------------
NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.
This program comes with ABSOLUTELY NO WARRANTY; for details
refer to the GNU General Public License.

You should have received a copy of the GNU General Public License
along with this program;  if not, write to the Free Software
Foundation, Inc., 59 Temple Place Suite 330, Boston, MA 02111-1307
USA.
            ----------------------

_EOH_
        exit 0
        ;;
    -n) shift; no_hdr=1 ;;
	-bs) shift; bs=$1; shift ;;
	-ms) shift; ms=$1; shift ;;
	-es) shift; es=$1; shift ;;
    *)  break ;;
  esac
done

# Set default output field separator.
[ -z "${ms}" ] && ms="\t"

awk 'BEGIN { NULL = ""; FS = "\t" }
# Table comments.
r == 0 && $0 ~ /^ *#/ {
  if( "'${no_hdr}'" != 1 ) print
  next
}
# Column names.
r == 0 {
  # Get requested column names.
  split( "'"$*"'", c_names, " " )
  # Fill column name and position arrays.
  while( ++p <= NF ) {
	P[$p] = p
	N[p] = $p
	d = 0
	while( c_names[++d] != NULL ) {
	  if( $p == c_names[d] ) { 
		P[$p] = NULL
		N[p] = NULL
  		break
	  }
	}
  }
  if( "'${no_hdr}'" != 1 ) {
	while( ++c <= NF ) {
	  if( N[c] != NULL ) {
		if( out_rec == NULL ) out_rec = $c
		else out_rec = out_rec FS $c
	  }
	}
 	print out_rec
  }
  r++
  next
}
# Column definitions.
r == 1 {
  c = 0
  out_rec = NULL
  if( "'${no_hdr}'" != 1 ) {
	while( ++c <= NF ) {
	  if( N[c] != NULL ) {
		if( out_rec == NULL ) out_rec = $c
		else out_rec = out_rec FS $c
	  }
	}
	print out_rec
  }
  r++
  NR = 0
  next
}
# Table body.
{
  c = 0
  out_rec = NULL
  while( ++c <= NF ) {
    if( N[c] != NULL ) {
      if( out_rec == NULL ) out_rec = "'"${bs}"'" $c
      else out_rec = out_rec "'"${ms}"'" $c
    }
  }
  print out_rec "'"${es}"'"
}'

exit 0
