# -*- 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           conflicts_build 1.0
PortGroup           legacysupport 1.1

# https://trac.macports.org/ticket/70676 
# fatal error: use of undeclared identifier 'AT_FDCWD'
legacysupport.newest_darwin_requires_legacy 13

if {${subport} eq "gmic-gimp" || ${subport} eq "gmic-qt"} {
    PortGroup       cmake           1.1
    PortGroup       qt5             1.0
    PortGroup       active_variants 1.1
} else {
    PortGroup       makefile        1.0

    patchfiles      patch-src-Makefile.diff
}

name                gmic
version             3.6.3
revision            0
license             CeCILL
categories          science graphics
maintainers         {mps @Schamschula} openmaintainer

description         G'MIC is an interpreter of image processing macros

long_description    G'MIC stands for GREYC's Magic Image Converter. It is \
                    focused on the design of possibly complex pipelines for \
                    converting, manipulating, filtering and visualizing \
                    generic 1d/2d/3d multi-spectral image datasets. This \
                    includes classical color images, but also more complex \
                    data as image sequences or 3D volumetric images.

homepage            https://gmic.eu
master_sites        https://gmic.eu/files/source/

checksums           rmd160  67aae064aeb2aa29e586f3dc0c670ed983298caa \
                    sha256  45304f179f2200ba527c819bd911bd6a85c4a4999740c730c7ccdd164ce240ac \
                    size    20183375

distfiles           ${name}_${version}${extract.suffix}

depends_lib         port:curl \
                    path:lib/libavcodec.dylib:ffmpeg \
                    port:fftw-3 \
                    port:GraphicsMagick \
                    port:ilmbase \
                    port:imath \
                    port:libheif \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:libpng \
                    port:openexr \
                    port:tiff \
                    port:zlib

compiler.blacklist              {clang < 902}
compiler.cxx_standard           2011

# Uses more memory per compile process than we can accommodate on our
# automated build system. See https://github.com/dtschump/gmic/issues/325
#use_parallel_build              no
# See: https://trac.macports.org/ticket/72933
build.mem_per_job   2048

subport ${name}-qt {
    description-append          (GUI application)

    configure.args-append       -DGMIC_QT_HOST=none
}

subport ${name}-gimp {
    description-append          (GIMP plug-in).

    configure.args-append       -DGMIC_QT_HOST=gimp
    depends_lib-append          path:lib/pkgconfig/gimp-2.0.pc:gimp2
}

subport ${name}-clib {
    description-append          (C library).
}

subport ${name}-lib {
    description-append          (C++ library).
}

if {${subport} eq ${name}} {
    description-append          (Stand-alone binary).

    depends_build-append        port:pkgconfig

    depends_lib-append          port:${name}-lib

    build.dir                   ${worksrcpath}/src
    build.target                cli

    installs_libs               no

    variant opencv4 description {compile with OpenCV support} {
        depends_lib-append      path:lib/opencv4/libopencv_core.dylib:opencv4
    }

    variant x11 {
        depends_lib-append      port:xorg-libX11 \
                                port:xorg-libXext \
                                port:xorg-libXcursor \
                                port:xorg-libice \
                                port:xorg-libsm
    }
    default_variants-append     +x11

    pre-build {
        # see ${worksrcpath}/src/Makefile
        if {[variant_isset x11]} {
            set x11_cflags     "-Dcimg_display=1 [exec ${prefix}/bin/pkg-config --cflags x11]"
            set x11_libs       "[exec ${prefix}/bin/pkg-config --libs x11] -lpthread"
        } else {
            set x11_cflags     "-Dcimg_display=0"
            set x11_libs       "-lpthread"
        }
        build.args-append       X11_CFLAGS="${x11_cflags}" \
                                X11_LIBS="${x11_libs}"
    }
    build.args-append           NO_STDLIB=Yes \
                                OPT_CFLAGS=""

    if {${os.platform} eq "darwin" && ${os.major} < 11} {
        build.args-replace      OPT_CFLAGS="" OPT_CFLAGS="-I${prefix}/include/LegacySupport"
        build.args-append       OPT_LIBS="-L${prefix}/lib -lMacportsLegacySupport"
    }

    post-destroot {
        delete ${destroot}${prefix}/include/gmic_libc.h
        delete ${destroot}/plug-ins
    }

    pre-activate {
        if {![catch {set installed [lindex [registry_active gmic] 0]}]} {
            set _version [lindex $installed 1]
            if {[vercmp $_version 3.1.1] < 0} {
                registry_deactivate_composite gmic "" [list ports_nodepcheck 1]
            }
        }
    }

    livecheck.url               https://gmic.eu/files/source/
    livecheck.regex             ${name}_(\[0-9.\]+)${extract.suffix}
} elseif {${subport} eq "${name}-clib"} {
    set soversion               1

    depends_build-append        port:pkgconfig

    build.dir                   ${worksrcpath}/src
    build.target                libc

    makefile.override-append    PREFIX
    makefile.prefix_name        USR

    # parallel build fails
    use_parallel_build          no

    variant x11 {
        depends_lib-append      port:xorg-libX11
    }
    default_variants-append     +x11

    pre-build {
        # see ${worksrcpath}/src/Makefile
        if {[variant_isset x11]} {
            set x11_cflags     "-Dcimg_display=1 [exec ${prefix}/bin/pkg-config --cflags x11]"
            set x11_libs       "[exec ${prefix}/bin/pkg-config --libs x11] -lpthread"
        } else {
            set x11_cflags     "-Dcimg_display=0"
            set x11_libs       "-lpthread"
        }
        build.args-append       X11_CFLAGS="${x11_cflags}" \
                                X11_LIBS="${x11_libs}"
    }
    build.args-append           NO_STDLIB=Yes \
                                OPT_CFLAGS="" \
                                SOVERSION=${soversion}

    if {${os.platform} eq "darwin" && ${os.major} < 11} {
        build.args-replace      OPT_CFLAGS="" OPT_CFLAGS="-I${prefix}/include/LegacySupport"
        build.args-append       OPT_LIBS="-L${prefix}/lib -lMacportsLegacySupport"
    }

    destroot {
        xinstall -m 0644 ${worksrcpath}/src/gmic_libc.h                 ${destroot}${prefix}/include
        xinstall -m 0644 ${worksrcpath}/src/libcgmic.a                  ${destroot}${prefix}/lib
        xinstall -m 0755 ${worksrcpath}/src/libcgmic.${soversion}.dylib ${destroot}${prefix}/lib
        ln       -s      libcgmic.${soversion}.dylib                    ${destroot}${prefix}/lib/libcgmic.dylib
    }

    livecheck.type  none
} elseif {${subport} eq "${name}-lib"} {
    set soversion               1

    depends_build-append        port:pkgconfig

    build.dir                   ${worksrcpath}/src
    build.target                lib

    makefile.override-append    PREFIX
    makefile.prefix_name        USR

    # parallel build fails
    use_parallel_build          no

    variant x11 {
        depends_lib-append      port:xorg-libX11
    }
    default_variants-append     +x11

    pre-build {
        # see ${worksrcpath}/src/Makefile
        if {[variant_isset x11]} {
            set x11_cflags     "-Dcimg_display=1 [exec ${prefix}/bin/pkg-config --cflags x11]"
            set x11_libs       "[exec ${prefix}/bin/pkg-config --libs x11] -lpthread"
        } else {
            set x11_cflags     "-Dcimg_display=0"
            set x11_libs       "-lpthread"
        }
        build.args-append       X11_CFLAGS="${x11_cflags}" \
                                X11_LIBS="${x11_libs}"
    }
    build.args-append           NO_STDLIB=Yes \
                                OPT_CFLAGS="" \
                                SOVERSION=${soversion}

    if {${os.platform} eq "darwin" && ${os.major} < 11} {
        build.args-replace      OPT_CFLAGS="" OPT_CFLAGS="-I${prefix}/include/LegacySupport"
        build.args-append       OPT_LIBS="-L${prefix}/lib -lMacportsLegacySupport"
    }

    destroot {
        xinstall -m 0644 ${worksrcpath}/src/gmic.h                      ${destroot}${prefix}/include
        xinstall -m 0644 ${worksrcpath}/src/libgmic.a                   ${destroot}${prefix}/lib
        xinstall -m 0755 ${worksrcpath}/src/libgmic.${soversion}.dylib  ${destroot}${prefix}/lib
        ln       -s      libgmic.${soversion}.dylib                     ${destroot}${prefix}/lib/libgmic.dylib
    }

    livecheck.type  none
} else {
    # CMakeLists.txt assumes variables CMAKE_CXX_FLAGS_${cmake.build_type}
    #    (CMAKE_CXX_FLAGS_DEBUG and CMAKE_CXX_FLAGS_RELEASE)
    if {[variant_isset debug]} {
        cmake.build_type        Debug
    } else {
        cmake.build_type        Release
    }

    qt5.min_version             5.11

    cmake.source_dir            ${worksrcpath}/gmic-qt

    patchfiles-append           patch-qt-CMakeLists.txt.diff \
                                patch-gmic-qt-src-FilterSelector-FiltersModelReader.cpp.diff

    configure.args-append       -DENABLE_DYNAMIC_LINKING=ON \
                                -DENABLE_LTO=OFF \
                                -DENABLE_SYSTEM_GMIC=OFF \
                                -DGMIC_PATH=${worksrcpath}/src

    qt5.depends_build_component qttools

    depends_lib-append          port:${name}-lib \
                                port:xorg-libX11 \
                                port:xorg-libXext \
                                port:xorg-libice \
                                port:xorg-libsm

    require_active_variants     ${name} x11

    platforms {darwin >= 12}

    if {${os.platform} eq "darwin" && ${os.major} < 17} {
        patchfiles-append       patch-gmic-qt-CMakeLists.txt.diff
    }

    livecheck.type  none
}
