#!/usr/bin/env ruby
require 'fileutils'
require 'digest'

require_relative 'run.rb'

PROTOC_DOWNLOAD = {
  'osx' => {
    'url' => 'https://github.com/google/protobuf/releases/download/v3.1.0/protoc-3.1.0-osx-x86_64.zip',
    'sha256' => '2cea7b1acb86671362f7aa554a21b907d18de70b15ad1f68e72ad2b50502920e',
  },
  'linux' => {
    'url' => 'https://github.com/google/protobuf/releases/download/v3.1.0/protoc-3.1.0-linux-x86_64.zip',
    'sha256' => '7c98f9e8a3d77e49a072861b7a9b18ffb22c98e37d2a80650264661bfaad5b3a',
  }
}
PROTOC_DIR = '_support/protoc'

def main
  current_platform = platform
  unless current_platform
    abort "Platform #{RUBY_PLATFORM} is not yet supported by #{$0}"
  end

  # protoc Protobuf compiler
  fetch_protoc(PROTOC_DOWNLOAD[current_platform])
  FileUtils.rm_rf(PROTOC_DIR)
  FileUtils.mkdir_p(PROTOC_DIR)
  run!(%W[unzip #{File.expand_path(File.basename(PROTOC_DOWNLOAD[current_platform]['url']))}], PROTOC_DIR)

  # Go-specific libraries
  %w[proto protoc-gen-go].each do |pkg|
    run!(%W[go get -u github.com/golang/protobuf/#{pkg}])
  end

  run!(%w[bundle install --binstubs], '_support')
end

def platform
  case RUBY_PLATFORM
  when /darwin/ then 'osx'
  when /linux/  then 'linux'
  end
end

def fetch_protoc(source)
  return if sha_match?(File.basename(source['url']), source['sha256'])

  run!(%W[curl -LO #{source['url']}])
end

def sha_match?(file, sha)
  File.exist?(file) && Digest::SHA256.file(file).hexdigest == sha
end

main
puts 'done'
