require_relative "packages-groonga-org-package-task"

class GroongaPackageTask < PackagesGroongaOrgPackageTask
  def initialize
    super("groonga", detect_version, detect_release_time)
    @archive_name = "groonga-#{@version}.tar.gz"
    @original_archive_name = @archive_name
  end

  private
  def detect_version
    version_env = ENV["VERSION"]
    return version_env if version_env

    base_version_file = File.join(__dir__,
                                  "..",
                                  "base_version")
    version = File.read(base_version_file).chomp
  end

  def detect_target_apache_arrow_version
    cmakelists = File.join(__dir__,
                           "..",
                           "vendor",
                           "apache-arrow-source",
                           "cpp",
                           "CMakeLists.txt")
    matched = File.read(cmakelists).match(/^set\(ARROW_VERSION "(.+?)"\)/)
    version = matched[1]
    matched = version.match(/-SNAPSHOT\z/)
    if matched
      major_version = matched.pre_match.split(".", 2)[0].to_i
      previous_major_version = major_version - 1
      "#{previous_major_version}.0.0"
    else
      version
    end
  end

  def detect_target_apache_arrow_major_version
    detect_target_apache_arrow_version.split(".", 2)[0]
  end

  def define_archive_task
    file "../#{@original_archive_name}" do
      source_archive_url = built_package_url(:source, @archive_base_name)
      download(source_archive_url, "..")
    end

    file @original_archive_name => "../#{@original_archive_name}" do
      ln_s("../#{@original_archive_name}",
           @original_archive_name)
    end
  end

  def docker_build_options(os, architecture)
    [
      "--build-arg",
      "APACHE_ARROW_VERSION=#{detect_target_apache_arrow_version}",
    ]
  end

  def apt_targets_default
    [
      "debian-bullseye",
      "debian-bullseye-arm64",
    ]
  end

  def yum_targets_default
    [
      "almalinux-8",
      "almalinux-8-aarch64",
      "almalinux-9",
      "almalinux-9-aarch64",
      "amazon-linux-2",
      "centos-7",
      "oracle-linux-8",
      "oracle-linux-9",
    ]
  end

  def yum_expand_variable(key)
    case key
    when "APACHE_ARROW_VERSION"
      detect_target_apache_arrow_version
    when "APACHE_ARROW_MAJOR_VERSION"
      detect_target_apache_arrow_major_version
    else
      super
    end
  end

  def source_targets_default
    [
      "#{@archive_base_name}.tar.gz",
      "#{@archive_base_name}.zip",
    ]
  end

  def windows_targets_default
    [
      "#{@package}-#{@version}-x64-vs2022.zip",
      "#{@package}-#{@version}-x64-vs2022-with-vcruntime.zip",
      "#{@package}-#{@version}-x64-vs2019.zip",
      "#{@package}-#{@version}-x64-vs2019-with-vcruntime.zip",
      "#{@package}-#{@version}-x86-vs2019.zip",
      "#{@package}-#{@version}-x86-vs2019-with-vcruntime.zip",
    ]
  end

  def use_built_package?
    true
  end

  def docker_image(os, architecture)
    "ghcr.io/groonga/package-#{super}"
  end

  def github_repository
    "groonga/groonga"
  end

  def github_actions_workflow_file_name(target_namespace, target)
    case target_namespace
    when :apt, :yum
      "package.yml"
    when :windows
      "windows-msvc.yml"
    else
      super
    end
  end

  def github_actions_artifact_name(target_namespace, target)
    case target_namespace
    when :apt
      if target.end_with?("-i386") or target.end_with?("-arm64")
        "packages-#{target}"
      else
        "packages-#{target}-amd64"
      end
    when :yum
      if target.end_with?("-aarch64")
        "packages-#{target}"
      else
        "packages-#{target}-x86_64"
      end
    else
      raise NotImplementedError
    end
  end

  def built_package_url(target_namespace, target)
    url = "https://github.com/groonga/groonga/releases/download/v#{@version}/"
    case target_namespace
    when :apt
      if target.end_with?("-i386") or target.end_with?("-arm64")
        url << "#{target}.tar.gz"
      else
        url << "#{target}-amd64.tar.gz"
      end
    when :yum
      if target.end_with?("-aarch64")
        url << "#{target}.tar.gz"
      else
        url << "#{target}-x86_64.tar.gz"
      end
    when :source
      url << "#{target}"
    when :windows
      url << "#{target}"
    else
      url << "#{target}.tar.gz"
    end
    url
  end

  def built_package_n_split_components
    3
  end

  def download_packages(target_namespace)
    super unless target_namespace == :windows
  end

  def prepare(target_namespace)
    return super unless target_namespace == :windows

    repositories_dir = download_repositories_dir(target_namespace)
    mkdir_p(repositories_dir)

    File.open("#{repositories_dir}/.htaccess", "w") do |htaccess|
      __send__("#{target_namespace}_targets").each do |target|
        redirect_url = built_package_url(target_namespace, target)
        redirect_target_file = target.gsub(@version, "latest")
        redirect_target = "/#{target_namespace}/#{@package}/#{redirect_target_file}"
        htaccess.puts("Redirect #{redirect_target} #{redirect_url}")
      end
    end
  end
end

task = GroongaPackageTask.new
task.define
