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

_realname=mdbook-pikchr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.2.0
pkgrel=1
pkgdesc='A mdbook preprocessor to render pikchr code blocks as images in your book (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://crates.io/crates/mdbook-pikchr'
msys2_repository_url='https://github.com/podsvirov/mdbook-pikchr'
msys2_references=(
  'aur: mdbook-pikchr'
  'purl: pkg:cargo/mdbook-pikchr'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-mdbook")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust")
source=("${msys2_repository_url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('dcdee1bf8f90bac02eb80412f862e4337979ba06d7eae513aa7119820f0f02ad')

prepare() {
  cp -r "${_realname}-${pkgver}" "build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  sed -e "s/https:\/\/raw.githubusercontent.com\/podsvirov\/mdbook-pikchr\/master\/docs\///" -i docs/README.md

  cargo fetch --locked --target "${RUST_CHOST}"
}

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

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

  msg "Build documentation"
  "${MINGW_PREFIX}/bin/mdbook.exe" build
}

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

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

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

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