#!/usr/bin/mawk -We
# *********************************************************************
# islist: check that a file has a valid NoSQL 'list' format.
# Copyright (c) 1998,2006 Carlo Strozzi
# 
# 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; version 2 dated June, 1991.
#
# 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: islist,v 1.7 2006/03/10 11:26:13 carlo Exp $

BEGIN {
  NULL = ""; 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] == "-v" || ARGV[i] == "--verbose") verbose = 1
    else if (ARGV[i] == "-E" || ARGV[i] == "--edit") edit = 1
    else if (ARGV[i] == "-i" || ARGV[i] == "--input") ifile = ARGV[++i]
    else if (ARGV[i] == "-a" || ARGV[i] == "--allow-duplicates") dups = 1
    else if (ARGV[i] == "-r" || ARGV[i] == "--relaxed") relaxed=dups=1
    else if (ARGV[i] == "-h" || ARGV[i] == "--help") {
       system("grep -v '^#' " nosql_install "/help/islist.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)
    }
  }

  ARGC = 1					# Fix argv[]

  if (ifile != NULL) { ARGV[1] = ifile; ARGC = 2 }
}

#
# Main loop
#

{ r_length = length($0) }

# First record must be a single newline.
NR == 1 {
  if (r_length) {
    if (verbose) 
      print "islist: first line must be" \
        " a newline with no data" > stderr
    if (edit) print "islist: " NR
    exit(errors=1)
  }
}

NR > 2 && /^\t/ { next }		# This is a continuation line

!r_length {				# This is a separator line.
  if (++col_num < 3) row_size = num_cols
  else if (num_cols != row_size) {
    if (verbose) print "islist: the input block ending at line " NR \
      " has a different No. of rows than its predecessors" > stderr

    if (edit) print "islist: " NR
    exit(errors=1)
  }
  num_cols = 0
  delete col_names
}

/^[_A-Za-z]/ {				# This is a field name.
  if (!relaxed && $0 !~ /^[A-Za-z]/) {
     if (verbose)
       print "islist: invalid column name '" $1 "'" > stderr

     if (edit) print "islist: " NR
     exit(errors=1)
  }
  num_cols++
  if (dups || col_names[$1] == "") col_names[$1] = $1
  else {
    if (verbose)
      print "islist: duplicated column name '" $1 "'" > stderr

    if (edit) print "islist: " NR
    exit(errors=1)
  }
}

# Last record must be a single newline.
END {
  if (rc) exit(rc)
  if (!errors) {
    if (r_length) {
      if (verbose) 
        print "islist: last line must be" \
          " a newline with no data" > stderr

      if (edit) print "islist: " NR
      exit 1
    }

    if (!row_size) {
      print "islist: no valid data in input file" > stderr
      exit 1
    }
  }
  if (verbose && !errors) {
    if (edit) print "edittable: table ok" > stderr
    else print "islist: list ok" > stderr
  }
}

# EOF
