#!/usr/bin/mawk -We
# *********************************************************************
# tabletoperlrdb: converts tables from nosql to the Perl rdb format
#
# Copyright (c) 2005 James A. Hart
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# *********************************************************************
# $Id: tabletoperlrdb,v 1.1 2006/01/10 17:16:59 carlo Exp $
# *********************************************************************
#
#    Read the file twice. The first time to figure out what the field
#    types and sizes are. The second time to write out the new table.
#
BEGIN {
  nmbrRE = "^[0-9.+-]+$"
  monthRE = "^(" "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec|" \
   "January|February|March|April|May|June|July|August|September|October|" \
   "November|December|" \
  "JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|" \
   "JANUARY|FEBRUARY|MARCH|APRIL|MAY|JUNE|JULY|AUGUST|SEPTEMBER|OCTOBER|" \
   "NOVEMBER|DECEMBER" ")$"

  NULL = "" ;  FS = OFS = "\t"

  # Get local settings.
  nosql_install = ENVIRON["NOSQL_INSTALL"]
  stdout = ENVIRON["NOSQL_STDOUT"]
  stderr = ENVIRON["NOSQL_STDERR"]

  # Set default values if necessary.
  if (nosql_install == NULL) nosql_install = "/usr/local/nosql"
  if (stdout == NULL) stdout = "/dev/stdout"
  if (stderr == NULL) stderr = "/dev/stderr"

  while (ARGV[++i] != NULL) {
    if (ARGV[i] == "-h" || ARGV[i] == "--help") {
       system("grep -v '^#' " nosql_install "/help/tabletoperlrdb.txt")
       exit(rc=1)
    }
    else if (ARGV[i] == "--show-copying") {
       system("cat " nosql_install "/doc/COPYING")
       exit(rc=1)
    }
    else if (ARGV[i] == "--show-warranty") {
       system("cat " nosql_install "/doc/WARRANTY")
       exit(rc=1)
    }
    else if (ARGV[i] ~ /^-/) {
       system("grep -v '^#' " nosql_install "/help/tabletoperlrdb.txt")
       exit(rc=1)
    }
    else {
      i_file = ARGV[i]
      if(ARGV[++i] != NULL) o_file = ARGV[i]
    }
  }

  ARGC = 1					# Fix argv[]

  if (o_file == NULL) o_file = stdout
  if (i_file != NULL) { 
	# first pass; determine field types and sizes
	nr = 0
	while((getline < i_file) > 0) {
		nr++
		if(nr == 1) {
			for(i=1;i<=NF;i++) {
				ftype[i] = "N"
			}
		} else {
			for(i=1;i<=NF;i++) {
				if(length($i) > flen[i]) flen[i] = length($i)
				if($i ~ monthRE){
					if(nr == 2) {ftype[i] = "M"}
				} else if($i !~ nmbrRE) ftype[i] = ""
			}
		}
	}
	close(i_file)
	nr = 0
	while((getline < i_file) > 0) {
		nr++
		if(nr == 1) {
			gsub(/\001/, "")		# remove SOH markers
			gsub(/ +/, "")		# trim blanks in names
			print > o_file		# column names
			for(i=1;i<=NF;i++) fdef = fdef \
                          ( flen[i] > 80 ? 80 : flen[i] ) \
                          ftype[i] "\t"
			sub(/\t$/,"",fdef)
			print fdef > o_file
		} else { print > o_file }
	}
  }
}
