# -*- 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           openssl 1.0

name                mupdf

# Keep synchronized with port:py-mupdf.
version             1.24.2

revision            0
categories          graphics
maintainers         nomaintainer
license             AGPL-3+

description         Lightweight PDF and XPS viewer and parser/rendering library

long_description    MuPDF is a lightweight PDF and XPS viewer \
                    and parser/rendering library. \
                    \
                    The renderer in MuPDF is tailored for high-quality \
                    anti-aliased graphics. It renders text with metrics \
                    and spacing accurate to within fractions of a pixel \
                    for the highest fidelity in reproducing the look \
                    of a printed page on screen. \
                    \
                    MuPDF is also small, fast, and yet complete. It supports \
                    PDF 1.7 with transparency, encryption, hyperlinks, \
                    annotations, search and many other bells and whistles. \
                    MuPDF can also read XPS documents (OpenXPS ECMA-388).

homepage            https://www.mupdf.com

master_sites        ${homepage}/downloads/archive/
distname            ${name}-${version}-source

checksums           rmd160  e19bad3ca13762fe5b238093d62e5ce292b69df4 \
                    sha256  7fbced6d6024608661d773b93990119db7eb2483f0d3e28855eadef9ca9a8686 \
                    size    55313640

depends_build-append \
                    path:bin/pkg-config:pkgconfig

depends_lib-append  port:curl \
                    port:freetype \
                    port:gumbo-parser \
                    path:lib/pkgconfig/harfbuzz.pc:harfbuzz \
                    port:jbig2dec \
                    port:leptonica \
                    port:libarchive \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:openjpeg \
                    port:tesseract \
                    port:zlib

patchfiles          patch-build.diff

# The following patch addresses two issues:
# 1. Broken linking: build uses C compiler to link C++ code
# and also hardcodes libstdc++:
# Undefined symbols: "__Unwind_Resume" etc.
# 2. A silly parsing error for a warning which we do not need anyway:
# Makethird:57: *** missing separator. Stop.
patchfiles-append   patch-Makethird.diff

use_configure       no
# Unfortunately, this is still needed,
# otherwise something goes wrong with fonts processing
# and build may fail.
use_parallel_build  no

compiler.cxx_standard   2017

# Undefined symbols:
# "__ZN9tesseract11TessBaseAPI4InitEPKciS2_NS_13OcrEngineModeEPPciPKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISC_EESG_bPFbS2_PS6_IcSB_EE"
if {${os.platform} eq "darwin" && ${configure.cxx_stdlib} ne "libc++"} {
    configure.cxxflags-append \
                    -D_GLIBCXX_USE_CXX11_ABI=0
}

build.args          PREFIX=${prefix}
build.args-append   CC=${configure.cc} \
                    CXX=${configure.cxx} \
                    CPP=${configure.cpp} \
                    XCFLAGS="${configure.cflags} [get_canonical_archflags cc]" \
                    XCXXFLAGS="${configure.cxxflags} [get_canonical_archflags cxx]" \
                    XLIBS="${configure.ldflags} [get_canonical_archflags ld]" \
                    HAVE_GLUT=yes \
                    HAVE_X11=no \
                    USE_SYSTEM_LIBS=yes \
                    USE_TESSERACT=yes \
                    build=release \
                    shared=yes \
                    verbose=yes \
                    shared-release

destroot.destdir    prefix=${destroot}${prefix}
destroot.args-append \
                    HAVE_GLUT=yes \
                    HAVE_X11=no \
                    USE_SYSTEM_LIBS=yes \
                    shared=yes

variant x11 {
    depends_lib-append \
                    port:mesa \
                    port:xorg-libXcursor \
                    port:xorg-libXext \
                    port:xorg-libXinerama \
                    port:xorg-libXrandr

    build.args-replace \
                    HAVE_X11=no HAVE_X11=yes
    destroot.args-replace \
                    HAVE_X11=no HAVE_X11=yes
}

variant universal   {}

post-extract {
# delete thirdparty software other than mujs and lcms2,
# which uses lcms2mt.h that lcms2 port does not provide
    delete ${worksrcpath}/thirdparty/curl
    delete ${worksrcpath}/thirdparty/freeglut
    delete ${worksrcpath}/thirdparty/freetype
    delete ${worksrcpath}/thirdparty/gumbo-parser
    delete ${worksrcpath}/thirdparty/harfbuzz
    delete ${worksrcpath}/thirdparty/jbig2dec
    delete ${worksrcpath}/thirdparty/jpeg
    delete ${worksrcpath}/thirdparty/leptonica
    delete ${worksrcpath}/thirdparty/libjpeg
    delete ${worksrcpath}/thirdparty/openjpeg
    delete ${worksrcpath}/thirdparty/tesseract
    delete ${worksrcpath}/thirdparty/zlib
}

# https://trac.macports.org/ticket/33240
# Mac OS X Tiger has an old version of `make`, needs to use GNU make
platform darwin 8 {
    depends_build-append port:gmake
    build.cmd ${prefix}/bin/gmake
}

livecheck.type      regex
livecheck.url       https://mupdf.com/releases
livecheck.regex     ${name}-(\\d+(?:\\.\\d+)*\[a-z\]?)-source