#!/usr/bin/mawk -We
# *********************************************************************
#  Written by and copyright Carlo Strozzi <carlos@linux.it>.
#
#  islist: check that a file has a valid NoSQL 'list' format.
#  Copyright (C) 1998-2001 Carlo Strozzi <carlos@linux.it>
# 
#  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.
#
#  2001-01-03 Ported to NoSQL v3
#  2001-03-09 Option '-e' changed to '-E'
#  2001-04-17 Added inline help.
#  2001-08-17 Added stdio portability
#
# *********************************************************************

BEGIN {
  NULL = ""; OFS = "\t"

  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") sIfile = ARGV[++i]
    else if (ARGV[i] == "-h" || ARGV[i] == "--help") {
       system("grep -v '^#' @NOSQLPATH@/nosql/help/islist.txt")
       rc = 1
       exit(rc)
    }
  }

  ARGC = 1					# Fix argv[]

  if (sIfile != NULL) { ARGV[1] = sIfile; 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@"
    errors = 1
    if (edit) print "islist: " NR
    exit 1
  }
}

NR > 2 && /^ / { 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@"

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

/^[A-Za-z]/ { num_cols++ }		# This is a field name.

# 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@"

      errors = 1
      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@"
  }
}

#
# End of program.
#
