# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=zizmor
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.15.2
pkgrel=1
pkgdesc="A static analysis tool for GitHub Actions (mingw-w64)"
url='https://github.com/zizmorcore/zizmor'
msys2_repository_url='https://github.com/zizmorcore/zizmor'
msys2_changelog_url='https://docs.zizmor.sh/release-notes/'
msys2_references=(
  'anitya: 378497'
  'archlinux: zizmor'
  'purl: pkg:cargo/zizmor'
)
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=('spdx:MIT')
depends=(
  "${MINGW_PACKAGE_PREFIX}-cc-libs"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-rust"
)
source=("https://github.com/zizmorcore/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "https://github.com/zizmorcore/zizmor/commit/522d1ce0ed1fee1e7bc2a8cf897d3d8941654bc5.patch")
sha256sums=('a3d3a062ffd79f3958c7d428a9aeb8b6332d57bc3fb15bed242d519aa11e2f42'
            '81645a500dabefe041c6bcf9bbbdeff9a02b80b5ffc821da4ba0dad8cf4edd3b')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${_realname}-${pkgver}"

  # See https://github.com/zizmorcore/zizmor/pull/1254.
  apply_patch_with_msg \
    522d1ce0ed1fee1e7bc2a8cf897d3d8941654bc5.patch

  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${_realname}-${pkgver}"
  cargo build --frozen --release
}

check() {
  cd "${_realname}-${pkgver}"
  cargo test --frozen
}

package() {
  cd "${_realname}-${pkgver}"
  install -Dm 755 -t "${pkgdir}${MINGW_PREFIX}/bin" \
    target/release/zizmor.exe

  install -Dm 644 LICENSE -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
}
