# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=glab
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.44.1
pkgrel=1
pkgdesc='Cli tool to help work seamlessly with GitLab from the command line (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gitlab.com/gitlab-org/cli"
msys2_references=(
  'archlinux: glab'
)
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-go"
             "${MINGW_PACKAGE_PREFIX}-cc")
optdepends=("git: To interact with repositories")
options=('!strip')
source=("https://gitlab.com/gitlab-org/cli/-/archive/v${pkgver}/cli-v${pkgver}.tar.gz")
sha256sums=('59502833637aadb7c88dadc3c2f826cbe185a8fe7c0b90d82ccaca1da9d5dcf6')

build() {
    cd "cli-v${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 CGO_ENABLED=1
    export GO_LDFLAGS="-s -w"
    export GOFLAGS="-trimpath -mod=readonly -modcacherw -ldflags=-linkmode=external"
    case "${CARCH}" in
      i686|x86_64)
        GOFLAGS+=" -buildmode=pie"
        ;;
    esac

    make GLAB_VERSION="${pkgver}" build manpage
    bin/glab completion -s bash | install -Dm644 /dev/stdin bash-completion/completions/glab
    bin/glab completion -s zsh | install -Dm644 /dev/stdin zsh/site-functions/_glab
    bin/glab completion -s fish | install -Dm644 /dev/stdin fish/vendor_completions.d/glab.fish
}

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

package() {
    cd "cli-v${pkgver}"
    install -Dm755 "bin/glab" "${pkgdir}${MINGW_PREFIX}/bin/glab.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/glab "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/glab"
    install -Dm644 zsh/site-functions/_glab "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_glab"
    install -Dm644 fish/vendor_completions.d/glab.fish "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/glab.fish"
}
