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

name                KeePassXC
categories          security aqua
maintainers         nomaintainer
license             {GPL-2 GPL-3}
license_noconflict  openssl openssl10 openssl11 openssl3

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.

if {${subport} eq ${name}} {
    # stable
    github.setup    keepassxreboot keepassxc 2.7.12
    revision        1

    github.tarball_from releases
    distname        keepassxc-${version}-src
    use_xz          yes
    distfiles-append \
                    ${distname}${extract.suffix}.sig
    worksrcdir      keepassxc-${version}

    # See keepassxc-${version}-src.tar.xz.DIGEST on upstream GitHub releases page for SHA256 sums
    checksums       ${distname}${extract.suffix} \
                    rmd160  6a4fbddc41aabeee8f825a1e75e120125b1ab4cf \
                    sha256  be34eeb297881adea4f894c7c6e4a1835bd7927f8043ddea495040df4792a7de \
                    size    10618652

     # simply check signature size
     checksums-append \
                    ${distname}${extract.suffix}.sig \
                    size    488

    gpg_verify.use_gpg_verification \
                    yes

    patchfiles-append \
                    add_support_for_old_macos.diff

    configure.args-append \
                    -DKEEPASSXC_BUILD_TYPE=Release
}

# Please remove after 2028-01-01
subport ${name}-devel {
    PortGroup       obsolete 1.0 

    replaced_by     KeePassXC

    github.setup    keepassxreboot keepassxc 36a26308d0cf8fc548a57e093b1fb3fc6ebe8a44
    set githash     [string range ${github.version} 0 6]
    version         20260309.git${githash}
    revision        0
}

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]
    }
}

# 2.7.11 requires Qt 5.12.0
# https://github.com/keepassxreboot/keepassxc/commit/df1035a31d8f934f31faf3dcd0b51cc947bfe0a8
qt5.min_version     5.12.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-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} \
                    -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 \
                    -DWITH_TESTS=ON \
                    -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.]+)}
