# Maintainer: @naveen521kk on Github <naveen521kk@gmail.com>

_realname=github-cli
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.83.2
pkgrel=1
pkgdesc='The GitHub CLI (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://cli.github.com/"
msys2_repository_url="https://github.com/cli/cli/"
msys2_references=(
  'anitya: 135615'
  'archlinux: github-cli'
  'gentoo: dev-util/github-cli'
  'purl: pkg:golang/github.com/cli/cli'
  'purl: pkg:golang/github.com/cli/cli/v2'
)
license=('spdx:MIT')
depends=("winpty")
makedepends=("${MINGW_PACKAGE_PREFIX}-go" "${MINGW_PACKAGE_PREFIX}-cc" "git")
optdepends=("git: To interact with repositories")
options=('!strip')
source=("https://github.com/cli/cli/archive/v${pkgver}/$_realname-$pkgver.tar.gz"
        "gh")
sha256sums=('c031ca887d3aaccb40402a224d901c366852f394f6b2b60d1158f20569e33c89'
            '9ee5f2b44b7e9aa751508f02c1020e341e0212a9aa146b7428eb5ffea310be27')

build() {
    cd "cli-$pkgver"
    export GOOS=windows
    export GOROOT=${MINGW_PREFIX}/lib/go
    export CGO_CPPFLAGS="${CPPFLAGS}"
    export CGO_CFLAGS="${CFLAGS}"
    export CGO_CXXFLAGS="${CXXFLAGS}"
    export CGO_LDFLAGS="${LDFLAGS}"
    export GO_LDFLAGS="-s -w"
    export GOFLAGS="-trimpath -mod=readonly -modcacherw -ldflags=-linkmode=external"
    case "${CARCH}" in
      x86_64)
        GOFLAGS+=" -buildmode=pie"
        ;;
    esac

    make GH_VERSION="v$pkgver" bin/gh.exe manpages
    bin/gh.exe completion -s bash | install -Dm644 /dev/stdin bash-completion/completions/gh
    bin/gh.exe completion -s zsh | install -Dm644 /dev/stdin zsh/site-functions/_gh
    bin/gh.exe completion -s fish | install -Dm644 /dev/stdin fish/vendor_completions.d/gh.fish
}

check(){
    cd "cli-$pkgver"
    make test
}

package() {
    cd "cli-$pkgver"
    install -Dm755 "${srcdir}/gh" "${pkgdir}${MINGW_PREFIX}/bin/gh"
    install -Dm755 "bin/gh.exe" "${pkgdir}${MINGW_PREFIX}/bin/gh.exe"
    cp -r share/ "${pkgdir}${MINGW_PREFIX}/"
    install -Dm644 "LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
    install -Dm644 "README.md" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/README.md"
    install -Dm644 bash-completion/completions/gh "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/gh"
    install -Dm644 zsh/site-functions/_gh "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_gh"
    install -Dm644 fish/vendor_completions.d/gh.fish "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/gh.fish"
}
