require 'package'

class Fribidi < Package
  description 'GNU FriBidi is an implementation of the Unicode Bidirectional Algorithm (bidi).'
  homepage 'https://github.com/fribidi/fribidi'
  version '1.0.12'
  license 'LGPL-2.1+'
  compatibility 'all'
  source_url 'https://github.com/fribidi/fribidi/archive/v1.0.12.tar.gz'
  source_sha256 '2e9e859876571f03567ac91e5ed3b5308791f31cda083408c2b60fa1fe00a39d'
  binary_compression 'tar.zst'

  binary_sha256({
    aarch64: '13c899b89cde0b3c30a83999333748373fa343dd49e1add72f19484a98d9a9c1',
     armv7l: '13c899b89cde0b3c30a83999333748373fa343dd49e1add72f19484a98d9a9c1',
       i686: '8d25201398c849b5993b27127e15ff3b5523c2354a543f8bb6b9423b426102be',
     x86_64: '4e4b3e936a23f145efaa878895cc8448196d6ead30d7bc0a61b3d8add4401a48'
  })

  depends_on 'glibc' # R

  def self.build
    system "meson setup #{CREW_MESON_OPTIONS} \
      --default-library=both \
      -Ddocs=false \
      -Dtests=false \
      builddir"
    system 'meson configure --no-pager builddir'
    system 'ninja -v -C builddir'
  end

  def self.install
    system "DESTDIR=#{CREW_DEST_DIR} ninja -C builddir install"
    # Create libtool file. Needed by handbrake build.
    return if File.file?("#{CREW_DEST_LIB_PREFIX}/#{@libname}.la")

    @libname = name.to_s.start_with?('lib') ? name.downcase : "lib#{name.downcase}"
    @libnames = Dir["#{CREW_DEST_LIB_PREFIX}/#{@libname}.so*"]
    @libnames = Dir["#{CREW_DEST_LIB_PREFIX}/#{@libname}-*.so*"] if @libnames.empty?
    @libnames.each do |s|
      s.gsub!("#{CREW_DEST_LIB_PREFIX}/", '')
    end
    @dlname = @libnames.grep(/.so./).first
    @libname = @dlname.gsub(/.so.\d+/, '')
    @longest_libname = @libnames.max_by(&:length)
    @libvars = @longest_libname.rpartition('.so.')[2].split('.')

    @libtool_file = <<~LIBTOOLEOF
      # #{@libname}.la - a libtool library file
      # Generated by libtool (GNU libtool) (Created by Chromebrew)
      #
      # Please DO NOT delete this file!
      # It is necessary for linking the library.

      # The name that we can dlopen(3).
      dlname='#{@dlname}'

      # Names of this library.
      library_names='#{@libnames.reverse.join(' ')}'

      # The name of the static archive.
      old_library='#{@libname}.a'

      # Linker flags that cannot go in dependency_libs.
      inherited_linker_flags=''

      # Libraries that this one depends upon.
      dependency_libs=''

      # Names of additional weak libraries provided by this library
      weak_library_names=''

      # Version information for #{name}.
      current=#{@libvars[1]}
      age=#{@libvars[1]}
      revision=#{@libvars[2]}

      # Is this an already installed library?
      installed=yes

      # Should we warn about portability when linking against -modules?
      shouldnotlink=no

      # Files to dlopen/dlpreopen
      dlopen=''
      dlpreopen=''

      # Directory that this library needs to be installed in:
      libdir='#{CREW_LIB_PREFIX}'
    LIBTOOLEOF
    File.write("#{CREW_DEST_LIB_PREFIX}/#{@libname}.la", @libtool_file)
  end
end
