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

# Enable use of 'macports-libcxx' for macOS 10.11 and earlier, as port uses
# libcxx features normally only available on 10.12 and later.
# See: https://trac.macports.org/ticket/64055
legacysupport.newest_darwin_requires_legacy 15
legacysupport.use_mp_libcxx yes

PortGroup               qt5 1.0
PortGroup               github 1.0
PortGroup               cmake 1.1
PortGroup               compiler_blacklist_versions 1.0
PortGroup               gpg_verify 1.0

name                    KeePassXC
categories              security aqua
maintainers             nomaintainer
description             KeePassXC is a cross-platform community-driven port \
                        of the Windows application “Keepass Password Safe”.
long_description        KeePassXC is a community fork of KeePassX with the \
                        goal to extend and improve it with new features and \
                        bugfixes to provide a feature-rich, fully \
                        cross-platform and modern open-source password manager.
license                 {GPL-2 GPL-3}
license_noconflict      openssl openssl10 openssl11 openssl3

if { ${os.platform} eq "darwin" && ${os.major} >= 24 } {
    macosx_deployment_target 14.0
}

if {${subport} eq ${name}} {
    # stable
    github.setup        keepassxreboot keepassxc 2.7.9
    revision            0
    github.tarball_from releases
    distname            keepassxc-${version}-src
    use_xz              yes
    distfiles-append    ${distname}${extract.suffix}.sig
    worksrcdir          keepassxc-${version}

    conflicts           KeePassXC-devel

    # See keepassxc-${version}-src.tar.xz.DIGEST on upstream GitHub releases page for SHA256 sums
    checksums           ${distname}${extract.suffix} \
                        rmd160  0ac59defa184907036007f57d55e97ab8b422ce6 \
                        sha256  3c44e45f22c00ddac63d8bc11054b4b0ada0222ffac08d3ed70f196cb9ed46fd \
                        size    9789312 \
                        ${distname}${extract.suffix}.sig \
                        rmd160  1ca16506c6bd2abbf3e30e3cb91b5d9a06139ec0 \
                        sha256  f3a51f4cbb7f4b3574cf3201d84427ebec59461eb5db516065bf282ba9b16a8b \
                        size    488

    gpg_verify.use_gpg_verification \
                        yes
}

subport ${name}-devel {
    github.setup        keepassxreboot keepassxc 37dabd2561c33d7c0e66bba6ab0883bb55e90cf1
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    set githash         [string range ${github.version} 0 6]
    version             20230514.git${githash}
    revision            2

    conflicts           KeePassXC

    checksums           rmd160  16d79aa775a0dc6d8cf32015a3dc7131a2e13163 \
                        sha256  05fb0c84e426f33bacd41f6809d52c5bc758dace52b41c58001385a26bed89c3 \
                        size    10967053

    gpg_verify.use_gpg_verification \
                        no
}

if {[option gpg_verify.use_gpg_verification]} {
    post-checksum {
        # The public key is downloaded from [1], and the key ID is listed on [2].
        # [1] https://keepassxc.org/keepassxc_master_signing_key.asc
        # [2] https://keepassxc.org/verifying-signatures/
        set gpg_keyid CFB4C2166397D0D2

        gpg_verify.verify_gpg_signature \
            ${filespath}/keyid-${gpg_keyid}.txt \
            ${distpath}/[lindex ${distfiles} 0].sig \
            ${distpath}/[lindex ${distfiles} 0]
    }
}

# CMakeLists.txt only checks for Qt 5.2.0 and later but, per the developers,
# "That version check is only for Linux OS. Every other platform we assume
# latest available Qt." Empirically, KeePassXC builds with Qt 5.8 and later
# but not with 5.7 and earlier.
# https://github.com/keepassxreboot/keepassxc/issues/6352
qt5.min_version         5.8.0
qt5.depends_component   qtmacextras qtsvg
qt5.depends_build_component \
                        qttools

depends_lib-append      port:argon2 \
                        port:botan \
                        port:libgcrypt \
                        port:libomp \
                        port:libsodium \
                        port:minizip \
                        port:readline \
                        port:qrencode \
                        port:ykpers \
                        port:zlib

patchfiles-append       patch-no-deployqt.diff \
                        patch-no-findpackage-path.diff \
                        add_support_for_old_macos.diff \
                        add_support_for_old_macos-2.diff

# error: no matching constructor for initialization of 'std::shared_ptr<unsigned char []>'
compiler.blacklist-append {clang < 1200}

compiler.cxx_standard   2017

configure.args-append \
    -DCMAKE_INSTALL_PREFIX=${applications_dir} \
    -DCMAKE_INSTALL_MANDIR=${prefix}/share/man \
    -DCMAKE_PREFIX_PATH=${qt_cmake_module_dir} \
    -DKEEPASSXC_BUILD_TYPE=Release \
    -DWITH_XC_BROWSER=ON \
    -DWITH_XC_KEESHARE=ON \
    -DWITH_XC_NETWORKING=ON \
    -DWITH_XC_SSHAGENT=ON \
    -DWITH_XC_YUBIKEY=ON \
    -DWITH_XC_UPDATECHECK=OFF \
    -DWITH_XC_DOCS=OFF

# QTest::addRow was introduced in Qt 5.9
if {[vercmp ${qt5.version} < 5.9]} {
    configure.args-append -DWITH_TESTS=OFF
} else {
    configure.args-append -DWITH_TESTS=ON
}

# Qt version 5.12.0 or higher is required for Passkeys support
if {[vercmp ${qt5.version} < 5.12]} {
    configure.args-append -DWITH_XC_BROWSER_PASSKEYS=OFF
} else {
    configure.args-append -DWITH_XC_BROWSER_PASSKEYS=ON
}

post-destroot {
    set kp_doc_path ${destroot}${prefix}/share/doc/${name}

    xinstall -d ${kp_doc_path}
    xinstall -W ${worksrcpath} COPYING LICENSE.BSD LICENSE.CC0 \
             LICENSE.GPL-2 LICENSE.GPL-3 LICENSE.LGPL-2.1 LICENSE.LGPL-3 \
             LICENSE.MIT LICENSE.NOKIA-LGPL-EXCEPTION LICENSE.OFL \
             ${kp_doc_path}

    set kp_app_path ${applications_dir}/KeePassXC.app/Contents/MacOS
    set kp_bin_path ${destroot}${prefix}/bin

    ln -s ${kp_app_path}/keepassxc-cli \
        ${kp_bin_path}/keepassxc-cli
    ln -s ${kp_app_path}/keepassxc-proxy \
        ${kp_bin_path}/keepassxc-proxy
}

test.run        yes
test.target     test ARGS="-V -E testautotype"
test.env        TMPDIR=/tmp

# Ignore betas, RCs, etc. - See: https://trac.macports.org/ticket/64956
github.livecheck.regex  {([0-9.]+)}