require 'package'

class Py3_codespell < Package
  description 'Fix common misspellings in text files.'
  homepage 'https://github.com/codespell-project/codespell'
  @_ver = '2.2.2'
  version "#{@_ver}-py3.11"
  license 'GPL-2.0'
  compatibility 'all'
  source_url 'https://github.com/codespell-project/codespell.git'
  git_hashtag "v#{@_ver}"

  binary_url({
    aarch64: 'https://gitlab.com/api/v4/projects/26210301/packages/generic/py3_codespell/2.2.2-py3.11_armv7l/py3_codespell-2.2.2-py3.11-chromeos-armv7l.tar.zst',
     armv7l: 'https://gitlab.com/api/v4/projects/26210301/packages/generic/py3_codespell/2.2.2-py3.11_armv7l/py3_codespell-2.2.2-py3.11-chromeos-armv7l.tar.zst',
       i686: 'https://gitlab.com/api/v4/projects/26210301/packages/generic/py3_codespell/2.2.2-py3.11_i686/py3_codespell-2.2.2-py3.11-chromeos-i686.tar.zst',
     x86_64: 'https://gitlab.com/api/v4/projects/26210301/packages/generic/py3_codespell/2.2.2-py3.11_x86_64/py3_codespell-2.2.2-py3.11-chromeos-x86_64.tar.zst'
  })
  binary_sha256({
    aarch64: '3cf7f2828d028eb28ff2b3d57eb2c597215b504ca4c905b5afd4d55c9a7b3d28',
     armv7l: '3cf7f2828d028eb28ff2b3d57eb2c597215b504ca4c905b5afd4d55c9a7b3d28',
       i686: '93051a0e20b1927e5e43d64ee2e3faf1199757f61dc4bb0485f76552d650cebf',
     x86_64: '6ded3458c8635855bf0549a2be1b8b7cc5c0245bcf708a9a6d64c5e53b8966a4'
  })

  depends_on 'python3'
  depends_on 'python3' => :build

  def self.build
    system "python3 setup.py build #{PY3_SETUP_BUILD_OPTIONS}"
  end

  def self.install
    @python_ver = "python#{`python3 -V`[/\d.\d+/]}"
    system "python3 setup.py install #{PY_SETUP_INSTALL_OPTIONS}"
    # Fixes ModuleNotFoundError: No module named 'codespell_lib._version'
    _version_tuple = "(#{@_ver.gsub('.', ', ')})"
    FileUtils.mkdir_p "#{CREW_DEST_PREFIX}/lib/#{@python_ver}/site-packages/codespell_lib"
    _version = <<~VERSION_EOF
      # coding: utf-8
      # file generated by setuptools_scm
      # don't change, don't track in version control
      __version__ = version = '#{@_ver}'
      __version_tuple__ = version_tuple = #{_version_tuple}
    VERSION_EOF
    File.write("#{CREW_DEST_PREFIX}/lib/#{@python_ver}/site-packages/codespell_lib/_version.py", _version)
  end

  def self.remove
    @python_ver = "python#{`python3 -V`[/\d.\d+/]}"
    # Remove data, __pycache__ and tests directories.
    FileUtils.rm_rf "#{CREW_PREFIX}/lib/#{@python_ver}/site-packages/codespell_lib"
  end
end
