# -*- 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           conflicts_build 1.0
PortGroup           boost 1.0

name                mapnik
version             4.0.4
revision            2
categories          gis devel
license             LGPL-2.1

maintainers         panulla.com:macports {stromnov @stromnov} openmaintainer

description         Open Source C++/Python mapping toolkit.

long_description    Mapnik is a Free Toolkit for developing mapping \
                    applications. It is written in modern C++ and has Python \
                    bindings that support fast-paced agile development. It \
                    can comfortably be used for both desktop map design and \
                    web development. \
                    \
                    Mapnik is about making beautiful maps. It uses the AGG \
                    graphics library, which offers world-class anti-aliasing \
                    rendering with subpixel accuracy for geographic data.

homepage            https://mapnik.org
master_sites        https://github.com/mapnik/mapnik/releases/download/v${version}/

distname            mapnik-v${version}

use_bzip2           yes

checksums           rmd160  7e5c2cb48662879ce1b8572ef84ac6153fd51c19 \
                    sha256  e4f564f9475088dd1d814b20b2500ab0ab073f657add56449ddb634f0f086ca7 \
                    size    13020826

# When changing `boost.version`, please update the `mod_tile` port
# correspondingly with the same version of Boost.  Also increase the
# `mod_tile` `revision` attribute.
boost.version       1.76

set python_branch       3.12
set python_version      [string map {. {}} ${python_branch}]

patchfiles          patch-sconstruct.diff

depends_build-append \
                    port:pkgconfig \
                    port:python${python_version}

depends_lib-append  path:lib/pkgconfig/harfbuzz.pc:harfbuzz \
                    path:lib/pkgconfig/icu-uc.pc:icu \
                    port:libpng \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:tiff \
                    port:webp \
                    port:zlib \
                    port:freetype \
                    port:proj7

conflicts_build     antigraingeometry

set input_plugins {shape csv raster geojson topojson}

variant cairo description {Enable Cairo support for PDF, PostScript, and SVG} {
    configure.args-delete   CAIRO=False
    configure.args-append   CAIRO=True
    depends_lib-append      path:lib/pkgconfig/cairo.pc:cairo
}

variant gdal description {Build GDAL/OGR plugin} {
    depends_lib-append      port:gdal
    configure.args-append   GDAL_CONFIG=${prefix}/bin/gdal-config
    lappend input_plugins gdal ogr
}

variant postgis description {Build PostGIS plugin} {
    depends_lib-append      path:lib/libpq.dylib:postgresql17
    if {![file exists ${prefix}/lib/libpq.dylib]} {
        configure.args-append   PG_CONFIG=${prefix}/lib/postgresql17/bin/pg_config
    }
    lappend input_plugins postgis
}

variant sqlite description {Build SQLite plugin} {
    depends_lib-append      port:sqlite3
    lappend input_plugins sqlite
}

variant libxml2 description {Enable libxml2 support for includes and entities} {
    depends_lib-append      port:libxml2
    configure.args-append   XML2_CONFIG=${prefix}/bin/xml2-config \
                            XMLPARSER=libxml2
}

default_variants +postgis

compiler.cxx_standard  2017

universal_variant   no

configure.pkg_config ${prefix}/bin/pkg-config

configure.python        ${prefix}/bin/python${python_branch}

build.cmd           ${configure.python} scons/scons.py
build.pre_args
use_parallel_build  yes

configure.cmd       ${build.cmd}
configure.pre_args  configure
configure.args      CXX="\$CXX" \
                    CC="\$CC" \
                    CUSTOM_CXXFLAGS="\$CPPFLAGS \$CXXFLAGS" \
                    CUSTOM_CFLAGS="\$CPPFLAGS \$CFLAGS" \
                    CUSTOM_LDFLAGS="\$LDFLAGS" \
                    PREFIX=${prefix} \
                    DESTDIR=${destroot} \
                    CAIRO=False \
                    CPP_TESTS=False \
                    PROJ_INCLUDES=${prefix}/lib/proj7/include \
                    PROJ_LIBS=${prefix}/lib/proj7/lib \
                    BOOST_INCLUDES=[boost::include_dir] \
                    BOOST_LIBS=[boost::lib_dir]

configure.pkg_config_path   ${prefix}/lib/proj7/lib/pkgconfig

configure.cppflags-append \
                    -DACCEPT_USE_OF_DEPRECATED_PROJ_API_H=1

foreach lib {ICU PNG JPEG TIFF CAIRO SQLITE HB WEBP} {
    configure.args-append   ${lib}_INCLUDES=${prefix}/include \
                            ${lib}_LIBS=${prefix}/lib
}

destroot.destdir

pre-configure {
    # Must be evaluated after the variants
    configure.args-append   INPUT_PLUGINS=[join ${input_plugins} ","]

    if {[variant_isset postgis] && [variant_isset sqlite]} {
        configure.args-append   PGSQL2SQLITE=True
    }
}

post-destroot {
    xinstall -d -m 755 ${destroot}${prefix}/share/doc/${name}
    xinstall -m 644 -W ${worksrcpath} \
        {*}[glob ${worksrcpath}/docs/*] AUTHORS.md CHANGELOG.md COPYING README.md \
        ${destroot}${prefix}/share/doc/${name}
    xinstall -d -m 755 ${destroot}${prefix}/share/${name}
    copy ${worksrcpath}/demo ${destroot}${prefix}/share/${name}
}

livecheck.type      regex
livecheck.url       https://mapnik.org/pages/downloads.html
livecheck.regex     {mapnik-v(\d+(?:\.\d+)*)[.tz]}
