#!/usr/bin/mawk -We
# *********************************************************************
#  Written by and copyright Carlo Strozzi <carlos@linux.it>.
#
#  tabletosh: convert the last record of a NoSQL table into sh(1)
#	      variable assignments.
#  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-02-13 Ported to NoSQL v3
#  2001-02-21 Added option '-s'
#  2001-04-18 Added inline help
#  2001-08-17 Added stdio portability
#  2001-09-03 Added options '-C' and '-L'
#
#  $Id$
# *********************************************************************

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

  while (ARGV[++i] != NULL) {
    if (ARGV[i] == "-p" || ARGV[i] == "--prefix") prefix = ARGV[++i]
    else if ( ARGV[i] == "-c" || ARGV[i] == "--compute" ) comp = 1
    else if (ARGV[i] == "-t" || ARGV[i] == "--trim") trim = 1
    else if (ARGV[i] == "-T" || ARGV[i] == "--trunc") trunc = ARGV[++i]
    else if (ARGV[i] == "-K" || ARGV[i] == "--key") key = ARGV[++i]
    else if (ARGV[i] == "-R" || ARGV[i] == "--readonly") readonly = 1
    else if (ARGV[i] == "-e" || ARGV[i] == "--export") export = 1
    else if (ARGV[i] == "-i" || ARGV[i] == "--input") i_file = ARGV[++i]
    else if (ARGV[i] == "-o" || ARGV[i] == "--output") o_file = ARGV[++i]
    else if (ARGV[i] == "-s" || ARGV[i] == "--strip-names") {
         s_pattern = ARGV[++i]
    }
    else if (ARGV[i] == "-C" || ARGV[i] == "--max-cols") {
	 max_cols = ARGV[++i]
    }
    else if (ARGV[i] == "-L" || ARGV[i] == "--max-rows") {
	 max_rows = ARGV[++i]
    }
    else if (ARGV[i] == "-h" || ARGV[i] == "--help") {
	 system("grep -v '^#' @NOSQLPATH@/nosql/help/tabletosh.txt")
	 rc = 1
	 exit(rc)
    }
  }

  ARGC = 1					# Fix argv[]

  if (o_file == NULL) o_file = "@STDOUT@"
  if (i_file != NULL) { ARGV[1] = i_file; ARGC = 2 }
}

#
# Main loop                                                                     
#

# Column names.
NR == 1 {
  i = 0
  while (++i <= NF) {
    if (!P[$i]) {
      if (i == 1) auto_col = $i
      else auto_col = auto_col " " $i
    }

    if (pick_last) P[$i] = i
    else {
      if (!P[$i]) P[$i] = i
    }
  }
  split(auto_col, c_names, " ")
  next
}

# Dashline.
NR == 2 { next }

# Table body.
{
  if (key != NULL) {
     if ($1 == key) got_key = 1
     else next
  }  else {
     if (max_rows && NR > max_rows) next
  }

  # Make sure we output something valid in any case.
  if (!comp) printf("{ :; ") > o_file

  i = 0
  while (P[c_names[++i]]) {		# Process each field in turn.

    if (max_cols && i > max_cols) break

    field = $P[c_names[i]]

    if (trunc) field = substr(field,1,trunc)	# Truncate if requested.

    # Unescape tabs and newlines first.
    if (!comp) field = cNoSqlUnescape(field)

    if (trim) { sub(/^ +/, NULL, field); sub(/ +$/, NULL, field) }

    # Escape sh(1)/mawk(1) special characters in string.

    field = cMawkBug(field)		# Escape backslashes
    gsub(/"/, "\\\"", field)		# Escape double-quotes

    s_name = c_names[i]
    if (s_pattern != NULL) sub(s_pattern, NULL, s_name)

    if (!comp) {
       gsub(/\$/, "\\$", field)
       gsub(/`/, "\\`", field)

       if (export) printf(" export") > o_file
       printf(" %s%s=\"%s\";", prefix, s_name, field) > o_file
       if (readonly) printf(" readonly %s%s;", prefix, s_name) > o_file

    }  else {
       printf(" %s%s=\"%s\";", prefix, s_name, field) > o_file
    }
  }

  if (!comp) printf(" }") > o_file
  printf("\n") > o_file
}

END {
  if (rc) exit(rc)
  if ((NR < 3) || (key != NULL && !got_key)) {
    # No data rows in the input table; print empty assignments.

    # Make sure we output something valid in any case.
    if (!comp) printf("{ :;") > o_file

    i = 0
    while (P[c_names[++i]]) {
       if (max_cols && i > max_cols) break
       s_name = c_names[i]
       if (s_pattern != NULL) sub(s_pattern, NULL, s_name)

       if (!comp) {
	  if (export) printf(" export") > o_file
	  printf(" %s%s=\"%s\";", prefix, s_name, field) > o_file
	  if (readonly)
	      printf(" readonly %s%s;", prefix, s_name) > o_file
       }  else {
	  printf(" %s%s=\"%s\";", prefix, s_name, field) > o_file
       }
    }
    if (!comp) printf(" }") > o_file
    printf("\n") > o_file	# In case the output goes to a file.
  }
}

# *********************************************************************
# cNoSqlUnescape(string)
#
# Takes a string and translates any unescaped '\t' and '\n' strings into
# physical tabs and newlines respectively. Returns the converted string.
# *********************************************************************
function cNoSqlUnescape(s,		S,i,s_length,a,escaped) {
  s_length = split(s, a, "")
  s_length++				# Cope with s_length==1
  while (++i <= s_length) {
    if (a[i] == "\\" && !escaped) { escaped = 1; continue }
    if (a[i] == "n" && escaped) { S = S "\n"; escaped = 0; continue }
    if (a[i] == "t" && escaped) { S = S "\t"; escaped = 0; continue }
    if (escaped) { S = S "\\" a[i]; escaped = 0; continue }
    S = S a[i]
  }
  return S
}

# *********************************************************************
# cMawkBug(string)
#
# Takes a string and turns all '\' characters into their escaped form
# '\\'. Returns the escaped string. This could be done with just a gsub(),
# but mawk(1) has a bug that makes it behave differently from other awk
# implementations:
#
# gsub(/\\/, "\\\\", field)		# This works with both gawk(1)
#					# and the original nawk(1).
#
# gsub(/\\/, "\\\\\\", field)		# This works just with mawk(1),
#					# otherwise it produces more
#					# backslashes than necessary,
#					# which looks rather obvious.
#
# *********************************************************************
function cMawkBug(s,		a,i,j,S) {

   i = split(s, a, "\\")
   S = a[1]
   for (j = 2; j <= i; j++) S = S "\\\\" a[j]
   return S
}

#
# End of program.
#
