#!/bin/sh
#
# Converts an /rdb table format to /rdb list format.
#
# Original code: starbase 2.24a , "tabletolist"
#
# Adapted by Carlo Strozzi <carlos@linux.it>

RCS_ID='$Id: nsq-tabletolist,v 0.9b 1998/03/04 09:12:49 carlos Exp carlos $'

my_name=$(basename $0)

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

		NoSQL operator: ${my_name}

Usage:  ${my_name}  [options]  < input_table

Options:
	-h	Print this help info.

Converts an /rdb "table" to the corresponding /rdb "list" format.

This operator reads an /rdb table via STDIN and produces the "list"
version of the same table on STDOUT.

It is provided to help exchanging data between /rdb and NoSQL.

$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
			;;
		*) break ;;
	esac
done

awk '
BEGIN {  FS="	"
	OFS="	"
	ncol = table_header(V, C, D)

	print ""
}
{
	for ( i = 1; i <= ncol; i++ ) {
		print C[i], $i
	}
	print ""
}

function table_header(V, C, D,		header, dashes)
{
	getline	header
	getline	dashes
	if ( dashes !~ /(-+\t)*(-+)/ ) {
	     do {
		header = dashes	
	     } while ( getline dashes > 0 && dashes !~ /(-+\t)*(-+)/ )
	}

	n = split(header, C, /\t/)
	n = split(dashes, D, /\t/)

	return n
} '

