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

_realname='glab'
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.135.0
pkgrel=1
pkgdesc='CLI tool to help work seamlessly with GitLab from the command line (mingw-w64)'
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://gitlab.com/gitlab-org/cli'
msys2_repository_url='https://gitlab.com/gitlab-org/cli'
msys2_references=(
  'anitya: 127208'
  'archlinux: glab'
  'gentoo: dev-util/gitlab-cli'
  'purl: pkg:golang/gitlab.com/gitlab-org/cli/cmd/glab'
)
license=('spdx:MIT')
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-go"
)
optdepends=(
  "${MINGW_PACKAGE_PREFIX}-git: To interact with repositories"
)
options=('!strip')
source=("https://gitlab.com/gitlab-org/cli/-/archive/v${pkgver}/cli-v${pkgver}.tar.gz")
sha256sums=('ecfd255fea77df300a8d68493c63021ae2bfb5e40ef1751c6f697ada4e1c06c2')

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
  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"
}
