# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=wakatime
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.132.1
pkgrel=1
pkgdesc="Command line interface used by all WakaTime text editor plugins (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url="https://github.com/wakatime/wakatime-cli"
msys2_references=(
  'aur: wakatime'
  'purl: pkg:golang/github.com/wakatime/wakatime-cli'
)
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-go"
             "${MINGW_PACKAGE_PREFIX}-cc"
             'git')
source=("git+${url}.git#tag=v${pkgver}")
sha256sums=('4217f3efbf710f274e4db0b520711198dda5f85166c01ecab9774e99cbc59631')

build() {
  # provide date but make it reproducible
  local _date="$(date --utc --date="@${SOURCE_DATE_EPOCH:-$(date +%s)}" '+%Y-%m-%dT%H:%M:%S %Z')"
  local _repo="${url#https://}"

  cd "${_realname}-cli"

  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"

  go build -o "build-${MSYSTEM}/${_realname}.exe" \
    -trimpath \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-linkmode external -extldflags '${LDFLAGS}' \
    -X '${_repo}/pkg/version.OS=$(go env GOOS)' \
    -X '${_repo}/pkg/version.Arch=$(go env GOARCH)' -X '${_repo}/pkg/version.BuildDate=${_date}' \
    -X '${_repo}/pkg/version.Commit=$(git rev-parse --short HEAD)' \
    -X '${_repo}/pkg/version.Version=${pkgver}'"
}

package() {
  cd "${_realname}-cli"

  install -Dm755 "build-${MSYSTEM}/${_realname}.exe" -t "${pkgdir}${MINGW_PREFIX}/bin"
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
