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

github.setup        git-ecosystem git-credential-manager 2.9.1 v

categories          security devel
supported_archs     arm64 x86_64
platforms           {darwin >= 21}
license             MIT
maintainers         {dports @drkp} openmaintainer

description         Secure, cross-platform Git credential storage with authentication \
                    to GitHub, Azure Repos, Bitbucket, and GitLab
long_description    Git Credential Manager (GCM) is a secure Git credential helper \
                    built on .NET that runs on macOS, Windows, and Linux. It provides \
                    multi-factor authentication support for GitHub, Azure Repos, \
                    Bitbucket, and GitLab, and stores credentials in the macOS keychain.

homepage            https://github.com/git-ecosystem/git-credential-manager

# The .NET tool package includes the application dependencies, but not the
# runtime. Generate only its native launcher locally; no NuGet restore is needed.
github.tarball_from releases
distname            ${name}.${version}
use_zip             yes
extract.suffix      .nupkg
extract.mkdir       yes

checksums           rmd160  ace385638efc7124e2855c3842cdcf8bb66eacf1 \
                    sha256  9428301c8d759958d4ced07dbe15bfbe4101495c0c909e4a95344680584d3a3f \
                    size    48454612

depends_build       port:dotnet-sdk-10
depends_lib         port:dotnet-runtime-10
depends_run         bin:git:git

universal_variant   no
use_configure       no

set gcm_payload     ${worksrcpath}/tools/net10.0/any
set gcm_core_dir    ${prefix}/share/gcm-core

post-extract {
    copy ${filespath}/launcher.proj ${filespath}/global.json ${worksrcpath}
    delete ${gcm_payload}/DotnetToolSettings.xml
    foreach d [glob ${gcm_payload}/runtimes/*] {
        if {![string match osx* [file tail ${d}]]} {
            delete ${d}
        }
    }
}

build.cmd           ${prefix}/bin/dotnet
build.target        msbuild
build.args          launcher.proj \
                    -nologo \
                    -verbosity:minimal \
                    -nodeReuse:false

build.env           DOTNET_CLI_HOME=${workpath} \
                    DOTNET_CLI_TELEMETRY_OPTOUT=1 \
                    DOTNET_NOLOGO=1 \
                    DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1 \
                    DOTNET_CLI_WORKLOAD_UPDATE_NOTIFY_DISABLE=true \
                    DOTNET_CLI_USE_MSBUILD_SERVER=0

destroot {
    xinstall -d ${destroot}${gcm_core_dir}
    copy {*}[glob ${gcm_payload}/*] ${destroot}${gcm_core_dir}

    fs-traverse f ${destroot}${gcm_core_dir} {
        if {[file isfile ${f}]} {
            file attributes ${f} -permissions 0644
        }
    }
    foreach f [glob ${destroot}${gcm_core_dir}/runtimes/osx*/native/*.dylib] {
        file attributes ${f} -permissions 0755
    }
    file attributes ${destroot}${gcm_core_dir}/git-credential-manager -permissions 0755

    xinstall -d ${destroot}${prefix}/bin
    ln -s ${gcm_core_dir}/git-credential-manager \
        ${destroot}${prefix}/bin/git-credential-manager
}

notes "
To finish setting up ${name}, configure Git to use it as the credential\
helper by running:

    git-credential-manager configure

To remove it from your Git configuration before uninstalling, run:

    git-credential-manager unconfigure
"

# Only match stable release tags (e.g. v2.8.0), not -beta/-rc prereleases.
github.livecheck.regex {(\d+(?:\.\d+)+)}
