#!/usr/bin/mawk -We
# *********************************************************************
#  Written by and copyright Carlo Strozzi <carlos@linux.it>.
#
#  trim: remove surrounding blanks from table columns.
#  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
#
# *********************************************************************
#
#  Usage: trim [options]
#
#  Options:
#      -m|--strip-all
#            Reduce multiple blanks between words to one single blank.
#
#      -e|--empty-rows
#            Remove empty rows from the input table.
#
#      -N|--no-header
#            Strip header from the output table.
#
#      -l|--lower-case
#            Convert data in the table body to lower-case.
#
#      -u|--upper-case
#            Convert data in the table body to upper-case
#            Note: If both '-l' and '-u' are given, the one that is
#	     specified last takes over.
#
#      -i|--input file
#           Read input from 'file' instead of the standard input.
#
#      -o|--output file
#           Write output to 'file' instead of the standard output.
#
#  Carries out several normalization tasks on the input table, like
#  stripping surrounding blanks from column values, converting data to
#  upper case or lower case, etc.
#
# *********************************************************************

# $Id: trim,v 1.1.1.1 2003/03/17 09:49:20 carlo Exp $

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

  while (ARGV[++i] != NULL) {
    if (ARGV[i] == "-m" || ARGV[i] == "--strip-all") strip_middle = 1
    else if (ARGV[i] == "-e" || ARGV[i] == "--empty-rows") rm_empty = 1
    else if (ARGV[i] == "-u" || ARGV[i] == "--upper-case") uc = 1
    else if (ARGV[i] == "-l" || ARGV[i] == "--lower-case") {
      lc = 1; uc = 0
    }
    else if (ARGV[i] == "-N" || ARGV[i] == "--no-header") no_hdr = 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]
  }

  ARGC = 1					# Fix argv[]

  if (o_file == NULL) o_file = "/dev/stdout"
  if (i_file != NULL) { ARGV[1] = i_file; ARGC = 2 }
}

#
# Main loop
#

NR < 3 {
  if (no_hdr) next
  sub(/^ */, NULL); sub(/ *$/, NULL); gsub(/ *\t */, OFS)
  print > o_file; next
}

rm_empty && /^[ \t]*$/ { next }

# Reduce middle blanks to one single blank if requested.
strip_middle { gsub(/ +/, " ") }

{ sub(/^ */, NULL); sub(/ *$/, NULL); gsub(/ *\t */, OFS) }

# Conver casing if requested.
uc { print toupper($0) > o_file; next }
lc { print tolower($0) > o_file; next }

{ print > o_file }

#
# End of program.
#
