#!/usr/bin/env ruby

require 'json'

HELP_MESSAGE = <<~EOS
Usage: ./bin/format-snapshots JSON_ARRAY_OF_SNAPSHOTS
EOS

if ARGV.length != 1
  puts HELP_MESSAGE
  exit 1
end

urls = JSON.parse(ARGV[0])

snapshots = urls.reduce({}) do |hash, url|
  file_type = url.end_with?(".pdf") ? :pdf : :png
  page_size = url.include?("letter") ? :letter : :a4
  version = case url
            when /-release\./
              :release
            when /-current\./
              :current
            when /-diff\./
              :diff
            end

  hash[page_size] ||= {}
  hash[page_size][:"#{version}_#{file_type}"] = url
  hash
end

table = <<~TABLE
## PaperAge visual snapshots

Compare the output of the latest release version of PaperAge with the results from the current commit.

|  | Latest release | Current commit | Diff |
|---|---|---|---|
| A4 | [![A4 release](#{snapshots.dig(:a4, :release_png)})](#{snapshots.dig(:a4, :release_pdf)}) | [![A4 current](#{snapshots.dig(:a4, :current_png)})](#{snapshots.dig(:a4, :current_pdf)}) | ![A4 diff](#{snapshots.dig(:a4, :diff_png)}) |
| Letter | [![Letter release](#{snapshots.dig(:letter, :release_png)})](#{snapshots.dig(:letter, :release_pdf)}) | [![Letter current](#{snapshots.dig(:letter, :current_png)})](#{snapshots.dig(:letter, :current_pdf)}) | ![Letter diff](#{snapshots.dig(:letter, :diff_png)}) |

Encryption passphrase: `#{ENV['PAPERAGE_PASSPHRASE']}`

*Note: Snapshots are deleted after 30 days.*

TABLE

File.open('visual-snapshots.tmp', 'w') { _1.write(table) }

puts "Wrote visual-snapshots.tmp"
