#!/usr/bin/mawk -We
# *********************************************************************
# tablecat: concatenate multiple NoSQL tables and print them to stdout
# in the form of one larger table. The input tables must all have the
# same No. of columns.
#
# Copyright (c) 2001,2007 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; 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$

BEGIN {
  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] == "-N" || ARGV[i] == "--no-header") no_hdr = 1
    else if (ARGV[i] == "-f" || \
		ARGV[i] == "--first-table") h_table = ARGV[++i]
    else if (ARGV[i] == "-p" || \
		ARGV[i] == "--path-elements") path_cols = ARGV[++i]
    else if (ARGV[i] == "-h" || ARGV[i] == "--help") {
       system("grep -v '^#' " nosql_install "/help/tablecat.txt")
       exit(rc_=1)
    }
    else argv[++j] = ARGV[i]
  }

  # Move argv[] back onto ARGV[].

  while (++k <= j) { ARGV[k] = argv[k] }

  ARGC = k				# Set the new arg count.
}

# Main loop

FNR == 1 {

   tmp = FILENAME

   # Make sure spurious left-hand side and middle "/"
   # characters in the file path are stripped,

   sub(/^[.\/]*\/+/,"",tmp)
   gsub(/\/+/,"/",tmp)

   # Dunno whether newlines can occur in filenames, but just in case.
   # Any forbidden characters are simply turned to spaces without taking
   # the trouble to escape them, as I think it is more appropriate here,
   # given the purpose of path-mapped table attributes.

   gsub(/[\n\t ]+/," ",tmp)

   x = split(tmp,b,"/")

   # Revert order of path components, so that '1' is the filename
   # itself, '2' the uplevel directory if any, and so on. I have
   # numbered the path components from right to left because it
   # is usually not known what a filepath is relative to in the
   # filesystem tree, and proceeding from right to left is the
   # only safe bet.

   i=1; delete c
   do {
	c[i++] = b[x--]
   }  while (x)
}

NR == 1 {

   # Remove any garbage surrounding the path argument
   # and make sure it contains only [1-9,]

   gsub(/[^1-9]+,+[^1-9]*/,NULL,path_cols)
   gsub(/[^1-9,]+/,NULL,path_cols)

   k = split(path_cols,a,",")

   if (no_hdr || h_table != NULL && FILENAME != h_table) next

   printf("%s",$0)

   for (j=1; j<=k; j++) if (a[j]) printf("\t\001_%d",j)

   printf("\n")
}

FNR > 1 {

   printf("%s",$0)

   for (j=1; j<=k; j++) if (a[j]) printf("\t%s",c[a[j]])

   printf("\n")
}

# End of program.
