#!/usr/bin/awk -f
#****************************************************************************
#  ##   ##         #####   #####  ##     **        NoSQL RDBMS - ddsh       *
#  ###  ##        ####### ####### ##     **        $Revision: 1.1.1.1 $       *
#  #### ##        ###     ##   ## ##     ************************************
#  #######  ####  #####   ##   ## ##     **   Carlo Strozzi (c) 1998-2000   *
#  ####### ######   ##### ## # ## ##     ************************************
#  ## #### ##  ##     ### ##  ### ##     **           Written by            *
#  ##  ### ###### ####### ######  ###### **          Carlo Strozzi          *
#  ##   ##  ####   #####   #### # ###### **     e-mail: carlos@linux.it     *
#****************************************************************************
#   NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.                          *
#   This program comes with ABSOLUTELY NO WARRANTY; for details             *
#   refer to the GNU General Public License.                                *
#****************************************************************************
# NOTE: to edit, set ts=8 in 'vi' (or equivalent)
#       to print, pipe through 'pr -t -e8'
#****************************************************************************
#
# NAME
#        ddsh - print shell assignments based on table header.
#
# SYNOPSIS
#        ddsh < table
#
#        Note: options must be passed through the environment 
#        variable _awk_args, i.e.:
#
#        _awk_args='[options]'
#
#
# DESCRIPTION
#
#     Prints the column names/positions in the form of Bourne-shell
#     assignments 'Name=position'.
#
# OPTIONS
#     -p|--prefix P
#          Prefix each output variable name with string 'P'.
#
#     -e|--export
#          Print the necessary statements to export each output  
#          assignment to the environment.
#
#     -r|--readonly
#          Print the necessary statements to make each output assignment
#          read-only.
#
#     -R|--Rc
#	   Print the output assignments in a format suitable to
#	   be eval'ed by rc(1), the Plan 9 Shell. The default is
#	   to prin the assignments in sh(1) format. If this option
#	   is specified it will override both '-e' and '-r', as they
#	   are meaningless with rc(1).
#
#     -a|--awk
#          Print the assignments in a for suitable to be passed to awk(1),
#          i.e. '-v Name=position'.
#
#     -l|--last
#          If the input table contains duplicated column names
#          pick the last occurrence of each. The default is to
#          pick the first one. This is sometimes useful after
#          the 'join' operator.
#
########################################################################

BEGIN {
  NULL = ""; FS = OFS = "\t"; split( ENVIRON["_awk_args"], args, " " )

  while ( args[++i] != NULL )
  {
    if ( args[i] == "-p" || args[i] == "--prefix" ) prefix = args[++i]
    else if ( args[i] == "-e" || args[i] == "--export" ) export = "export "
    else if ( args[i] == "-r" || args[i] == "--readonly" ) readonly = 1
    else if ( args[i] == "-R" || args[i] == "--Rc" ) out_fmt = "Rc"
    else if ( args[i] == "-a" || args[i] == "--awk" ) out_fmt = "awk"
    else if ( args[i] == "-l" || args[i] == "--last" ) pick_last = 1
  }

  # Now do any necessary overrides among the supplied options.
  if ( out_fmt == "awk" || out_fmt == "Rc" ) {
     # Reset options that are incompatible with formats other than Bourne.
     export = NULL
     readonly = 0
  }
  else out_fmt = "sh"			# Default.
}

########################################################################
# Main loop
########################################################################

NR == 1 {
  if ( out_fmt == "sh" ) printf("{ :;")
  else if ( out_fmt == "Rc" ) printf("{ ")

  # Load the table header.
  i = 0
  while ( ++i <= NF )
  {
    if ( pick_last ) P[$i] = i
    else
    {
      if ( ! P[$i] ) P[$i] = i
    }
  }

  i = 0
  while ( ++i <= NF )
  {
    if ( out_fmt == "awk" ) printf(" -v")

    printf(" %s%s%s=%s", export, prefix, $i, P[$i])

    if ( out_fmt != "awk" )
    {
      printf(";")
      if ( readonly ) printf(" readonly %s%s;", prefix, $i)
    }
  }
  if ( out_fmt != "awk" ) printf(" }")
}

NR > 1 { exit }			# Skip the rest of the input table.

########################################################################
# End of program.
########################################################################

