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

PortSystem          1.0

name                postgis3
categories          databases gis
license             GPL-2+
version             3.4.0
revision            0
maintainers         {vince @Veence} {yahoo.com:n_larsson @nilason} openmaintainer

description         PostGIS is the spatial extension to the\
                    PostGreSQL database.

long_description    PostGIS adds geometrical, geographical and topological\
                    types, and functions operating thereon, to the PostGreSQL\
                    database. It also handles raster tiles and MVT format.

homepage            https://postgis.net/

master_sites        https://download.osgeo.org/postgis/source
distname            postgis-${version}

checksums           rmd160  d88cdfc8a3033a002e3db9a07e79e10bef6a9fae \
                    sha256  aee9b60a6c884d354164b3096c4657f324454186607f859d1ce05d899798af9d \
                    size    14693348

depends_build       port:autoconf \
                    port:automake \
                    port:libtool \
                    port:libxslt \
                    port:pkgconfig

depends_lib         port:geos\
                    port:libiconv\
                    port:libxml2\
                    port:json-c\
                    port:protobuf-c \
                    path:lib/pkgconfig/icu-uc.pc:icu

conflicts           postgis postgis2

# PostgreSQL database variants
set postgresql_suffixes {12 13 14 15}

set portsgresql_variants {}
set postgresql_default_variant "if {"

foreach s ${postgresql_suffixes} {
    lappend portsgresql_variants postgresql${s}
    set postgresql_default_variant "${postgresql_default_variant}!\[variant_isset postgresql${s}] && "
}

set postgresql_default_variant [string range ${postgresql_default_variant} 0 end-4]
set postgresql_default_variant "${postgresql_default_variant}} {default_variants +postgresql${s}}"
eval $postgresql_default_variant

foreach s ${postgresql_suffixes} {
    set p postgresql${s}
    set v [string index ${s} 0].[string index ${s} 1]
    set i [lsearch -exact ${portsgresql_variants} ${p}]
    set c [lreplace ${portsgresql_variants} ${i} ${i}]
    variant ${p} description "Use PostgreSQL ${v}" conflicts {*}${c} "
        depends_lib-append      port:${p}
        configure.args-append   --libdir=${prefix}/lib/${p} \
                                --bindir=${prefix}/lib/${p}/bin \
                                --with-pgconfig=${prefix}/lib/${p}/bin/pg_config
        build.args-append       PGSQL_DOCDIR=${destroot}${prefix}/share/doc/${p} \
                                PGSQL_MANDIR=${destroot}${prefix}/share/man
    "
}

variant raster              description {Build raster support} {

    depends_lib-append      port:gdal
    configure.args-append   --with-raster
    configure.args-append   --with-gdalconfig=${prefix}/bin/gdal-config
}

variant topology            description {Build topology support} {

    configure.args-append   --with-topology
}

variant sfcgal              description {Uses SFCGAL for 3D queries} {
    depends_lib-append      port:sfcgal
    configure.args-append   --with-sfcgal=${prefix}/bin/sfcgal-config
}

variant lto                 description {Build with Link Time Optimization (LTO)} {
    configure.args-append   --enable-lto
}

# Set PROJ variant
set proj_versions {6 7 8 9}

set proj_variants {}
foreach pjver ${proj_versions} {
    lappend proj_variants proj${pjver}
}
foreach proj_ver ${proj_versions} {
    set index [lsearch -exact ${proj_variants} proj${proj_ver}]
    set cflcts [lreplace ${proj_variants} ${index} ${index}]

    variant proj${proj_ver} description "Use Proj${proj_ver}" conflicts {*}${cflcts} "
            depends_lib-append      port:proj${proj_ver}
            configure.args-append   PROJ_CFLAGS=-I${prefix}/lib/proj${proj_ver}/include \
                                    PROJ_LIBS=\"-L${prefix}/lib/proj${proj_ver}/lib -lproj\"
        "
}

set projdf "if {"
foreach pv ${proj_versions} {
    set projdf "${projdf}!\[variant_isset proj${pv}\] && "
}
set projdf [string range ${projdf} 0 end-4]
set projdf "${projdf}} { default_variants +proj${pv} }"
eval ${projdf}

default_variants            +raster +topology

# Port phases

compiler.thread_local_storage yes

configure.cflags-append \
    -Diconv=libiconv -Diconv_open=libiconv_open -Diconv_close=libiconv_close

# see https://trac.macports.org/wiki/UsingTheRightCompiler
configure.env-append        CPPBIN=${configure.cpp}

post-configure {
    if {[variant_exists universal] && [variant_isset universal]} {
        # We must add universal flags to compile liblwgeom properly
        file copy ${filespath}/ed_command ${worksrcpath}/ed_command
        reinplace -E "s#XXX#${configure.universal_cflags}#" \
                    ${worksrcpath}/ed_command
        system -W ${worksrcpath}/liblwgeom \
                "ed Makefile < ${worksrcpath}/ed_command"
    }
}

build.args                  ICONV_LDFLAGS='-L${prefix}/lib -liconv'
use_parallel_build          yes

notes                       "\n\
                            ! This version comes with the following warning if you use postgresql14:\n\
                            ‘Due to some query performance degradation with the new fast index build\n\
                            that requires PG14, we have decided to disable the feature by default\n\
                            until we get more user testing as to the true impact of real-world queries.\n\
                            If you are running PG14+, you can reenable it by doing:\n\
                            \n\
                                ALTER OPERATOR FAMILY gist_geometry_ops_2d USING gist\n\
                                    ADD FUNCTION 11 (geometry)\n\
                                        geometry_gist_sortsupport_2d (internal);\n\
                            \n\
                            and then reindex your gist indexes.\n\
                            To revert back to the old index behavior:\n\
                            \n\
                                ALTER OPERATOR FAMILY gist_geometry_ops_2d using gist\n\
                                    DROP FUNCTION 11 (geometry);\n\
                            \n\
                            and then reindex your gist indexes.’"

livecheck.type              regex
livecheck.url               ${master_sites}
livecheck.regex             {postgis2?-(\d+(?:\.\d+)*)\.[tz]}
