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

_realname=wasm-bindgen
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=0.2.104
pkgrel=1
pkgdesc="Facilitating high-level interactions between Wasm modules and JavaScript (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/rustwasm/wasm-bindgen'
msys2_references=(
  'purl: pkg:cargo/wasm-bindgen'
)
license=('spdx:Apache-2.0 OR MIT')
depends=("${MINGW_PACKAGE_PREFIX}-rust-wasm"
         "${MINGW_PACKAGE_PREFIX}-nodejs")
makedepends=("${MINGW_PACKAGE_PREFIX}-mdbook"
             'git')
source=("git+${url}.git#tag=${pkgver}"
        "Cargo.lock")
sha256sums=('4333b76d261b1320887576412abc3375b8405674355518f88275e7d38a006f3d'
            '75f807ae511a3540c5e346dc0f1ec16a81e364cad3cfefafa570189f52e0364d')

prepare() {
  cd "${_realname}"

  # https://github.com/rustwasm/wasm-bindgen/issues/1819
  cp ../Cargo.lock .
  # remove an example that pulls deps triggering security alerts
  sed -i '/"examples\/raytrace-parallel",/d' Cargo.toml

  cargo fetch --locked \
    --config='net.git-fetch-with-cli=true' \
    --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${_realname}"

  cargo build --release --frozen -p wasm-bindgen-cli

  cd guide
  mdbook build
}

check() {
  cd "${_realname}"

  cargo test --release --frozen -p wasm-bindgen-cli
}

package_wasm-bindgen() {
  cd "${_realname}"

  cargo install \
    --offline \
    --no-track \
    --frozen \
    --path crates/cli \
    --root "${pkgdir}${MINGW_PREFIX}"

  install -Dm644 LICENSE-{APACHE,MIT} -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
}

package_wasm-bindgen-docs() {
  pkgdesc+=" (Documentation)"
  depends=()

  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html/"
  cp -r -t "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html/" "${_realname}"/guide/book/html/*
}

# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
