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

version             2.96.0
revision            0

# Github CLI is failing to build on 10.12 and below.
# So in that case, use the pre-built binary tarball.
if {${os.major} >= 17} {
    set source_build yes
} else {
    set source_build no
}

if ${source_build} {
    PortGroup       golang 1.0

    go.setup        github.com/cli/cli ${version} v
    go.package      github.com/cli/cli/v2
} else {
    PortGroup       github 1.0
    PortGroup       legacysupport 1.1

    github.setup    cli cli ${version} v

    supported_archs x86_64
}

name                gh

homepage            https://cli.github.com

description         GitHub’s official command line tool

long_description    ${name} is GitHub on the command line. It brings pull \
                    requests, issues, and other GitHub concepts to the \
                    terminal next to where you are already working with git \
                    and your code.

categories          devel
installs_libs       no
license             MIT
maintainers         {gmail.com:herby.gillot @herbygillot} \
                    openmaintainer

if ${source_build} {
    # Allow Go to fetch dependencies at build time
    go.offline_build no
    build.cmd           make
    build.pre_args-append \
                        GH_VERSION=${version}
    build.args          bin/${name} manpages

    use_parallel_build  no

    checksums           rmd160  54e7b08f0b38a3a0279232e644666a0c16881a47 \
                        sha256  8d80d0aeccea7bec8024f8c30365bbfa76852901f2b2cb0afb7ab2cbf6d317c2 \
                        size    14796760

    dist_subdir         ${name}

    patch {
        # Do not override GOOS, GOARCH, GOARM and other environment variables
        reinplace -E \
            {s|GOOS= GOARCH= GOARM= GOFLAGS= CGO_ENABLED= go build|go build|g} \
            ${worksrcpath}/Makefile
    }

} else {
    github.tarball_from releases

    build {}

    distname        gh_${version}_macOS_amd64

    checksums       rmd160  baf2cd6c736c5f9cba9a9a9c1a43f54560957c1c \
                    sha256  4bd449df9ad639391bc62b8032546f0fe9edcd8526e06682a4f88abd8c5d163c \
                    size    15298430

    use_configure   no
    use_zip         yes
}

destroot {
    xinstall -m 0755 -W ${worksrcpath} bin/gh ${destroot}${prefix}/bin

    if {!$source_build} {
        # standard legacysupport tweaks don't work, since the install here is
        # from a binary tarball ... have to tweak the binary to use the legacy
        # support library, which in turn uses the System.B library.
        # Add legacysupport for all non-source versions for utimensat() support
        system -W ${destroot}${prefix}/bin "install_name_tool -change /usr/lib/libSystem.B.dylib ${prefix}/lib/libMacportsLegacySystem.B.dylib gh"
    }

    set docdir ${prefix}/share/doc/${name}
    xinstall -d ${destroot}${docdir}
    xinstall -m 0644 -W ${worksrcpath} LICENSE ${destroot}${docdir}

    # Man pages
    xinstall -d ${destroot}${prefix}/share/man/man1
    xinstall -m 0644 {*}[glob ${worksrcpath}/share/man/man1/*] \
        ${destroot}${prefix}/share/man/man1

    # Shell completions
    xinstall -d ${destroot}${prefix}/share/bash-completion/completions
    exec ${destroot}${prefix}/bin/gh completion -s bash >> \
        ${destroot}${prefix}/share/bash-completion/completions/gh

    xinstall -d ${destroot}${prefix}/share/zsh/site-functions
    exec ${destroot}${prefix}/bin/gh completion -s zsh >> \
        ${destroot}${prefix}/share/zsh/site-functions/_gh

    xinstall -d ${destroot}${prefix}/share/fish/vendor_completions.d
    exec ${destroot}${prefix}/bin/gh completion -s fish >> \
        ${destroot}${prefix}/share/fish/vendor_completions.d/gh.fish
}

github.livecheck.regex {([0-9.]+)}
