# -*- 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.9.2
revision            1
maintainers         {yahoo.com:n_larsson @nilason} {vince @Veence} 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  41ed9e66cc6e4f369b4546c3ae37b442358b22da \
                    sha256  7c19c7a28b5737076707e4e704c4bb53ba0f5a1b37a7b705bcf55b008c100811 \
                    size    10726929

cmake.source_dir    ${worksrcpath}/saga-gis

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

depends_build-append \
                    port:pkgconfig \
                    port:swig

if {${subport} eq ${name}} {
    depends_lib-append  \
                    port:${wxWidgets.port} \
                    port:exiv2 \
                    port:gdal \
                    port:proj \
                    port:opencv4 \
                    port:pdal \
                    port:curl \
                    port:tiff
} else {
    depends_build-append \
                    port:${wxWidgets.port} \
                    port:exiv2 \
                    port:gdal \
                    port:proj \
                    port:opencv4 \
                    port:pdal \
                    port:curl \
                    port:tiff
}
compiler.cxx_standard       2017

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 \
                            -DWITH_EXIV2=ON \
                            -DPROJ_INCLUDE_DIR=${prefix}/lib/proj9/include \
                            -DPROJ_LIBRARY=${prefix}/lib/proj9/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 17}
    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 +postgresql17 }"
    eval ${pgdefault}
}

# Python subports
set python_versions {39 310 311 312 313}
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}
