# -*- 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           qt6 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.8.0
    revision        0

    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  1c22d11b1862ee920c8550c0b7dc82ec5d6b7ace \
                    sha256  4ed4b77d375f021fc04648c257fedfb41dc13c6bd5c4965d8ded31284c9c653c \
                    size    10867052

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

    gpg_verify.use_gpg_verification \
                    yes

    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.8.0 requires Qt 6.5 due to QUtiMimeConverter

qt6.depends_lib \
                    qtsvg
qt6.depends_build \
                    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

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

compiler.cxx_standard   2020

configure.args-append \
                    -DCMAKE_INSTALL_PREFIX=${applications_dir} \
                    -DCMAKE_INSTALL_MANDIR=${prefix}/share/man \
                    -DCMAKE_PREFIX_PATH=${qt6.dir}/lib/cmake \
                    -DKPXC_FEATURE_BROWSER=ON \
                    -DKPXC_FEATURE_NETWORK=ON \
                    -DKPXC_FEATURE_SSHAGENT=ON \
                    -DKPXC_FEATURE_UPDATES=OFF \
                    -DKPXC_FEATURE_DOCS=OFF \
                    -DWITH_TESTS=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.]+)}
