Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: 76b7c468dfb602aa387a19db4184d895a7f16500 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
# Compiles a "magic" CSV file into a normal CSV
#
#   Copyright (C) 2018 R-T 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.
##

# 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 "$@"