# -*- 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.8.0
revision            0

name                pdal
categories          gis
license             BSD
maintainers         {vince @Veence} {yahoo.com:n_larsson @nilason} 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  935c172699e2b4f0eb7ae0c7f5971177f646953c \
                    sha256  e23802d16c7bb930802bfb7947a3dae6bfdbb678e6d1c7247334c3e1ebf35805 \
                    size    90060581

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:xercesc3 \
                        port:zlib \
                        port:zstd

    if {${name} eq ${subport}} {
        patchfiles-append   patch-powerpc.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 \
                        -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

    # Set PROJ
    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  -DPROJ_DIR=${prefix}/lib/proj${proj_ver}/lib/cmake/proj
            "
    }
    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}

    # 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 {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 +postgresql16}"
    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
        "
    }

    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
}
