# -*- 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
PortGroup           active_variants 1.1
PortGroup           cmake 1.1
PortGroup           wxWidgets 1.0

wxWidgets.use       wxWidgets-3.2

name                saga
categories          gis
license             GPL
version             9.5.1
revision            0
maintainers         {vince @Veence} {yahoo.com:n_larsson @nilason} openmaintainer

description         SAGA is a GIS oriented towards statistics and analysis

long_description    SAGA - System for Automated Geoscientific Analyses - is\
                    a GIS software designed for an easy and effective\
                    implementation of spatial algorithms and offers\
                    a comprehensive, growing set of geoscientific methods.\
                    It uses wxWidgets as a GUI.

homepage            https://saga-gis.sourceforge.io/en/index.html
set master_sites_in sourceforge:project/saga-gis/SAGA%20-%20[lindex [split ${version} "."] 0]/SAGA%20-%20${version}
master_sites        ${master_sites_in}

checksums           rmd160  6dc6889af65642493b4915a13c2274f812be0f4f \
                    sha256  eab76d1291f16d2a890d67fbfa3471d08012ded090e4d8a8aac913564869490a \
                    size    9207793

cmake.source_dir    ${worksrcpath}/saga-gis

patchfiles-append   shape_cpp.diff \
                    python_cmake.diff \
                    patch-findpostgres-cmake.diff

depends_build-append \
                    port:pkgconfig \
                    port:swig

if {${subport} eq ${name}} {
# SAGA 9.1.0 do not support PROJ 8+ API
    depends_lib-append  \
                    port:${wxWidgets.port} \
                    port:gdal \
                    port:proj7 \
                    port:opencv4 \
                    port:pdal \
                    port:curl \
                    port:tiff
} else {
    depends_build-append \
                    port:${wxWidgets.port} \
                    port:gdal \
                    port:proj7 \
                    port:opencv4 \
                    port:pdal \
                    port:curl \
                    port:tiff
}
compiler.cxx_standard       2014

configure.pkg_config_path-append \
                            "${prefix}/lib/opencv4/pkgconfig"

configure.args              -DwxWidgets_CONFIG_EXECUTABLE=${wxWidgets.wxconfig} \
                            -DOpenMP_SUPPORT=OFF \
                            -DWITH_GUI=OFF \
                            -DWITH_PYTHON=OFF \
                            -DWITH_SYSTEM_SVM=OFF \
                            -DWITH_SYSTEM_GEOTRANS=OFF \
                            -DWITH_CLIPPER_ONE=OFF \
                            -DWITH_MRMR=OFF \
                            -DPROJ_INCLUDE_DIR=${prefix}/lib/proj7/include \
                            -DPROJ_LIBRARY=${prefix}/lib/proj7/lib/libproj.dylib
                            
configure.args-append       -DWITH_TOOLS=ON \
                            -DWITH_TOOLS_OPENCV=ON \
                            -DWITH_TOOLS_GDAL=ON \
                            -DWITH_TOOLS_HPDF=OFF \
                            -DWITH_TOOLS_PDAL=ON \
                            -DWITH_TOOLS_POSTGRES=OFF \
                            -DWITH_TOOLS_PROJ=ON \
                            -DWITH_TOOLS_RIEGL=OFF \
                            -DWITH_TOOLS_VIGRA=OFF \

# Variants
if {${subport} eq ${name}} {

    variant gui description "Build with GUI interface" {
        configure.args-replace  -DWITH_GUI=OFF -DWITH_GUI=ON
    }

    # Set ODBC variant
    variant unixodbc conflicts iodbc description {Build ODBC driver against unixODBC} {
        depends_lib-append      port:unixODBC
    }

    variant iodbc conflicts unixodbc description {Build ODBC driver against iODBC} {
        depends_lib-append      port:libiodbc
    }

    if {![variant_isset unixodbc] && ![variant_isset iodbc]} {
        default_variants        +unixodbc
    }

    variant vigra description "Add VIGRA support" {
        pre-configure {
            # If HDF5 is built with a mpi variant, we need to know the path to "mpi.h".
            # Figure out HDF5's mpi include directory:
            set mpl_include_dir ""
            if {![catch {set result [active_variants hdf5 openmpi]}]} {
                if {$result} {
                    set mpl_include_dir "-I${prefix}/include/openmpi-mp"
                }
            }
            if {![catch {set result [active_variants hdf5 mpich]}]} {
                if {$result} {
                    set mpl_include_dir "-I${prefix}/include/mpich-mp"
                }
            }
            if {$mpl_include_dir ne ""} {
                configure.cxxflags-append ${mpl_include_dir}
            }
        }

        configure.args-replace     -DWITH_TOOLS_VIGRA=OFF -DWITH_TOOLS_VIGRA=ON \
                                   -DWITH_MRMR=OFF -DWITH_MRMR=ON
        depends_lib-append         port:vigra \
                                   port:hdf5 \
                                   port:fftw-3
    }

    variant libharu description "Add libHaru support for PDF creation" {
        configure.args-replace  -DWITH_TOOLS_HPDF=OFF -DWITH_TOOLS_HPDF=ON
        depends_lib-append      port:libharu
    }

    # Set PostgreSQL variant
    set postgresql_suffixes {12 13 14 15 16}
    set postgresql_variants {}
    foreach suffix ${postgresql_suffixes} {
        lappend postgresql_variants postgresql${suffix}
    }
    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}
            configure.env-append    POSTGRES_HOME=${prefix}/lib/${vrt}
            configure.args-replace  -DWITH_TOOLS_POSTGRES=OFF -DWITH_TOOLS_POSTGRES=ON
        "
    }
    # PostgreSQL default
    set pgdefault "if {"
    foreach suffix ${postgresql_suffixes} {
        set pgdefault "${pgdefault}!\[variant_isset postgresql${suffix}\] && "
    }
    set pgdefault [string range ${pgdefault} 0 end-4]
    set pgdefault "${pgdefault}} { default_variants +postgresql16 }"
    eval ${pgdefault}
}

# Python subports
set python_versions {39 310 311 312}
foreach v ${python_versions} {
    subport py${v}-py${name} {
        PortGroup           python 1.0

        categories          gis python

        depends_lib-append  port:${name}

        use_configure       yes

        python.default_version  ${v}
        python.add_dependencies no
        build.cmd           make
        build.target
        destroot.cmd        make install
        destroot.destdir    DESTDIR=${destroot}

        master_sites        ${master_sites_in}
        
        livecheck.type      none
    }
}

# Python bindings for supported Python versions
if {[string match "py*" ${subport}]} {
    description             PySAGA, Python ${python.branch} bindings for SAGA
    long_description        {*}${description}.

    depends_lib-append      port:python${python.version}
    depends_build-append    port:swig-python

    configure.args-replace  -DWITH_PYTHON=OFF -DWITH_PYTHON=ON
    configure.args-append   -DWITH_PYTHON_PKG=ON
    post-patch {
        reinplace "s|@PYVER@|${python.branch}|" \
          ${worksrcpath}/saga-gis/src/saga_core/saga_api/saga_api_python/CMakeLists.txt
    }

    post-destroot {
        delete --force ${destroot}${prefix}/bin
        delete --force ${destroot}${prefix}/include
        delete --force ${destroot}${prefix}/lib
        delete --force ${destroot}${prefix}/share
    }
}

destroot.target             install

livecheck.regex             ${name}-(\[0-9a-z.\]+)${extract.suffix}
