#!/usr/bin/env ruby
#
# sort(1) helper which allows custom sort orders based on string/regexp matching.
# Prefixes each line with a given value according to whether the line or a field
# within it matches a string or regexp.

require "optparse"
require "open3"

def parse_options
  delimiter = nil
  unmatched = "Z"

  parser = OptionParser.new do |opts|
    opts.banner = <<~EOF
      Usage: csort [options] PATTERN VALUE [PATTERN VALUE [...]]

    EOF
    opts.on("-t", "--field-separator SEP", String,
            "set field delimiter [whitespace]") do |d|
      delimiter = d
    end
    opts.on("-u", "--unmatched STR", String,
            "set unmatched sort value [#{unmatched}]") do |v|
      unmatched = v
    end
  end

  # ugh, why doesn't OptionParser allow setting a footer??
  def parser.help
    super + <<~EOF

      Pattern formats:
        /regexp/   - matches if the line contains the (non-anchored) regexp
        N:/regexp/ - matches if the Nth field in the line contains the (non-anchored) regexp
        N=string   - matches if the Nth field in the line is equal to the given string
        string     - matches if the line is equal to the given string
    EOF
  end

  parser.parse!

  if ARGV.length == 0
    abort "Must have at least one pattern and value."
  end

  if ARGV.length.odd?
    abort "Each pattern must have a corresponding value."
  end

  sort_map = []
  (0..ARGV.length - 1).step(2).each do |i|
    matcher = interpret_pattern(ARGV[i], delimiter)
    value = ARGV[i + 1]
    sort_map.push [matcher, value]
  end

  return delimiter, unmatched, sort_map
end

def interpret_pattern(pattern, delimiter)
  case pattern
  when %r{^/(.+)/$}
    re = Regexp.new($1)
    proc { |line| line =~ re }
  when %r{^(\d+):/(.+)/$}
    fieldnum = $1.to_i - 1
    re = Regexp.new($2)
    proc do |line|
      fields = delimiter ? line.split(delimiter) : line.split
      fields[fieldnum] =~ re
    end
  when /^(\d+)=(.+)$/
    fieldnum = $1.to_i - 1
    str = $2
    proc do |line|
      fields = delimiter ? line.split(delimiter) : line.split
      fields[fieldnum] == str
    end
  else
    proc { |line| line.chomp == pattern }
    #abort "Couldn't interpret pattern '#{pattern}'"
  end
end

def line_prefix(line, sort_map)
  sort_map.each do |matcher, value|
    return value if matcher.call(line)
  end
  nil
end

def main
  delimiter, unmatched, sort_map = parse_options

  $stdin.each_line do |line|
    prefix = line_prefix(line, sort_map) || unmatched
    puts prefix + (delimiter || " ") + line
  end
end

main
