#!/usr/bin/mawk -We
# *********************************************************************
# envtotable: convert environment variables into a NoSQL table.
# 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: envtotable,v 1.4 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"

  if (ENVIRON["NOSQL_DEBUG"] == 1) debug = 1

  while (ARGV[++i] != NULL) {
    if (ARGV[i] == "-m" || ARGV[i] == "--match") m_pattern = ARGV[++i]
    else if (ARGV[i] == "-o" || ARGV[i] == "--output") o_file = ARGV[++i]
    else if (ARGV[i] == "-d" || ARGV[i] == "--delete") {
       remove = 1
       d_pattern = ARGV[++i]
    }
    else if (ARGV[i] == "-b" || ARGV[i] == "--blank") {
       blank = 1
       b_pattern = ARGV[++i]
    }
    else if (ARGV[i] == "-x" || ARGV[i] == "--debug") debug = 1
    else if (ARGV[i] == "-s" || ARGV[i] == "--strip-names") {
       strip_names = 1
       s_pattern = ARGV[++i]
    }
    else if (ARGV[i] == "-N" || ARGV[i] == "--no-header") no_hdr = 1
    else if (ARGV[i] == "-p" || ARGV[i] == "--prefix") prefix = ARGV[++i]
    else if (ARGV[i] == "-I" || ARGV[i] == "--ignore") i_pattern = ARGV[++i]
    else if (ARGV[i] == "-h" || ARGV[i] == "--help") {
       system("grep -v '^#' " nosql_install "/help/envtotable.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; ARGV[1] = NULL			# Fix argv[]

  if (o_file == NULL) o_file = stdout

  if (debug) {
     print "-I pattern: " i_pattern > stderr
     print "-m pattern: " m_pattern > stderr
     print "-d pattern: " d_pattern > stderr
     print "-b pattern: " b_pattern > stderr
     print "-s pattern: " s_pattern > stderr
  }

  if (m_pattern == NULL) m_pattern = "."	# catch all.
  if (i_pattern == NULL) i_pattern = "^$"	# skip nothing

  for (env in ENVIRON) {

      # skip non-matches and invalid NoSQL names.
      if (env ~ i_pattern || env !~ m_pattern || \
		env !~ /^[A-Za-z][A-Za-z0-9_]+$/) continue

      # Honour the '-s' switch.
      dd = env
      if (strip_names) gsub(s_pattern, NULL, dd)

      # In case we stripped the whole name.
      if (dd == NULL) continue

      dd = prefix dd

      if (j) col_names = col_names OFS "\001" dd
      else col_names = "\001" dd

      var = ENVIRON[env]

      # Honour '-d' and '-b' first.
      if (remove) gsub(d_pattern, NULL, var)
      if (blank) gsub(b_pattern, " ", var)

      # Escape NoSQL special characters in input values.
      gsub(/\n/, "\\n", var)
      gsub(/\t/, "\\t", var)

      if (j) { out_rec = out_rec OFS var; j++ }
      else { out_rec = var; j++ }
  }

  # Print column names.
  if (col_names == NULL) exit
  if (!no_hdr) print col_names > o_file

  # Print table body.
  print out_rec > o_file
}

# End of program.
