require 'json'
require 'progress_bar'
require 'open-uri'
require 'digest/sha1'

class Array
  include ProgressBar::WithProgress
end

short_revs = `git rev-list master`.lines.map { |line| line.strip[0...4] }
if short_revs.length != short_revs.uniq.length
  raise "Short revisions collide, see 'git rev-list master'."
end

def create_card_map(pattern)
  branch = `git rev-parse --abbrev-ref HEAD`.strip
  return Dir[pattern].with_progress.map { |file|
    rev = `git rev-list #{branch} -1 -- #{file}`.strip[0...4]
    raise "Unversioned file: #{file}." if rev.empty?

    dbf_id = File.basename(file, '.png')
    dir = File.dirname(file)
    path = "#{rev}/#{dir}"
    hash = Digest::SHA1.base64digest(File.read(file))[0...5]

    [dbf_id, hash]
  }.to_h
end

package = JSON.parse(File.read('package.json'))
open('images.json', 'w') do |w|
  w.write(JSON.dump({
    config: {
      version: package['version'],
      base: 'https://raw.githubusercontent.com/schmich/hearthstone-card-images'
    },
    cards: {
      pre: create_card_map('pre/*.png'),
      rel: create_card_map('rel/*.png')
    }
  }))
end

require 'json'
require 'progress_bar'
require 'open-uri'
require 'digest/sha1'

class Array
  include ProgressBar::WithProgress
end

def create_card_map(pattern)
  branch = `git rev-parse --abbrev-ref HEAD`.strip
  return Dir[pattern].with_progress.map { |file|
    versioned = `git ls-files #{file}`.strip
    raise "Unversioned file: #{file}." if versioned.empty?

    id = File.basename(file, '.png')
    hash = Digest::SHA1.base64digest(File.read(file))[0...5]

    [id, hash]
  }.to_h
end

package = JSON.parse(File.read('package.json'))
open('images.json', 'w') do |w|
  w.write(JSON.dump({
    config: {
      version: package['version'],
      base: 'https://raw.githubusercontent.com/schmich/hearthstone-card-images'
    },
    cards: {
      pre: create_card_map('pre/*.png'),
      rel: create_card_map('rel/*.png')
    }
  }))
end

Remove and simplify checks now that we're using package.json versioning. Also, remove unused leftover values.
Remove and simplify checks now that we're using package.json versioning.
Also, remove unused leftover values.
