require 'package'

class Giflib < Package
  description 'giflib is a library for reading and writing gif images.'
  homepage 'https://giflib.sourceforge.net/'
  version '5.2.2'
  license 'MIT'
  compatibility 'aarch64 armv7l x86_64'
  source_url 'https://downloads.sourceforge.net/project/giflib/giflib-5.2.2.tar.gz'
  source_sha256 'be7ffbd057cadebe2aa144542fd90c6838c6a083b5e8a9048b8ee3b66b29d5fb'
  binary_compression 'tar.zst'

  binary_sha256({
    aarch64: '73bc9172813f7d8637e5448d7c80a12270224bba12fb6f76604a9a11cc895f81',
     armv7l: '73bc9172813f7d8637e5448d7c80a12270224bba12fb6f76604a9a11cc895f81',
     x86_64: 'c7e4b56340c481f7007e459010ebd126b8401d8511d6e4045b4b6245c4115941'
  })

  depends_on 'glibc' # R
  depends_on 'imagemagick7' => :build

  no_env_options

  def self.patch
    # Fix /usr/local/bin/ld: fatal error: -soname: must take a non-empty argument
    system "sed -i 's/-soname -Wl,/-soname /g' Makefile"
    # Make sure all the binaries are installed
    system "sed -i 's/$(INSTALL) $^/$(INSTALL) $(UTILS)/' Makefile"
  end

  def self.build
    # No configure script in the source.
    system "#{CREW_ENV_OPTIONS.gsub('-mfpu=vfpv3-d16',
                                    '-mfpu=neon-fp16')} make PREFIX=#{CREW_PREFIX} LIBDIR=#{CREW_LIB_PREFIX}"
  end

  def self.check
    system 'make', 'check'
  end

  def self.install
    system 'make', "DESTDIR=#{CREW_DEST_DIR}", "PREFIX=#{CREW_PREFIX}", "LIBDIR=#{CREW_LIB_PREFIX}", 'MANUAL_PAGES=doc/*.1', 'install'
    # Remove static library.
    FileUtils.rm "#{CREW_DEST_LIB_PREFIX}/libgif.a"
    @libname = name.to_s.start_with?('lib') ? name.downcase : "lib#{name.gsub('lib', '').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
