# -*- 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           active_variants 1.1

name                gtk-osx-application
conflicts           ige-mac-integration
set my_name         gtk-mac-integration
version             3.0.2
revision            1

categories          devel
license             LGPL-2.1
maintainers         {mascguy @mascguy} openmaintainer

description         Mac OS X menu bar integration library for GTK2 quartz
long_description \
                    A simple library whose purpose is to \
                    allow GTK quartz applications to integrate with \
                    the Mac OS X menu bar. Requires gtk2 or gtk3 and its \
                    dependencies to be built with variant +quartz

homepage            https://wiki.gnome.org/Projects/GTK%2B/OSX/Integration

set branch          [join [lrange [split ${version} .] 0 1] .]
master_sites        gnome:sources/gtk-mac-integration/${branch}/
distname            ${my_name}-${version}
use_xz              yes

checksums           sha256  42f29e002365467eac10f4ba78435d4be785a947424d9890112c8c8d5e21be25 \
                    rmd160  31738aa3fad57b8419805734c67321f967bf50f3 \
                    size    361576

# Fix build error on older macOS releases:
#   GtkApplicationDelegate.c:76:83: error: expected '>' before '*' token
#      76 | -(void) application: (NSApplication*)theApplication openFiless: (NSArray<NSString *> *) files
#         |                                                                                   ^
#   GtkApplicationDelegate.c:76:1: error: cannot find protocol declaration for 'NSString'
#      76 | -(void) application: (NSApplication*)theApplication openFiless: (NSArray<NSString *> *) files
#         | ^
# See: https://trac.macports.org/ticket/71548
patchfiles-append   patch-gtk-app-delegate.diff

depends_build-append \
                    port:pkgconfig

proc gtk_version {ver} {
    global          gtk_version subport_dir

    set gtk_version ${ver}
    set subport_dir gtkmacintegration-${gtk_version}
}

gtk_version         gtk2

pre-configure {

  if {${name} eq ${subport}} {
    ui_error "
The gtk-osx-application port has been replaced by 'gtk-osx-application-gtk2'
and 'gtk-osx-application-gtk3'.
Please `sudo port deactivate gtk-osx-application`
followed by `sudo port upgrade -u outdated` or another command to update.
Then if all works as intended `sudo port uninstall gtk-osx-application`
"
    return -code error
  }

  if {![active_variants $gtk_version quartz ""]} {
    ui_error "
${subport} is meant to be used only in a GTK quartz
development environment but your version of GTK does not
support quartz.  Please make sure that port $gtk_version and all its
dependencies are built with variants +quartz and try again.
"
    error "$gtk_version +quartz not installed."
  }
}
 
configure.ccache    no

subport ${name}-gtk3 {
    gtk_version      gtk3

    depends_lib-append \
                    path:lib/pkgconfig/gtk+-3.0.pc:gtk3 \
                    path:include/${subport_dir}/gtkmacintegration/gtk-mac-bundle.h:gtk-osx-application-common-gtk3

    # force gtk version
    configure.args-append \
                    --with-gtk3 \
                    --without-gtk2

    configure.args-append \
                    --enable-python=no

    post-destroot {
        delete ${destroot}${prefix}/include
        delete ${destroot}${prefix}/lib/pkgconfig/gtk-mac-integration.pc
        delete ${destroot}${prefix}/share/${subport_dir}/locale
        delete ${destroot}${prefix}/share/${subport_dir}/strings
    }
}

subport ${name}-common-gtk3 {
    gtk_version      gtk3

    depends_lib-append \
                    path:lib/pkgconfig/gtk+-3.0.pc:gtk3

    # force gtk version
    configure.args-append \
                    --with-gtk3 \
                    --without-gtk2

    configure.args-append \
                    --enable-python=no

    post-destroot {
        delete ${destroot}${prefix}/lib
        delete ${destroot}${prefix}/share/${subport_dir}/gir-1.0
    }
}

subport ${name}-gtk2 {
    gtk_version      gtk2

    depends_lib-append \
                    path:lib/pkgconfig/gtk+-2.0.pc:gtk2 \
                    path:include/${subport_dir}/gtkmacintegration/gtk-mac-bundle.h:gtk-osx-application-common-gtk2

    # force gtk version
    configure.args-append \
                    --with-gtk2 \
                    --without-gtk3 \
                    --disable-introspection

    configure.args-append \
                    --enable-python=no

    post-destroot {
        delete ${destroot}${prefix}/include
        delete ${destroot}${prefix}/lib/pkgconfig/gtk-mac-integration.pc
        delete ${destroot}${prefix}/share/${subport_dir}/locale
        delete ${destroot}${prefix}/share/${subport_dir}/strings
    }
}

subport ${name}-common-gtk2 {
    gtk_version      gtk2

    depends_lib-append \
                    path:lib/pkgconfig/gtk+-2.0.pc:gtk2

    # force gtk version
    configure.args-append \
                    --with-gtk2 \
                    --without-gtk3 \
                    --disable-introspection

    configure.args-append \
                    --enable-python=no

    post-destroot {
        delete ${destroot}${prefix}/lib
    }
}

# NOTE: This must be done after all subport declarations, as we're dependent on value of 'subport_dir'
configure.cmd-append \
                    --includedir=${prefix}/include/${subport_dir} \
                    --libdir=${prefix}/lib/${subport_dir} \
                    --datarootdir=${prefix}/share/${subport_dir} \
                    --with-pkgconfigdir=${prefix}/lib/pkgconfig

if {${subport} eq ${name}} {
    livecheck.type  gnome
    livecheck.name  ${my_name}
} else {
    livecheck.type  none
}
