# frozen_string_literal: true require 'pry'

# rubocop:disable LineLength namespace :proto do

desc 'Generate the Ruby code'
task :install do
  proto_version = '2.5.0'
  pwd = File.expand_path(Dir.pwd)

  FileUtils.mkdir_p("#{pwd}/lib/proto/proto")

  base_url = 'https://raw.githubusercontent.com/SteamRE/'\
                'SteamKit/master/Resources/Protobufs/'

  protos = {
    csgo: %w(
      steammessages
      engine_gcmessages
      cstrike15_gcmessages
      cstrike15_usermessages
      netmessages
      gcsdk_gcmessages
      gcsystemmsgs
    ),
    steamclient: %w(
      steammessages_base
      steammessages_clientserver
      encrypted_app_ticket
      steammessages_clientserver_2
    ),
    gc: [
      'gc'
    ]
  }

  Dir.chdir("#{pwd}/lib/proto/proto") do
    protos.each_pair do |game, files|
      files.each { |file| `curl -O #{base_url}/#{game}/#{file}.proto` }
    end

    curl_cmd = 'curl -L https://github.com/google/protobuf/archive/'\
                  "v#{proto_version}.tar.gz -o protobuf.tar.gz"
    `#{curl_cmd}`
    `rm -f protobuf.tar`
    `gunzip protobuf.tar.gz`
    `tar -xvzf protobuf.tar`

    protos.each_pair do |game, files|
      FileUtils.mkdir_p("#{pwd}/lib/steam/proto/#{game}")

      ns = game.capitalize
      files.each do |file|
        `BEEFCAKE_NAMESPACE=#{ns} protoc --beefcake_out #{pwd}/lib/steam/proto/#{game} -I . -I protobuf-#{proto_version}/src #{file}.proto`
      end
    end
  end

  `rm -rf #{pwd}/lib/proto/proto/`
end

end