#!/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 .
#
# 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 <