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

_realname=rust-bindgen
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.72.0
pkgrel=1
pkgdesc='Automatically generates Rust FFI bindings to C (and some C++) libraries (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://rust-lang.github.io/rust-bindgen/'
msys2_repository_url='https://github.com/rust-lang/rust-bindgen'
msys2_references=(
  'archlinux: rust-bindgen'
  'purl: pkg:cargo/bindgen'
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-clang" "${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-mdbook")
source=("${msys2_repository_url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('1da7050a17fdab0e20d5d8c20d48cddce2973e8b7cb0afc15185bfad22f8ce5b')

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

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

build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  "${MINGW_PREFIX}/bin/cargo.exe" build \
    --release \
    --frozen

  cd book
  "${MINGW_PREFIX}/bin/mdbook.exe" build
}

check() {
  cd "${srcdir}/${_realname}-${pkgver}"

  "${MINGW_PREFIX}/bin/cargo.exe" test \
    --release \
    --locked
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"

  install -Dm755 "target/release/bindgen.exe" "${pkgdir}${MINGW_PREFIX}/bin/bindgen.exe"
  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  cp -a "book/book" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html"
  install -Dm644 "LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
