#!/bin/sh
#
# Converts an /rdb table to NoSQL format.
#
# Author: Carlo Strozzi <carlos@linux.it>

RCS_ID='$Id: nsq-r2n,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 NoSQL format. Table header comments are
NOT preserved.

This operator reads a NoSQL table via STDIN and produces the /rdb
version of the same table on STDOUT.

Column names are preserved across the conversion.
Column types/widths cannot be desumed from the input /rdb table
and therefore they are computed dinamically in an auto-adaptive manner.

If a different header is desired, possibly based on a pre-existing
NoSQL template file, then the one created by this script has to be chopped
off in a subsequent step, after this program has completed.

Only String and Numeric NoSQL data types are (usually) autodetected,
i.e. Month data types will be treated as strings. The environment
variable TMPDIR is honoured, if set, else temporary work files will
go to /tmp.

$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

tmp_body=${TMPDIR:-/tmp}/${my_name}.b.$$

trap "rm -f ${tmp_body}" 0 1 2 15

awk -F"\t" -v tmp_body=${tmp_body} \
  'BEGIN { tbl_body = 0 ; col_defs = "" }
  tbl_body == 0  {
    if ( $0 ~ /^(-+\t*)+-+$/ ) {       # Look for the dash line.
      tbl_body = 1              # Found ! Start table body.
	  num_cols = split( header, c_names, "\t" )
	  for ( i = 1; i <= num_cols; i++ ) {
		col_w[i] = length( c_names[i] )	# Default column width.
		}
      print header
      for ( i = 1; i <= NF; i++ ) {
        col_t[i] = "N"		# Default data type is Number.
        }
      next
      }
    header = $0
    }
  tbl_body > 0  {	# Header auto-adaptation algorithm.
    for ( i = 1; i <= NF; i++ ) {
      c_width = length( $i )
      if ( c_width > col_w[i] )  col_w[i] = c_width
      if ( $i !~ /^[0-9]+$/  &&  $i != "" )
		  col_t[i] = ""  # Revert to type String.
      }
    print > tmp_body
    }
  END {
    for ( i = 1; i <= NF; i++ ) {
	  if ( col_w[i] > 16  &&  col_t[i] == "N" )  col_t[i] = ""
      col_defs = col_defs col_w[i] col_t[i]
      if ( i < NF )  col_defs = col_defs "\t"
      }
    print col_defs
    }'
  
[ -s ${tmp_body} ] && cat ${tmp_body}

