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

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.3.3
revision            1
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/

# stealth update; remove for next release
dist_subdir         ${dist_subdir}/${version}_1

checksums           rmd160  079d8614d170c423a9965c28da0f3abc91856010 \
                    sha256  903937d6475878df1e2130eee32d1fd93c4597bd2ef7f94e1d9775da1839645d \
                    size    18007438

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

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=""

    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}

    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}

    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
}
