# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem                  1.0
PortGroup                   muniversal 1.0

name                        fontconfig
version                     2.15.0
revision                    0
checksums                   rmd160  d999328f874e67b6f4d63f0f92947907f2774f71 \
                            sha256  63a0658d0e06e0fa886106452b58ef04f21f58202ea02a94c39de0d3335d7c0e \
                            size    1447820

categories                  graphics
maintainers                 {ryandesign @ryandesign}
license                     fontconfig
use_parallel_build          yes

description                 An XML-based font configuration API for X Windows

long_description            Fontconfig is a library for configuring and \
                            customizing font access.

homepage                    https://www.freedesktop.org/wiki/Software/fontconfig/
master_sites                https://www.freedesktop.org/software/fontconfig/release/
use_xz                      yes

set python_version          3.12

depends_build               port:gettext \
                            path:bin/pkg-config:pkgconfig \
                            port:python[string map {. {}} ${python_version}]

depends_lib                 port:expat \
                            port:freetype \
                            port:gettext-runtime \
                            port:libiconv \
                            port:ossp-uuid

if {${os.platform} eq "darwin" && ${os.major} < 9} {
    set add_fonts           /usr/X11R6/lib/X11/fonts
} else {
    set add_fonts           /usr/X11/lib/X11/fonts
}
lappend add_fonts           ${prefix}/share/fonts
set docdir                  ${prefix}/share/doc/${name}

patchfiles-append           patch-docbook-4.2.diff
patchfiles-append           no-json-c.patch

configure.checks.implicit_function_declaration.whitelist-append strchr
configure.args              --disable-silent-rules \
                            --enable-iconv \
                            --enable-shared \
                            --enable-static \
                            --with-expat=${prefix} \
                            --with-libiconv=${prefix} \
                            --with-libiconv-prefix=${prefix} \
                            --with-libintl-prefix=${prefix} \
                            HASDOCBOOK=no
configure.python            ${prefix}/bin/python${python_version}

# We put this into a pre-configure block so it can be evaluated _after_ platform selection.
pre-configure {
    configure.args-append   --with-add-fonts=[join ${add_fonts} ,]

    # Fix building for older macOS versions via MACOSX_DEPLOYMENT_TARGET
    # https://bugs.freedesktop.org/show_bug.cgi?id=102986
    if {${os.platform} eq "darwin" && [vercmp ${macosx_version} >= 10.12]} {
        if {[vercmp ${macosx_deployment_target} < 10.12]} {
            configure.args-append ac_cv_func_mkostemp=no
        }
    }
}

post-destroot {
    xinstall -d ${destroot}${docdir}
    xinstall -m 0644 -W ${worksrcpath} \
        AUTHORS \
        COPYING \
        ChangeLog \
        NEWS \
        README.md \
        ${destroot}${docdir}
}

post-activate {
    # fc-cache can fail due to /Network/Library/Fonts being unavailable, so force success.
    system "${prefix}/bin/fc-cache -sv || true"
    system "${prefix}/bin/fc-cache -v || true"
}

pre-pkg {
    xinstall -m 0755 ${filespath}/postinstall ${package.scripts}/
    reinplace -locale C "s|@PREFIX@|${prefix}|g" ${package.scripts}/postinstall
    long_description-append  Install prefix: ${prefix}
}

platform macosx {
    lappend add_fonts       /Library/Fonts \
                            /Network/Library/Fonts \
                            /System/Library/Fonts
}

merger_arch_flag            no
merger_arch_compiler        yes
if {${os.arch} eq "i386"} {
    if { ${os.major} >= 10 } {
        set merger_configure_args(ppc) --with-arch=ppc
        set merger_configure_env(ppc)  CC_FOR_BUILD=${configure.cc}
    }
    set merger_configure_args(ppc64)   --with-arch=ppc64
    set merger_configure_env(ppc64)    CC_FOR_BUILD=${configure.cc}
} else {
    set merger_configure_args(i386)    --with-arch=i386
    set merger_configure_args(x86_64)  --with-arch=x86_64
    set merger_configure_env(i386)     CC_FOR_BUILD=${configure.cc}
    set merger_configure_env(x86_64)   CC_FOR_BUILD=${configure.cc}
}

livecheck.type              regex
livecheck.url               [lindex ${master_sites} 0]
livecheck.regex             ${name}-(\\d+(?:\\.\\d+)*\\.(?:\\d|\[0-8\]\\d))\\.tar
