#!/usr/bin/env ruby

require 'bundler/inline'
gemfile do
  source 'https://rubygems.org'
  gem 'inifile', '~> 3.0'
end

name = File.read("project.godot").split("\n")
  .find { |l| l.include?("config/name") }
  .split("=").last.gsub("\"", "")
name_kebab = name.downcase.gsub(" ", "-").gsub(":", "")

exports = {}
exports_cfg = IniFile.load("export_presets.cfg")

exports_cfg.each_section do |section|
  next if !section.include?("preset") || section.include?(".options")

  sect = exports_cfg[section]
  exports[sect["name"]] = {
    "platform" => sect["platform"],
    "export_path" => sect["export_path"].gsub("game", name_kebab),
  }
end

exports.each do |k, v|
  puts("Building export: #{k}")
  if system("godot --headless --export-release \"#{k}\" #{v['export_path']}", exception: true)
    puts("Success! Exported to: #{v['export_path']}")

    # compress exports that aren't
    if File.extname(v['export_path']) != ".zip"
      if v['platform'].downcase == "web"
        file = "exports/#{name_kebab}-web.zip"
        system("zip #{file} -j #{v['export_path'].gsub('index.html', '*')}", exception: true)
        v['export_path'] = file
      else
        file = v['export_path'].gsub(File.extname(v['export_path']), '.zip')
        system("zip #{file} -j #{v['export_path']}", exception: true)
        v['export_path'] = file
      end
    end
  else
    puts("Failed to build: #{$?}")
  end
end

export_cfg = IniFile.load("export.cfg")
itch_cfg = export_cfg["itch"]
if itch_cfg
  if system("butler -V")
    puts("itch.io butler installed, pushing builds")

    exports.each do |k, v|
      file = v['export_path'].strip
      user = itch_cfg['user']
      game = itch_cfg['game']
      channel = v['platform'].downcase.gsub(" ", "-").gsub("/", "-")

      if channel == "web"
        channel = "html"
      end

      butler_cmd = "butler push #{file} #{user}/#{game}:#{channel}"
      if ver = export_cfg['metadata']['version']
        butler_cmd += " --userversion #{ver}"
      end
      if File.exist?(file)
        puts("Pushing export to itch.io: #{k}, #{file}")

        if !system(butler_cmd, exception: true)
          puts("Failed to push to itch.io: #{$?}")
        end
      end
    end
  else
    abort("itch.io butler not installed")
  end
end

puts("Export builds complete!")
