# -*- 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           boost 1.0

name                orfeotoolbox
version             9.1.1
revision            1
categories          gis graphics
license             Apache-2

maintainers         nomaintainer

description         Orfeo ToolBox - Free library of image processing algorithms

long_description    Orfeo ToolBox (OTB) is an open-source project for          \
                    state-of-the-art remote sensing. Built on the shoulders    \
                    of the open-source geospatial community, it can process    \
                    high resolution optical, multispectral and radar images at \
                    the terabyte scale. A wide variety of applications are     \
                    available: from ortho-rectification or pansharpening, all  \
                    the way to classification, SAR processing, and much more!

homepage            https://www.orfeo-toolbox.org/
master_sites        https://www.orfeo-toolbox.org/packages/

distname            OTB-${version}
use_xz              no
extract.mkdir       yes

checksums           rmd160  83e1f2aaecf2a983ce9ce4bb2256d15afdf8eefc \
                    sha256  e0be44c8ffc613766fabe9ea5fb09ea4cb7e95a97c8d56e849e1c0e086b5681b \
                    size    857572702

boost.version       1.87

compiler.cxx_standard 2014
compiler.thread_local_storage yes

set opencv          opencv4

depends_build-append port:swig

depends_lib-append  port:curl \
                    port:expat \
                    port:fftw-3 \
                    port:fftw-single \
                    port:gdal \
                    port:InsightToolkit4 \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:libgeotiff \
                    port:libkml \
                    port:libpng \
                    port:libsvm \
                    port:muparser \
                    port:muparserx \
                    port:${opencv} \
                    port:shark \
                    port:tiff \
                    port:tinyxml \
                    port:zlib

patchfiles-append   patch-neural.diff \
                    patch-FindOpenCV.diff \
                    patch-app.diff \
                    patch-compiler-flags.diff \
                    patch_assert.diff

post-patch {
    reinplace "s|@OPENCV@|${opencv}|" ${worksrcpath}/CMake/FindOpenCV.cmake
}

cmake.module_path-append \
                    ${prefix}/libexec/${opencv}/cmake

configure.args-append \
                    -DBUILD_EXAMPLES=OFF \
                    -DBUILD_SHARED_LIBS=ON \
                    -DBUILD_TESTING=OFF \
                    -DOTBGroup_Core=ON \
                    -DOTBGroup_FeaturesExtraction=ON \
                    -DOTBGroup_Hyperspectral=ON \
                    -DOTBGroup_Learning=ON \
                    -DOTBGroup_Miscellaneous=ON \
                    -DOTBGroup_Remote=ON \
                    -DOTBGroup_SAR=ON \
                    -DOTBGroup_Segmentation=ON \
                    -DOTBGroup_StereoProcessing=ON \
                    -DOTBGroup_ThirdParty=ON \
                    -DOTB_USE_6S=ON \
                    -DOTB_USE_CURL=ON \
                    -DOTB_USE_GSL=OFF \
                    -DOTB_USE_LIBKML=ON \
                    -DOTB_USE_LIBSVM=ON \
                    -DOTB_USE_MUPARSER=ON \
                    -DOTB_USE_MUPARSERX=ON \
                    -DOTB_USE_OPENCV=ON \
                    -DOTB_USE_SHARK=ON \
                    -DOTB_USE_SIFTFAST=ON \
                    -DOTB_USE_SIFTFAST=ON \
                    -DOTB_WRAP_PYTHON=OFF \
                    -DOTB_WRAP_QGIS=ON

# The default installation path for library is ${prefix}/lib/otb
# But the cmake PortGroup is using rpath and CMAKE_INSTALL_NAME_DIR=${prefix}/lib
configure.args-replace \
                    -DCMAKE_INSTALL_NAME_DIR=${prefix}/lib \
                    -DCMAKE_INSTALL_NAME_DIR=${prefix}/lib/otb

configure.args-append \
                    -DCMAKE_BUILD_RPATH=${cmake.build_dir}/lib

# Create Python subports
set python_versions {310 311 312 313}
foreach v ${python_versions} {
    subport py${v}-${name} {
        set py_dot_ver      [string index ${v} 0].[string range ${v} 1 end]
        set py_prefix       ${frameworks_dir}/Python.framework/Versions/${py_dot_ver}
        set py_pkgd         ${py_prefix}/lib/python${py_dot_ver}/site-packages

        categories          gis python

        description         Python ${py_dot_ver} bindings for {*}${name}
        long_description    This package provides Python ${py_dot_ver} modules \
                            for {*}${name}.

        depends_lib-append  port:${name} \
                            port:python${v} \
                            port:swig-python

        configure.args-replace \
                            -DOTB_WRAP_PYTHON=OFF -DOTB_WRAP_PYTHON=ON

        configure.args-append \
                            -DOTB_INSTALL_PYTHON_DIR=${py_pkgd}/otbApplication

        patchfiles-append   patch-py-version.diff \
                            patch_swig43.diff

        post-patch {
            reinplace "s|@PYVER@|${py_dot_ver}|" ${worksrcpath}/CMakeLists.txt
        }

        post-destroot {
            delete ${destroot}${prefix}/bin
            delete ${destroot}${prefix}/include
            delete ${destroot}${prefix}/lib
            delete ${destroot}${prefix}/share/doc
            delete ${destroot}${prefix}/share/otb/description
            delete ${destroot}${prefix}/LICENSE
            delete ${destroot}${prefix}/README
            file rename ${destroot}${prefix}/share/otb/swig \
                ${destroot}${prefix}/share/otb/swig-py${v}

            # https://trac.macports.org/ticket/39671
            xinstall -m 0644 ${filespath}/__init__.py \
                ${destroot}/${py_pkgd}/otbApplication
            reinplace "s|@PREFIX@|${prefix}|" \
                ${destroot}/${py_pkgd}/otbApplication/__init__.py
        }

        livecheck.type  none
    }
}

post-destroot {
    delete ${destroot}${prefix}/tools
    delete ${destroot}${prefix}/recompile_bindings.sh
    delete ${destroot}${prefix}/otbenv.profile
    delete ${destroot}${prefix}/share/otb/swig
}

if {${subport} eq ${name}} {
    post-destroot {
        file rename ${destroot}${prefix}/share/doc/otb ${destroot}${prefix}/share/otb/doc
        file rename ${destroot}${prefix}/LICENSE ${destroot}${prefix}/share/otb/doc/LICENSE
        file rename ${destroot}${prefix}/README ${destroot}${prefix}/share/otb/doc/README
    }

    notes-append "
        To enable the OrfeoToolbox provider plugin in QGIS (version <3.36):
         set in 'Preferences >  Processing > Providers > OTB' the entries
        'OTB folder' to '${prefix}' and 'OTB application folder' to
        '${prefix}/lib/otb/applications'.
        "
}

livecheck.type      regex
livecheck.url       ${master_sites}
livecheck.regex     OTB-(\[0-9.\]+)[quotemeta ${extract.suffix}]
