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

github.setup        PDAL PDAL 2.7.1
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  ca748d52bfdfe05a483daa5070754f5d989b76b7 \
                    sha256  5e5ac8b5d11ae4dbbde1b5af2ae097b13ed9ed67a57ad942b2c7704d0190c705 \
                    size    90059072

depends_build-append \
                    port:pkgconfig \

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

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

variant tiledb description "Add TileDB support" {
    depends_lib-append      port:tiledb
    configure.args-replace  -DBUILD_PLUGIN_TILEDB=OFF -DBUILD_PLUGIN_TILEDB=ON
}

variant draco description "Add Draco support" {
    depends_lib-append      port:draco
    configure.args-replace  -DBUILD_PLUGIN_DRACO=OFF -DBUILD_PLUGIN_DRACO=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 pgversion [string index ${suffix} 0].[string index ${suffix} 1]
    set index [lsearch -exact ${postgresql_variants} ${vrt}]
    set conf [lreplace ${postgresql_variants} ${index} ${index}]

    variant ${vrt} description "Use PostgreSQL ${pgversion}" 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}
        configure.args-replace  -DBUILD_PLUGIN_PGPOINTCLOUD=OFF -DBUILD_PLUGIN_PGPOINTCLOUD=ON
        configure.env-append    PostgreSQL_ADDITIONAL_VERSIONS=15,16
    "
}

# 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_INCLUDE_DIR=${prefix}/lib/proj${proj_ver}/include
            configure.env-append   LIB=${prefix}/lib/proj${proj_ver}/lib/
        "
}
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
}
