# -*- 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           kde4    1.1
PortGroup           boost   1.0

#Fetch from git repository to follow updates after the end of KDE4 public releases
fetch.type          git
git.url             https://github.com/KDE/akonadi.git
git.branch          c733429f

name                akonadi
set akoversion      1.13.1
version             ${akoversion}.20141210
revision            6
categories          devel kde kde4
maintainers         nomaintainer
license             LGPL-2+
description         A personal information management storage service.
long_description    Akonadi is an extensible cross-desktop storage service \
for PIM data and metadata providing concurrent read, write, and query access.
platforms           darwin
homepage            https://community.kde.org/KDE_PIM/Akonadi

depends_lib-append  port:soprano \
                    port:shared-mime-info

compiler.cxx_standard 2011

post-patch {
    reinplace "s/c++0x/c++11/" ${worksrcpath}/CMakeLists.txt
}

configure.args-append  -DPOSTGRES_PATH=Off
# mute akonadiserver which is usually overly chatty:
configure.cflags-append     -DQT_NO_DEBUG_OUTPUT
configure.cxxflags-append   -DQT_NO_DEBUG_OUTPUT

set startup_root ""
post-destroot {
    #Create a simple startup item
    xinstall -d -m 755 ${destroot}${prefix}/Library/LaunchDaemons
    set plist [open "${destroot}${prefix}/Library/LaunchDaemons/org.macports.akonadi.plist" w 0644]

    puts ${plist} "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
    puts ${plist} "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
    puts ${plist} "<plist version=\"1.0\">"
    puts ${plist} "<dict>"
    puts ${plist} "<key>Label</key>"
    puts ${plist} "<string>org.macports.akonadi</string>"
    puts ${plist} "<key>ProgramArguments</key>"
    puts ${plist} "<array>"
    puts ${plist} "<string>${prefix}/bin/akonadiserver-script.sh</string>"
    puts ${plist} "</array>"
    puts ${plist} "<key>Disabled</key>"
    puts ${plist} "<true/>"
    puts ${plist} "<key>RunAtLoad</key>"
    puts ${plist} "<true/>"
    puts ${plist} "</dict>"
    puts ${plist} "</plist>"
    close ${plist}

    #Create the script to launch server (needed to set the path, so that
    #akonadictl can find akonadi_control
    xinstall -d -m 755 ${destroot}${prefix}/bin
    set scriptFl [open "${destroot}${prefix}/bin/akonadiserver-script.sh" w 0755]
    puts ${scriptFl} "#!/bin/sh"
    puts ${scriptFl} "export PATH=${prefix}/bin:${prefix}/sbin:\$PATH"
    puts ${scriptFl} "${prefix}/bin/akonadictl start"
    close ${scriptFl}

    # 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/LaunchDaemons
        ln -s ${prefix}/Library/LaunchDaemons/org.macports.akonadi.plist ${destroot}${startup_root}/Library/LaunchDaemons
    }

    set docdir ${prefix}/share/doc/${name}
    xinstall -d ${destroot}${docdir}
    xinstall -m 0644 -W ${worksrcpath} AUTHORS NEWS README README.sqlite \
        ${destroot}${docdir}
}

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

notes "Applications using akonadi need the akonadi server to be running to function.
To start the server manually, run
 akonadictl start &
Otherwise, an agent to start the server at logon is provided.
To start it run the following command:
 launchctl load -w ${startup_root}/Library/LaunchDaemons/org.macports.akonadi.plist"

variant no_root description {Run the akonadi server start as MacPorts install user.} {
    pre-fetch {
        if { ${install.user}=="root" || ${install.group}=="wheel" } {
            ui_error "The akonadi server 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
}

variant mysql51 \
   conflicts sqlite mysql55 mysql56 mariadb55 \
   description {build with mysql51 port} {

   depends_lib-append      port:qt4-mac-mysql51-plugin
   depends_run-append      port:mysql51-server
   configure.args-append   -DDATABASE_BACKEND=MYSQL \
                           -DMYSQLD_EXECUTABLE=${prefix}/lib/mysql51/bin/mysqld
}

variant mysql55 \
   conflicts sqlite mysql51 mysql56 mariadb55 \
   description {build with mysql55 port} {

   depends_lib-append      port:qt4-mac-mysql55-plugin
   depends_run-append      port:mysql55-server
   configure.args-append   -DDATABASE_BACKEND=MYSQL \
                           -DMYSQLD_EXECUTABLE=${prefix}/lib/mysql55/bin/mysqld
}

variant mysql56 \
   conflicts sqlite mysql51 mysql55 mariadb55 \
   description {build with mysql56 port} {

   depends_lib-append      port:qt4-mac-mysql56-plugin
   depends_run-append      port:mysql56-server
   configure.args-append   -DDATABASE_BACKEND=MYSQL \
                           -DMYSQLD_EXECUTABLE=${prefix}/lib/mysql55/bin/mysqld
}

variant mariadb55 \
   conflicts sqlite mysql51 mysql55 mysql56 \
   description {build with mariadb port} {

   depends_lib-append      port:qt4-mac-mariadb-plugin
   depends_run-append      port:mariadb-server
   configure.args-append   -DDATABASE_BACKEND=MYSQL \
                           -DMYSQLD_EXECUTABLE=${prefix}/lib/mariadb/bin/mysqld
}

variant sqlite \
   conflicts mysql51 mysql55 mysql56 mariadb55 \
   description {Use sqlite backend instead of MySQL} {

   depends_lib-append      port:qt4-mac-sqlite3-plugin          
   configure.args-append   -DDATABASE_BACKEND=SQLITE \
                           -DMYSQLD_EXECUTABLE=Off
}

if {![variant_isset mysql51] && ![variant_isset mysql55] \
    && ![variant_isset mysql56] && ![variant_isset mariadb55] \
    && ![variant_isset sqlite]} {
    default_variants +mariadb55
}

post-patch {
    fs-traverse item ${workpath}/${distname}/server {
        if {[file isfile ${item}]} {
            reinplace "/include/s@\\(utils\\.h\\)@src/\\1@g" ${item}
        }
    }
    reinplace "s|/Applications|${applications_dir}|" ${worksrcpath}/CMakeLists.txt
}

variant verbose description {Enable debug output (chatty!)} {
    configure.cflags-delete     -DQT_NO_DEBUG_OUTPUT
    configure.cxxflags-delete   -DQT_NO_DEBUG_OUTPUT
}

platform darwin {
    #Deactivate tests as they require full C++11 support (ticket #45300)
    configure.args-append -DAKONADI_BUILD_TESTS:BOOL=OFF -DBUILD_TESTING:BOOL=OFF
    if {${os.major} == 9} {
        if {[info exists universal_target] && ${universal_target} == "10.4"} {
        # Needed if compiling for 10.4 universal on 10.5
            post-configure {
                reinplace "s|#define HAVE_EXECINFO_H 1||" ${workpath}/build/config-akonadi.h
            }
        }
    }
}

livecheck.type      none
