#!/bin/bash
# Compiles a "magic" CSV file into a normal CSV
#
#   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/>.
#
# For format of CSVMs, see `csvm-expand'.
#
# To disable sorting of CSVM output, use the `!NOSORT' directive before the
# header line.
##

set -o pipefail

# account for symlinks, since historically this script lives in a different
# directory and has been symlinked for compatibility
declare -r mypath=$( dirname "$( readlink -f "$0" )" )


# Generate -k arguments for GNU sort given a CSV header
#
# The generated arguments will be of the form -k1,1n ... -kl,ln, where `l'
# is the total number of header entries.
#
# For example, given this header:
#   foo, bar, baz
# the output would be:
#   -k1,1n -k2,2n -k3,3n
sort-key-args()
{
  local -r header="${1?Missing CSV header}"

  local -i i=0

  # generate -ki,in for each column (notice that a trailing
  # comma is added to the header because of the read delimiter)
  while read -d,; do
    echo -n "-k$((++i)),${i}n "
  done <<< "$header,"
}


# Sort every column of CSV
#
# The columns will all be sorted left-to-right.  The header is left in place
# as the first row.
csv-sort()
{
  # the first line of the expanded CSVM is the CSV header
  local header; read -r header
  local -r keys=$( sort-key-args "$header" )

  # all remaining input (which is now sans header) is sorted
  echo "$header"
  sort -t, $keys -
}


# Output usage information
#
# Kudos to you if you understand the little Easter egg.
usage()
{
  cat <<EOU
Usage: $0 [FILE]
Expand CSVM represented by FILE or stdin into a CSV

The columns of the expanded CSV will be automatically sorted
left-to-right.  To inhibit this behavior, use the \`!NOSORT'
directive anywhere before the header line in the source CSVM.

Options:
  --help  Output usage information.

This program has magic CSV powers.
EOU

  exit 64  # EX_USAGE
}


# Sort CSV rows left-to-right unless the `!NOSORT' directive is provided
main()
{
  test ! "$1" == --help || usage

  "$mypath/csvm-expand" "$@" \
    | {
      local directives; read -r directives

      # ignore sorting if given NOSORT directive
      if [[ "$directives" =~ NOSORT ]]; then
        cat
      else
        csv-sort "$sort"
      fi
    }
}

main "$@"
