# -*- 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           github 1.0
PortGroup           kde4 1.1
PortGroup           legacysupport 1.1
PortGroup           openssl 1.0

# Only wrappers
legacysupport.newest_darwin_requires_legacy 0
openssl.branch      1.1

# optional support for HFS-compression of the source and build dirs
if {[file exists ${cmake::currentportgroupdir}/compress_workdir-1.0.tcl]} {
    PortGroup       compress_workdir 1.0
}

name                kdelibs4
github.setup        KDE kdelibs 1c5b25966136b6099665477616fb4d38a98fd7ef
version             4.14.38.4
revision            1
categories          kde kde4
maintainers         nomaintainer
license             LGPL-2+ GPL-2+ BSD
description         KDE4 core libraries, git/master
long_description    Core libraries for KDE4 applications to function. \
                    This port follows git/master
homepage            https://www.kde.org
checksums           rmd160  638bd9454ec4b1f61cab494d5fb68564caa84830 \
                    sha256  7ede1891f658e2b1969e59f12e37d7ce70e211e93e0fc9593f629f0b8d50c409 \
                    size    15343423
github.tarball_from archive

# Well, we do not really know if all of these need wrappers.
# But some do:
# kbuildsycoca4(99281,0xa00d4620) malloc: *** error for object 0xa00a6754: pointer being freed was not allocated
legacysupport.redirect_bins \
                    kbuildsycoca4 kconfig_compiler kcookiejar4 kde4-config kdeinit4_shutdown kdeinit4_wrapper kjs kmailservice \
                    kshell4 ktelnetservice kunittestmodrunner kwrapper4 makekdewidgets meinproc4 meinproc4_simple nepomuk-rcgen

# no longer true with the OpenSSL 1.1 patch:
# No binary links to openssl libraries, apart from libkio.dylib at
# runtime if required. As libkio.dylib code is purely LGPL, there is no
# conflict with openssl license
# license_noconflict  openssl

depends_build-append \
                    port:docbook-xsl-ns \
                    port:flex \
                    port:gettext \
                    port:gmake

# dropped grantlee dependency because it's optional and used only for 1 test
depends_lib-append  port:aspell \
                    port:aspell-dict-en \
                    port:attica \
                    port:avahi \
                    port:bison \
                    port:bzip2 \
                    port:cyrus-sasl2 \
                    port:dbusmenu-qt \
                    port:docbook-xml \
                    port:docbook-xsl-nons \
                    port:enchant \
                    port:expat \
                    port:gettext-runtime \
                    path:lib/libgif:giflib \
                    path:lib/pkgconfig/glib-2.0.pc:glib2 \
                    port:jasper2 \
                    port:kerberos5 \
                    port:libart_lgpl \
                    port:libiconv \
                    port:libidn \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:libpng \
                    port:libxml2 \
                    port:libxslt \
                    port:qca \
                    port:pcre \
                    path:bin/perl:perl5 \
                    port:shared-desktop-ontologies \
                    port:shared-mime-info \
                    port:soprano \
                    port:strigi \
                    port:tiff \
                    port:xz \
                    port:zlib

# Even old openexr2 requires C++11 now, so avoid it.
# configure.pkg_config_path-prepend \
                    ${prefix}/libexec/openexr2/lib/pkgconfig
# Also avoid exiv2, its API changed way too much.
conflicts_build     exiv2 openexr openexr2

# the aspell dictionaries are just files
depends_skip_archcheck aspell-dict-en

conflicts           kdelibs3

# workaround-kdeinit4-crash: Mac-specific patch developed by Macports to circumvent some crashes.
# avoid-kwindowinfo-destructor: Prevent sending null pointer to CFRelease (see ticket #44258)
# default-kde4-xdg-menu-prefix: solves a conflict with a file from gnome-menus (see ticket #18914)
# add-bundles-to-path: improve support for KIO slaves by adding a search path
# kapplications-raster: ensures that kde applications start in raster (faster) mode, but also provides a switch for non-working applications
# removeFindFlex: remove FindFlex.cmake which may hide the working one of cmake (ticket #44119)
# patch-cmake-modules-FindKDE4-Internal.cmake: Fixes zlib detection (see ticket #24128)
# patch-cmake-modules-KDE4Macros.cmake: Ensures that compiled objects are properly detected during build (see ticket #43720)

patch.pre_args-replace  -p0 -p1

patchfiles          workaround-kdeinit4-crash.patch \
                    default-kde4-xdg-menu-prefix.patch \
                    add-bundles-to-path.patch \
                    patch-kapplications-raster.diff \
                    patch-removeFindFlex.diff \
                    patch-cmake-modules-FindKDE4-Internal.cmake.diff \
                    patch-cmake-modules-FindMySQL.cmake.diff

# RJVB
# fixCrashInKCrash.patch
patchfiles-append   fixInvokeBrowser.patch \
                    fixReportBug.patch \
                    streamline-systray-menu.patch \
                    correct-Preferences-Menu.patch \
                    nogui-patches.patch \
                    patch-improved-osx-keychain.diff \
                    patch-kdecore-tests.diff \
                    patch-backtrace-demangle-4osx.diff \
                    patch-kwindowsystem-4osx.diff \
                    patch-Display-typedef.diff \
                    patch-restore-prevactive-after-notification.diff \
                    patch-kmenu-addtitle.diff \
                    patch-kioslave-as-agent.diff \
                    patch-launch-lldb-not-gdb.diff \
                    patch-kidletime-poller.diff \
                    patch-solid-backend-iokit.diff \
                    patch-simulate-full_session.diff \
                    patch-mimetyperepo.diff \
                    patch-revert-a02df05e.diff \
                    patch-catch-fileopen.diff \
                    patch-aqua-widget-alignment.diff

variant nativefiledialogs description {File dialogs are native by default.\
                    Set Native=false under [KFileDialog Settings] in kdeglobalrc to use KDE dialogs} {}

if { [variant_isset nativefiledialogs] } {
    patchfiles-append   native-filedialogs.patch
}

variant unifiedtoolbar description {Add a unified title and tool bar} {}

if { [variant_isset unifiedtoolbar] } {
    patchfiles-append   unified-toolbar.patch
}

patchfiles-append   patch-no-concurrent-meinproc4.diff

# make sure findSoprano.cmake doesn't get confused because of moved soprano headers
patchfiles-append   patch-findsoprano-cmake.diff

# Fix build with modern compilers
patchfiles-append   patch-fix-compile.diff

# explicitly define certain headers and libraries, to avoid
# conflicts with those installed into system paths by the user.
configure.args-append   -DJASPER_LIBRARY_RELEASE=${prefix}/libexec/jasper2/lib/libjasper.dylib \
                        -DKDE_DEFAULT_HOME=Library/Preferences/KDE \
                        -DKDE4_AUTH_BACKEND_NAME="OSX" \
                        -DWITH_Avahi=ON \
                        -DWITH_ENCHANT=ON \
                        -DWITH_FAM=OFF \
                        -DWITH_HSPELL=OFF \
                        -DWITH_Soprano=ON

# Install the kdelibs headerfiles in their own directory to prevent clashes with KF5 headers
configure.args-append   -DINCLUDE_INSTALL_DIR=${kde4.include_dirs}

variant osxkeychain description {kwallet uses the OSX KeyChain} {}

if { [variant_isset osxkeychain] } {
    configure.args-append -DMAC_USE_OSXKEYCHAIN:BOOL=ON
}

variant nostrip description {Prevent stripping of binaries} {}

if { [variant_isset nostrip] } {
#     configure.args-delete -DCMAKE_BUILD_TYPE=Release
#     configure.args-append -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo -DCMAKE_STRIP:FILEPATH=/bin/echo
    ui_warn "you have requested an obsolete variant"
}

if { [file exists ${destroot}${prefix}/lib/kde4/kspell_aspell.so] } {
    notes "
Only the English dictionary has been installed.
You will need to install extra dictionaries to
get support for spell checking other languages.
The command 'port search aspell-dict' will show
you the currently available dictionary options.
"
}

set startup_root ""

post-patch {
    reinplace "s|%PREFIX%|${prefix}|g" \
        ${worksrcpath}/cmake/modules/FindMySQL.cmake
    # patch a number of other hardcoded paths to refer to ${prefix}, using a direct approach
    reinplace "s|/usr/local|${prefix}|g" \
        ${worksrcpath}/kdecore/kernel/kstandarddirs.cpp \
        ${worksrcpath}/kdecore/network/k3socks.cpp \
        ${worksrcpath}/kio/kio/ksambashare.cpp \
        ${worksrcpath}/kio/kssl/kopenssl.cpp \
        ${worksrcpath}/solid/solid/xdgbasedirs.cpp
    reinplace "s|@PREFIX@|${prefix}|g" \
        ${worksrcpath}/kdecore/services/kmimetyperepository.cpp
    reinplace "s|/usr/share|${prefix}/share|g" \
        ${worksrcpath}/kdeui/icons/kiconcache.cpp \
        ${worksrcpath}/kdeui/icons/kiconloader.cpp \
        ${worksrcpath}/kdeui/icons/kicontheme.cpp \
        ${worksrcpath}/sonnet/plugins/hunspell/kspell_hunspelldict.cpp \
        ${worksrcpath}/sonnet/plugins/hunspell/kspell_hunspellclient.cpp
}

pre-configure {
    #The FindFlex.cmake file is not working and may conflict with the one provided by cmake (ticket #44119)
    if {[file exists ${worksrcpath}/cmake/modules/FindFlex.cmake]} {
        delete file ${worksrcpath}/cmake/modules/FindFlex.cmake
    }
}
post-configure {
    catch {eval exec sh -c "\"printenv > ${workpath}/macports.env\""} result
    ui_debug "printenv: $result"
}

pre-activate {
    if {![catch {set vers [lindex [registry_active kde4-runtime] 0]}]
        && [vercmp [lindex $vers 1] 4.8.1] < 0} {
        registry_deactivate_composite kde4-runtime "" [list ports_nodepcheck 1]
    }
}

post-destroot {
    xinstall -d ${destroot}${prefix}/include/nepomuk
    eval xinstall -m 644 [glob ${worksrcpath}/nepomuk/core/*.h] \
        ${destroot}${prefix}/include/nepomuk
    eval xinstall -m 644 [glob ${worksrcpath}/nepomuk/types/*.h] \
        ${destroot}${prefix}/include/nepomuk
    eval xinstall -m 644 [glob ${worksrcpath}/nepomuk/nepomuk_export.h] \
        ${destroot}${prefix}/include/nepomuk

    xinstall -d -m 755 ${destroot}${prefix}/Library/LaunchAgents
    xinstall -m 644 ${filespath}/org.macports.kdecache.plist \
        ${destroot}${prefix}/Library/LaunchAgents
    reinplace "s|%PREFIX%|${prefix}|g" \
        ${destroot}${prefix}/Library/LaunchAgents/org.macports.kdecache.plist
    reinplace "s|%APPLICATIONS_DIR%|${applications_dir}|g" \
        ${destroot}${prefix}/Library/LaunchAgents/org.macports.kdecache.plist

    xinstall -m 755 ${filespath}/kdeinit4.sh ${destroot}${prefix}/bin/kdeinit4
    reinplace "s|@KDEAPPDIR@|${applications_dir}/KDE4|g" ${destroot}${prefix}/bin/kdeinit4

    # The startupitem.install keyword does not exist in MacPorts < 2.1.
    if {![info exists startupitem.install]} {
        set startupitem.install yes
    }
    if { ${startupitem.install} != "no" } {
        xinstall -d -m 755 ${destroot}${startup_root}/Library/LaunchAgents
        ln -s ${prefix}/Library/LaunchAgents/org.macports.kdecache.plist ${destroot}${startup_root}/Library/LaunchAgents
    }

    # The KDE4 PortGroup provides support for a (kf5compat) variant that installs to a legacy_prefix.
    # We provide a symlink that will allow ports to build using this "shadow prefix" (the legacy_prefix)
    # and then move the share directory back into the "real" ${prefix} ... but still find certain
    # resources at runtime. The reason for not leaving them in the legacy_prefix is that other XDG-compliant
    # software (including KDE ports not built with the legacy_prefix) will expect them in ${prefix}/share,
    # not in ${kde4.legacy_prefix}/share.
    xinstall -m 755 -d ${destroot}${kde4.legacy_prefix}
    ln -s ${prefix}/share ${destroot}${kde4.legacy_prefix}/share
}

post-activate {
    if { ![variant_isset no_root] } {
        file attributes ${prefix}/Library/LaunchAgents/org.macports.kdecache.plist -owner root -group wheel 
    }
}

notes-append "An agent to provide communication between KDE programs must be started.
To start it run the following command:
 launchctl load -w ${startup_root}/Library/LaunchAgents/org.macports.kdecache.plist"

variant no_root description {Run the kde cache agent as MacPorts install user.} {
    pre-fetch {
        if { ${install.user}=="root" || ${install.group}=="wheel" } {
            ui_error "The kde cache agent should not be run as root with no_root variant."
            error "Please do not use this variant with your MacPorts configuration."
        }
    }

    set startup_root  [join [lrange [exec /usr/bin/dscl . -read Users/${install.user} NFSHomeDirectory] 1 end]]
    # Files are installed into user's startup directory.
    destroot.violate_mtree  yes
}

post-activate {
    if {[info exists kde4::currentportgroupdir]} {
        set pgroup_files {"kde4-1.1.tcl"}
        ui_msg "Don't forget to copy the following file(s) into your main port tree's _resources directory:"
        foreach pg ${pgroup_files} {
            ui_msg "\t${kde4::currentportgroupdir}/${pg}"
        }
        ui_msg "(That's probably ${prefix}/var/macports/sources/rsync.macports.org/release/ports/_resources/port1.0/group"
        ui_msg "or ${prefix}/var/macports/sources/svn.macports.org/trunk/dports/_resources/port1.0/group)"
    }
}

test.run            yes

livecheck           none
