# -*- 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           cmake         1.1
PortGroup           github        1.0
PortGroup           legacysupport 1.1

github.setup        PDAL PDAL 2.9.2
revision            0

name                pdal
categories          gis
license             BSD
maintainers         {yahoo.com:n_larsson @nilason} {vince @Veence} openmaintainer
description         PDAL - Point Cloud Manipulation Library
long_description    PDAL is a C++ BSD library for translating and\
                    manipulating point cloud data. It is very much\
                    like the GDAL library which handles raster and\
                    vector data.
homepage            https://www.pdal.io
distname            PDAL-${version}-src

# Enable use of 'macports-libcxx' for macOS 10.14 and earlier, as port uses
# libcxx features normally only available on 10.15 and later.
legacysupport.use_mp_libcxx \
                    yes
legacysupport.newest_darwin_requires_legacy \
                    18

compiler.cxx_standard  2017
compiler.thread_local_storage yes

github.tarball_from releases

checksums           rmd160  15aa80e05bc5ed5d14c25d09d01dae6de93f2102 \
                    sha256  4e6a0eedd0b757eec79eaa4a05ba36f1b5b591786adec3723baeba2c9dec48eb \
                    size    99731813

depends_build-append \
                    port:pkgconfig \

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

    depends_lib-append  port:curl \
                        port:gdal \
                        port:geos \
                        port:libgeotiff \
                        port:libxml2 \
                        port:lzma \
                        port:proj \
                        port:xercesc3 \
                        port:zlib \
                        port:zstd

    if {${name} eq ${subport}} {
        patchfiles-append   patch-powerpc.diff

        # Build fails on macOS 11 and older, see https://trac.macports.org/ticket/72983.
        if { ${os.platform} eq "darwin" && ${os.major} < 21 } {
            patchfiles-append patch-pointview.diff
        }
    }

    # Starting with Xcode 15, the port libunwind is causing crashes,
    # see https://trac.macports.org/ticket/68250 and https://trac.macports.org/ticket/66250.
    if {[vercmp $xcodeversion >= 15.0]} {
        conflicts       libunwind
    }


    configure.args-append \
                        -DBUILD_TOOLS_NITFWRAP=OFF \
                        -DPROJ_DIR=${prefix}/lib/proj9/lib/cmake/proj \
                        -DWITH_BACKTRACE=OFF \
                        -DWITH_COMPLETION=OFF \
                        -DWITH_LZMA=ON \
                        -DWITH_TESTS=OFF \
                        -DWITH_ZLIB=ON \
                        -DWITH_ZSTD=ON

    configure.args-append \
                        -DBUILD_PLUGIN_CPD=OFF \
                        -DBUILD_PLUGIN_DRACO=OFF \
                        -DBUILD_PLUGIN_E57=ON \
                        -DBUILD_PLUGIN_FBX=OFF \
                        -DBUILD_PLUGIN_HDF=OFF \
                        -DBUILD_PLUGIN_ICEBRIDGE=OFF \
                        -DBUILD_PLUGIN_MATLAB=OFF \
                        -DBUILD_PLUGIN_MBIO=OFF \
                        -DBUILD_PLUGIN_NITF=OFF \
                        -DBUILD_PLUGIN_OPENSCENEGRAPH=OFF \
                        -DBUILD_PLUGIN_PGPOINTCLOUD=OFF \
                        -DBUILD_PLUGIN_RDBLIB=OFF \
                        -DBUILD_PLUGIN_RIVLIB=OFF \
                        -DBUILD_PLUGIN_TEASER=OFF \
                        -DBUILD_PLUGIN_TILEDB=OFF \
                        -DBUILD_PLUGIN_TRAJECTORY=OFF

    # Kludge: needs proper fixing
    configure.cppflags-append   -g
    configure.ldflags-append    -g
    post-configure {
        reinplace "s|-isysroot\[^ \]*||" ${workpath}/build/bin/pdal-config
    }
}

subport pdal-draco {
    description         PDAL - Draco driver
    long_description    {*}${description}.

    worksrcdir          ${worksrcpath}/plugins/draco

    depends_lib-append  port:draco \
                        port:pdal

    configure.args      -DSTANDALONE=ON \
                        -DBUILD_PLUGIN_DRACO=ON

    livecheck.type      none
}

subport pdal-pgpointcloud {
    description         PDAL - pgpointcloud driver
    long_description    {*}${description}.

    worksrcdir          ${worksrcpath}/plugins/pgpointcloud

    depends_lib-append  port:libxml2 \
                        port:pdal

    configure.args      -DSTANDALONE=ON \
                        -DBUILD_PLUGIN_PGPOINTCLOUD=ON

    # PostgreSQL variants
    set postgresql_suffixes {17 16 15 14 13 12}
    set def_psql_var "if {"

    set postgresql_variants {}
    foreach suffix ${postgresql_suffixes} {
        lappend postgresql_variants postgresql${suffix}
        set def_psql_var "${def_psql_var}![variant_isset postgresql${suffix}] && "
    }

    # Set default variant
    set def_psql_var [string range ${def_psql_var} 0 end-4]
    set def_psql_var "${def_psql_var}} {default_variants +postgresql17}"
    eval ${def_psql_var}

    foreach suffix ${postgresql_suffixes} {
        set vrt postgresql${suffix}
        set index [lsearch -exact ${postgresql_variants} ${vrt}]
        set conf [lreplace ${postgresql_variants} ${index} ${index}]

        variant ${vrt} description "Use PostgreSQL ${suffix}" conflicts {*}${conf} "
            depends_lib-append      port:${vrt} \
                                    port:pg${suffix}-pgpointcloud
            configure.args-append   -DPostgreSQL_ROOT=${prefix}/lib/${vrt} \
                                    -DPostgreSQL_INCLUDE_DIR=${prefix}/include/${vrt} \
                                    -DBUILD_PLUGIN_PGPOINTCLOUD=ON
            configure.env-append    PostgreSQL_ADDITIONAL_VERSIONS=15,16,17
        "
    }

    livecheck.type  none
}

subport pdal-tiledb {
    description         PDAL - TileDB driver
    long_description    {*}${description}.

    worksrcdir          ${worksrcpath}/plugins/tiledb

    depends_lib-append  port:pdal \
                        port:tiledb

    configure.args      -DSTANDALONE=ON \
                        -DBUILD_PLUGIN_TILEDB=ON

    livecheck.type      none
}
