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

github.setup        petasis tkdnd 2.9.4 tkdnd-release-test-v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
revision            1
categories          x11
license             BSD
maintainers         {mcalhoun @MarcusCalhoun-Lopez} {gmx.us:chrischavez @chrstphrchvz} openmaintainer
description         an extension that adds native drag & drop capabilities to the tk toolkit
long_description    Tk Drag & Drop: tkdnd is an extension that adds native drag & drop capabilities to the tk toolkit.
homepage            http://www.ellogon.org/petasis/index.php/tcltk-projects/tkdnd

checksums           rmd160  4491343d260d7e3693667a60563e7e8a1f90fc1d \
                    sha256  2f119db4ec61dfaa397300b68b5e5fa753b09748811f440b5ffa56f03aa47920 \
                    size    1358814

cmake.install_prefix ${prefix}/lib

# do not:
#     find macOS Tcl/Tk,
#     build universal, or
#     use garbage collection
patchfiles-append   patch-CMakeLists.txt.diff

configure.args-append \
    -DTCL_INCLUDE_PATH:PATH=${prefix}/include

depends_lib-append port:tcl

if {${os.platform} eq "darwin" && ${os.subplatform} eq "macosx"
    && ${os.major} >= 10 && ![string match ppc* ${build_arch}]} {

    variant quartz conflicts x11 {
        depends_lib-append  port:tk-quartz
        configure.args-append \
            -DTK_INCLUDE_PATH:PATH=${prefix}/include/tk-quartz \
            -DTK_WISH:PATH=${prefix}/libexec/tk-quartz/wish \
            -DTK_STUB_LIBRARY:PATH=${prefix}/lib/tk-quartz/libtkstub8.6.a \
            -DTK_LIBRARY:PATH=${prefix}/lib/tk-quartz/libtk.dylib
        # garbage collection is still available for ${os.major} < 16
        # ARC is available for ${os.major} > 10
        # in Xcode < 10, ARC forbids Objective-C objects in struct
        #     see https://developer.apple.com/videos/play/wwdc2018/409/?time=229
        #     see https://trac.macports.org/ticket/59058
        if {${os.major} >= 16} {
            compiler.blacklist-append {clang < 1000}
        }
        post-patch {
            reinplace "s|MACPORTS_APPLE|APPLE|g" \
                ${worksrcpath}/CMakeLists.txt
            if {${os.major} < 16} {
                reinplace "s|#MACPORTS_DO_NOT_USE_GC||g" \
                    ${worksrcpath}/CMakeLists.txt
            } else {
                reinplace "s|#MACPORTS_DO_NOT_USE_ARC||g" \
                    ${worksrcpath}/CMakeLists.txt
    
            }
        }
    }
    if {![variant_isset x11]} {
        default_variants +quartz
    }
    set x11conflicts quartz
} else {
    default_variants +x11
    set x11conflicts {}
}

variant x11 conflicts {*}${x11conflicts} {
    depends_lib-append \
        port:tk-x11 \
        port:xorg-libX11 \
        port:xorg-libXext \
        port:xorg-libice \
        port:xorg-libsm

    configure.args-append \
        -DTK_INCLUDE_PATH:PATH=${prefix}/include/tk-x11 \
        -DTK_WISH:PATH=${prefix}/libexec/tk-x11/wish \
        -DTK_STUB_LIBRARY:PATH=${prefix}/lib/tk-x11/libtkstub8.6.a \
        -DTK_LIBRARY:PATH=${prefix}/lib/tk-x11/libtk.dylib
}

pre-configure {
    if {!([variant_exists quartz] && [variant_isset quartz]) && ![variant_isset x11]} {
        error "Either +x11 or +quartz is required"
    }
}
