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

name                    qca
version                 2.2.1

# Qt version handling logic
global Qt_Major
subport ${name}-qt5     {}

if {[string match "${name}-qt5*" ${subport}]} {
    PortGroup           qt5 1.0
    set Qt_Major        5
} else {
    PortGroup           qt4 1.0
    set Qt_Major        4
}
# best included after the required Qt PG
PortGroup               cmake 1.1

categories              devel crypto security
maintainers             {gmail.com:rjvbertin @RJVB} openmaintainer
license                 LGPL-2.1
description             Qt Cryptographic Architecture
long_description        Qt Cryptographic Architecture provides an easy API for the following \
                        features: SSL/TLS, X509, SASL, RSA, Hashing (SHA1, MD5), Ciphers \
                        (BlowFish, 3DES, AES).  Functionality is supplied via plugins.

platforms               darwin

homepage                https://userbase.kde.org/QCA
master_sites            https://download.kde.org/stable/qca/${version}
use_xz                  yes
checksums               rmd160  dddc3cf240dc5424b9df13fc1bf41c8e04f3b814 \
                        sha256  d716d2d8e3ed8d95bbdb061f03081d7d032206f746a30a4d29d72196f50e7b02 \
                        size    691676

depends_lib-append      port:botan \
                        port:libgcrypt \
                        port:nss

patch.pre_args          -Np1
patchfiles-append       patch-qca-ossl.diff \
                        patch-installname.diff \
                        patch-cmakeminver.diff

# the QCA plugins have been rolled into the main source tarball and are built by default
# when their dependencies are available. Allow this for all plugins that were not already
# available through an individual port; no reason to impose new dependencies on our dependents.
# This is mostly of interest for Qt5-based dependents, evidently.
configure.args          -DCMAKE_INSTALL_PREFIX:PATH=${qt_dir} \
                        -DQCA_PLUGINS_INSTALL_DIR:PATH=${qt_plugins_dir} \
                        -DQCA_LIBRARY_INSTALL_DIR:PATH=${qt_libs_dir} \
                        -DQCA_BINARY_INSTALL_DIR:PATH=${qt_bins_dir} \
                        -DQCA_FEATURE_INSTALL_DIR:PATH=${qt_mkspecs_dir}/features \
                        -DQCA_INCLUDE_INSTALL_DIR:PATH=${qt_includes_dir} \
                        -DQCA_PRIVATE_INCLUDE_INSTALL_DIR:PATH=${qt_includes_dir} \
                        -DQCA_DOC_INSTALL_DIR:PATH=${qt_docs_dir} \
                        -DQCA_MAN_INSTALL_DIR:PATH=${prefix}/share/man \
                        -DPKGCONFIG_INSTALL_PREFIX:PATH=${qt_pkg_config_dir} \
                        -DOSX_FRAMEWORK:BOOL=OFF \
                        -DBUILD_PLUGINS:STRING="botan\;gcrypt\;logger\;nss\;softstore"

if {${Qt_Major} eq "4"} {
    configure.args-append \
                        -DQT4_BUILD:BOOL=ON
} elseif {${Qt_Major} eq "5"} {
    patchfiles-append   patch-qca210-qt550.diff
    configure.args-append \
                        -DQCA_SUFFIX:STRING="qt5"
}

post-destroot {
    if {${subport} eq ${name} || ${subport} eq "${subport}-qt5"} {
        # install docs
        xinstall -d -m 755 ${destroot}${prefix}/share/doc/${subport}
        xinstall -m 644 -W ${worksrcpath} COPYING README TODO \
            ${destroot}${prefix}/share/doc/${subport}
    }

    if {${subport} eq "${name}"} {
        # install cmake file (taken from port 'kdelibs4'). To use this
        # file, include '-DCMAKE_MODULE_PATH=...' in configure.args during
        # the initial 'configure' stage (which uses CMake), to the full
        # path of where the CMake files are installed.
        xinstall -d -m 755 ${destroot}${qt_cmake_module_dir}
        xinstall -m 644 -W ${filespath} FindQCA2.cmake \
            ${destroot}${qt_cmake_module_dir}

    }
}

# avoid installing the examples into Qt's binary tree
variant examples description {Include examples in install} {
    post-destroot {
        xinstall -d -m 755 ${destroot}${prefix}/share/${subport}/examples/
        eval file copy [glob ${worksrcpath}/examples/*] \
            ${destroot}${prefix}/share/${subport}/examples/
    }
}

### Plugin subports:
### The ossl, cyrus-sasl and gnupg plugins used to be standalone ports
### are now implemented as subports. The other plugins are built together with
### the main port; I don't see a reason to introduce new subports for components
### that used to be built along with the main ports, only to impose new dependencies
### on the ports that require qca.
### 
# Qt4 receives no suffix:
set qt.versions     {"" "-qt5"}
foreach qv ${qt.versions} {
    subport ${name}${qv}-ossl {
        license                 LGPL-2.1+
        depends_lib-append      port:${name}${qv} port:openssl
        configure.args-delete   -DBUILD_PLUGINS:STRING="botan\;gcrypt\;logger\;nss\;softstore"
        configure.args-append   -DBUILD_PLUGINS:STRING="ossl"
        build.dir               ${workpath}/build/plugins/qca-ossl
    }
    subport ${name}${qv}-cyrus-sasl {
        license                 LGPL-2.1+
        depends_lib-append      port:${name}${qv} port:cyrus-sasl2
        configure.args-delete   -DBUILD_PLUGINS:STRING="botan\;gcrypt\;logger\;nss\;softstore"
        configure.args-append   -DBUILD_PLUGINS:STRING="cyrus-sasl"
        build.dir               ${workpath}/build/plugins/qca-cyrus-sasl
    }
    subport ${name}${qv}-gnupg {
        license                 LGPL-2.1+
        depends_lib-append      port:${name}${qv}
        configure.args-delete   -DBUILD_PLUGINS:STRING="botan\;gcrypt\;logger\;nss\;softstore"
        configure.args-append   -DBUILD_PLUGINS:STRING="gnupg"
        build.dir               ${workpath}/build/plugins/qca-gnupg
    }
    subport ${name}${qv}-pkcs11 {
        license                 LGPL-2.1+
        depends_lib-append      port:${name}${qv} port:openssl port:pkcs11-helper
        configure.args-delete   -DBUILD_PLUGINS:STRING="botan\;gcrypt\;logger\;nss\;softstore"
        configure.args-append   -DBUILD_PLUGINS:STRING="pkcs11"
        build.dir               ${workpath}/build/plugins/qca-pkcs11
    }
}
### Provide stub subports for those plugins that have been available through
### a subport for a while.
foreach virtual {"botan" "gcrypt" "logger" "nss" "softstore"} {
    subport ${name}-${virtual} {
        archive_sites
        distfiles
        depends_lib         port:${name}
        use_configure       no
        supported_archs     noarch
        patchfiles
        build {}
        destroot {
            set stubdir ${destroot}${prefix}/share/doc/${subport}
            xinstall -d -m 755 ${stubdir}
            system "touch ${stubdir}/.this.is.a.stubport"
        }
    }
}

livecheck.type  regex
livecheck.url   https://download.kde.org/stable/qca
livecheck.regex "(2\\.\\d+(?:\\.\\d+)+(?:\\.\\d+)*)\\/"
