# 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.106
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=('b9181e21b775b6b0ebad117c261e3bac3d36b3e5fdab9583f703c0bd328b3940'
            '650805d49d602d252cf2fd3dec3ddbd09cce3e5b5f410f8da958408ca23a7877')

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 "${RUST_CHOST}"
}

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
