# -*- 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.6.0
revision            0
maintainers         {yahoo.com:n_larsson @nilason} {vince @Veence} 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  8b4d90449549162235afb4bdcb266cb85e111373 \
                    sha256  8caffef4b457ed70d5328bf4e5a21f9306b06c271662e03e1a65d30090e5f25f \
                    size    16202671

patchfiles          patch-configure.diff

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"
    }
}

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

# PostgreSQL subports
set postgresql_suffixes {12 13 14 15 16 17}
set subport_names {}
set docports {}
foreach v ${postgresql_suffixes} {
    lappend subport_names pg${v}-${name}
    lappend docports pg${v}-${name}-doc
}
foreach v ${postgresql_suffixes} {
    subport pg${v}-${name} {
        depends_build-append \
                            port:autoconf \
                            port:automake \
                            port:libtool \
                            port:libxslt \
                            port:pkgconfig

        depends_lib-append  port:geos \
                            port:json-c \
                            port:libiconv \
                            port:libxml2 \
                            port:protobuf-c

        conflicts           postgis postgis2

        compiler.cxx_standard 2011
        compiler.thread_local_storage yes

        configure.args-append \
                            --without-address-standardizer \
                            --without-raster \
                            --without-sfcgal \
                            --without-topology

        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}

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

        variant address_standardizer description {Build with Address Standardizer support} {
            depends_lib-append      port:pcre2
            configure.args-replace  --without-address-standardizer --with-address-standardizer
        }

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

        # PROJ variants
        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}

        variant raster description {Build raster support} {
            depends_lib-append      port:gdal
            configure.args-replace  --without-raster --with-raster
            configure.args-append   --with-gdalconfig=${prefix}/bin/gdal-config
        }

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

        variant topology description {Build topology support} {
            configure.args-replace  --without-topology --with-topology
        }

        default_variants    +raster +topology

        # Set PostgreSQL subport
        set p postgresql${v}
        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
        livecheck.type          none

        post-destroot {
            delete ${destroot}${prefix}/share/man
        }

        if {${v} eq 14} {
            notes-append "\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.’"
        }
    }

    subport pg${v}-${name}-doc {
        platforms           any
        supported_archs     noarch

        description         Documentation of PostGIS for PostgreSQL $v
        long_description    {*}${long_description} This adds documentation.

        set index [lsearch -exact ${docports} pg${v}-${name}-doc]
        set cflcts [lreplace ${docports} ${index} ${index}]
        conflicts {*}${cflcts}
        set p postgresql${v}

        depends_build-append \
                            port:autoconf \
                            port:automake \
                            port:geos \
                            port:libtool \
                            port:libxslt \
                            port:pkgconfig \
                            port:${p} \
                            port:proj9 \
                            port:protobuf-c

        configure.args-append \
                            --libdir=${prefix}/lib/${p} \
                            --bindir=${prefix}/lib/${p}/bin \
                            --with-pgconfig=${prefix}/lib/${p}/bin/pg_config \
                            --without-address-standardizer \
                            --without-raster \
                            --without-sfcgal \
                            --without-topology \
                            PROJ_CFLAGS=-I${prefix}/lib/proj9/include \
                            PROJ_LIBS=\"-L${prefix}/lib/proj9/lib -lproj\"
        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}
        build.args          ICONV_LDFLAGS='-L${prefix}/lib -liconv'
        build.args-append   PGSQL_DOCDIR=${destroot}${prefix}/share/doc/${p} \
                            PGSQL_MANDIR=${destroot}${prefix}/share/man
        build.env-append    DESTDIR=${destroot}
        build.dir           ${worksrcpath}/doc
        build.target        man-install

        post-destroot {
            delete ${destroot}${prefix}/share/${p}
        }

        livecheck.type          none
    }
}

if {${name} eq ${subport}} {
    PortGroup   stub 1.0

    platforms   any
    supported_archs noarch

    notes "The $name port is just a stub. Please install one of the\
          subports: ${subport_names}."
}
