require 'package'

class Graphicsmagick < Package
  description 'GraphicsMagick is the swiss army knife of image processing.'
  homepage 'http://www.graphicsmagick.org/'
  version "1.3.45-#{CREW_ICU_VER}"
  license 'MIT'
  compatibility 'all'
  min_glibc (%w[x86_64 aarch64 armv7l].include?(ARCH) ? '2.37' : '2.23').to_s
  source_url 'https://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/1.3.45/GraphicsMagick-1.3.45.tar.xz'
  source_sha256 'dcea5167414f7c805557de2d7a47a9b3147bcbf617b91f5f0f4afe5e6543026b'
  binary_compression 'tar.zst'

  binary_sha256({
    aarch64: 'd43124fbe8d5b9d83baee6a6f414de0cfebf3c8ece98b988bb1e4b15af70bb7d',
     armv7l: 'd43124fbe8d5b9d83baee6a6f414de0cfebf3c8ece98b988bb1e4b15af70bb7d',
       i686: '3e03732df5107a6c8617b785baae64d477ead57553087fafa31de0822d7f06fc',
     x86_64: '36081e952cf6c73855cf4b59770bf5b89ecfed1fe9f4f9a16105d8ebbd5b8113'
  })

  if %w[x86_64 aarch64 armv7l].include?(ARCH)
    depends_on 'freetype'
    depends_on 'ghostscript'
    depends_on 'harfbuzz'
    depends_on 'jasper'
    depends_on 'libde265'
    depends_on 'libdeflate' # R
    depends_on 'libheif'
    depends_on 'libjxl'
    depends_on 'libsm'
    depends_on 'libwebp'
    depends_on 'libwmf'
    depends_on 'libx11'
    depends_on 'libxext'
  end
  depends_on 'brotli' # R
  depends_on 'bzip2' # R
  depends_on 'gcc_lib' # R
  depends_on 'glibc_lib' # R
  depends_on 'glibc' # R
  depends_on 'highway' # R
  depends_on 'icu4c' # R
  depends_on 'jbigkit' # R
  depends_on 'lcms' # R
  depends_on 'libbsd' # R
  depends_on 'libice' # R
  depends_on 'libjpeg_turbo' # R
  depends_on 'libmd' # R
  depends_on 'libpng' # R
  depends_on 'libtiff' # R
  depends_on 'libtool' # R
  depends_on 'libxau' # R
  depends_on 'libxcb' # R
  depends_on 'libxdmcp' # R
  depends_on 'libxml2' # R
  depends_on 'msttcorefonts' # L
  depends_on 'py3_docutils' => :build
  depends_on 'util_linux' # R
  depends_on 'xzutils' # R
  depends_on 'zlib' # R
  depends_on 'zstd' # R

  no_env_options

  def self.prebuild
    # Imagemagick wants a libtool file.
    @libname =  'liblzma'
    @libnames = Dir["#{CREW_LIB_PREFIX}/#{@libname}.so*"]
    @libnames = Dir["#{CREW_LIB_PREFIX}/#{@libname}-*.so*"] if @libnames.empty?
    @libnames.each do |s|
      s.gsub!("#{CREW_LIB_PREFIX}/", '')
    end
    @dlname = @libnames.grep(/.so./).first
    @dlname = @libnames.grep(/.so/).first if @dlname.nil?
    @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_LIB_PREFIX}/#{@libname}.la", @libtool_file) unless File.file?("#{CREW_LIB_PREFIX}/#{@libname}.la")
  end

  def self.build
    system "#{CREW_ENV_OPTIONS.gsub('-mfpu=vfpv3-d16', '-mfpu=neon-fp16')} ./configure #{CREW_CONFIGURE_OPTIONS} \
      --with-windows-font-dir=#{CREW_PREFIX}/share/fonts/truetype/msttcorefonts \
      --with-perl=#{CREW_PREFIX}/bin/perl \
      --disable-maintainer-mode \
      --enable-magick-compat \
      --enable-shared=yes \
      --enable-static=no \
      --with-modules \
      --with-perl \
      #{%w[x86_64 aarch64 armv7l].include?(ARCH) ? '--with-x' : ''} \
      --with-xml"
    system 'make'
  end

  def self.install
    system 'make', "DESTDIR=#{CREW_DEST_DIR}", 'install'
  end
end
