# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=tea
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.11.0
pkgrel=1
pkgdesc='A command line tool to interact with Gitea servers (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://gitea.com/gitea/tea'
msys2_references=(
  'anitya: 377482'
  'archlinux: tea'
  'aur: gitea-tea-git'
  'gentoo: dev-util/tea'
)
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-go" "${MINGW_PACKAGE_PREFIX}-cc" "git")
optdepends=("git: To interact with repositories")
options=('!strip')
source=("$_realname-$pkgver.tar.gz::${url}/archive/v${pkgver}.tar.gz")
sha256sums=('278bbdf2e197f6f80a838e09574e8a950de535f0ba0f53154d26930a3adfaaa6')

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  cp -r "${srcdir}/${_realname}" "${srcdir}/build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}" && mkdir build

  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"
  case "${CARCH}" in
    i686|x86_64)
      GOFLAGS+=" -buildmode=pie"
      ;;
  esac

  ${MINGW_PREFIX}/bin/go build -v \
    -trimpath \
    -mod=readonly \
    -modcacherw \
    -ldflags "-linkmode=external -compressdwarf=false -X main.Version=${pkgver}" \
    -o build \
    .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  install -Dm755 "build/tea.exe" "${pkgdir}${MINGW_PREFIX}/bin/tea.exe"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 README.md "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/README.md"
  install -Dm644 contrib/autocomplete.sh "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/tea"
  install -Dm644 contrib/autocomplete.zsh "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_tea"
}
