#!/bin/sh
#
# Converts an rdbtable record to HTML3 table rows.
#

RCS_ID='$Id$'

my_name=$(basename $0)

while [ $# -ge 1 ] ; do
  case $1 in
    -h*) cat <<_EOH_

        NoSQL operator: ${my_name}

Usage:  ${my_name}  [options]  [rdbtable]

Options:
    -h      Print this help info.
    -i "L"  Blank-separated list of input fields. Only the table columns
            in list 'L' will be shown as input boxes in the output form.
            All the others will be read-only. 'L' must be one single token
            to the shell, so it must be quoted if it lists more than
            one column. The special word '(all)' means that all the
            columns are to be shown as input fields.
    -M nnn  Set maximum length for input fields (HTML 'maxlength' field
            attribute). If a table column is wider than 'nnn', then a
            textarea is displayed, otherwise an ordinary entry field is shown.
    -u      Prefix textarea field names with an underscore (_). This feature
            is meant for using with the 'uncgi' program. 
            (See http://hyperion.com/~koreth/uncgi.html for more details).

Converts an rdbtable row to HTML3 table rows. Each column in the rdbtable
is turned into a separated HTML table row. Only the last row of the input
rdbtable is used, regardless of whether the former contains more records. If
the input table has no records, then nothing is printed. Characters that are
special to the Unix shell must be escaped on the command line. ${my_name}
produces only the body of the HTML table; table heading and footing, as well
as any <FORM></FORM> tags, must be provided by the calling program.

Each HTML field is prefixed by a label (field name). The label is the first
blank-separated token of the corresponding column documentation text. If the
latter is blank then the rdbtable column name is used instead, followed by a
colon (:).
If more than one blank-separated words from the column documentation text
have to be used for an HTML field label, just escape blanks with the ASCII
sequence &#32; to form single tokens.  For instance, suppose that a column
documentation contains the phrase "Job description of the person", and you
want "Job description" to be the HTML field label, simply change the above
phrase into "Job&#32;description of the person" in the column documentation,
to make the first two words be one single token.

This operator reads an rdbtable from a file and prints the HTML output to
STDOUT. If no rdbtable is specified on the command line, then the input
table is read from STDIN.

An example of usage of this operator from within a shell script is :

                 ${my_name} -i 'NAME JOB' [rdbtable]

which means that only the columns NAME and JOB of 'rdbtable' will be displayed
as HTML entry fields, while all the others will be read-only text.


$RCS_ID

            ----------------------
NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.
This program comes with ABSOLUTELY NO WARRANTY; for details
refer to the GNU General Public License.

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., 59 Temple Place Suite 330, Boston, MA 02111-1307
USA.
            ----------------------

_EOH_
        exit 0
        ;;
	-i)    shift; i_list=$1; shift    ;;
	-u)    shift; uncgi=1             ;;
	-M)    shift; max_width=$1; shift ;;
    *)     break					  ;;
  esac
done

case ${1} in
  -*)
    echo "Usage: ${my_name} [options] [rdbtable]" >&2
    exit 1
    ;;
esac

[ "${i_list}" = '(all)' ] && i_all=1

awk 'BEGIN { NULL=""; FS="\t"; OFS=FS;
  # Get list of type="input" fields.
  split("'"${i_list}"'", i_list, " ")
  while( i_list[++c] != NULL ) f_type[i_list[c]] = "i"
}
# Table comments.
r==0 && $0 ~ /^ *#/ { next }
# Column names.
r==0 { split($0, c_names); r++; next; }
# Column definitions.
r == 1 {
  hdr_nr=NR; c=0;
  split($0,c_defs)
  while(c_defs[++c] != NULL) {
	a1 = substr(c_defs[c],1,index(c_defs[c]," "))
	gsub(/[^0-9]+/,NULL,a1)
	if( a1==NULL ) {
	  a1=c_defs[c]
	  c_width[c] = a1 + 0
	  c_label[c]=c_names[c] ":"
	}
	else {
	  # Turn length into a number.
	  c_width[c] = a1 + 0
	  a2 = substr(c_defs[c],index(c_defs[c]," "))
	  sub(/^ */,NULL,a2)
	  c_label[c] = a2
	}
  }
  r++; next;
}
END {
  if( hdr_nr == NR ) { exit } # No records in table, just quit.
  c=0
  while( ++c <= NF ) {
	out_rec = "<td>" c_label[c] "</td>"
	f_name = c_names[c]
    if('${i_all:-0}' || f_type[f_name] == "i" ) {
	  if( c_width[c] > '${max_width:=64}' ) {
		add_row = c_width[c] % '${max_width}'
		if(add_row > 0) add_row=1
		n_rows = int(c_width[c] / '${max_width}') + add_row
		# Uncgi handles a textarea specially if its name
		# starts with "_".
		if('${uncgi:-0}') f_name = "_" f_name
		out_rec = out_rec "<td><textarea name=\"" f_name "\" " \
		  "rows=\"" n_rows "\" cols=\""'${max_width}'"\">" \
		  $c "</textarea></td>"
	  }
	  else {
		out_rec = out_rec "<td><input type=text name=\"" \
		  f_name "\" size=\"" c_width[c] "\" maxlength=\"" \
		  c_width[c] "\" value=\"" $c "\"></td>"
	  }
	}
	else out_rec = out_rec "<td>" $c "</td>"
	print "<tr>" out_rec "</tr>"
  }
}' ${1}

exit $?

