#!/usr/bin/awk -f
#
# Compiles the given CSV into a table definition
#
#   Copyright (C) 2014-2023 Ryan Specialty, LLC.
#
#   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 3 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, see <http://www.gnu.org/licenses/>.
##


function columngen( header )
{
  # output a field constant for each field in the header
  i = 0
  while ( field = header[ ++i ] )
  {
    printf "    <t:table-column name=\"%s\" " \
        "index=\"%d\" seq=\"%s\" />\n",
        field,
        ( i - 1 ),
        ( seq[ i ] ) ? "true" : "false"
  }
}


function seqchk( last )
{
  # if there's no last row, then do not bother
  i = 0
  while ( i++ < NF )
  {
    if ( seq[ i ] == "" ) seq[ i ] = 1

    # this field is sequential if it is greater than or equal to the last field
    # (we don't check for descending [yet]); note that on the first check, last
    # will be empty and therefore this check will succeed (properly
    # initializing seq[i] to 1)
    seq[ i ] = seq[ i ] && ( $(i) >= last[ i ] )
  }
}


# header
BEGIN {
  rootpath = "../../../"
  file     = ARGV[1]

  # grab only the filename (remove all preceding directories and the file ext)
  name = gensub( /^.*\/|\.[^.]+$/, "", "g", file )


  # output package header
  printf \
    "<?xml version=\"1.0\"?>\n" \
    "<package\n" \
    "  xmlns=\"http://www.lovullo.com/rater\"\n" \
    "  xmlns:c=\"http://www.lovullo.com/calc\"\n" \
    "  xmlns:t=\"http://www.lovullo.com/rater/apply-template\"\n" \
    "  desc=\"%2$s rate table\">\n\n" \
    "  <!--\n" \
    "    WARNING: This file was generated by csv2xml; do not modify!\n" \
    "  -->\n\n" \
    "  <import package=\"/rater/core/base\" />\n" \
    "  <import package=\"/rater/core/vector/table\" />\n\n", \
    rootpath, name

  # the first row of the CSV is the header representing the column identifiers
  getline
  split( $0, header, /,/ )

  # table constant identifier
  tconst = toupper( gensub( /-/, "_", "g", name ) ) "_RATE_TABLE"

  # generate the header for the table constant
  printf "  <t:create-table name=\"%s\">\n", name

  printf "%s", "    <t:table-rows data=\"\n"

  # delimit fields by commas (the field separator for CSVs); note that this
  # won't work properly if strings contain commas
  FS = ","
}


# each row of the CSV
{
  # generate value string for each field
  i = 0
  while ( i++ < NF )
  {
    printf "%s", ( ( i > 1 ) ? "," : "" ) $(i)
  }

  print ";"

  seqchk( last )
  split( $0, last )
}


# footer
END {
  # end of table-rows node
  print "\" />"

  # columns can't be generated until after we know which ones represent
  # sequential data
  columngen( header )

  print "  </t:create-table>"
  print "</package>"
}
