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

_realname=wasmer
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.3.5
pkgrel=1
pkgdesc="Universal Binaries Powered by WebAssembly (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64')
url="https://github.com/wasmerio/wasmer"
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-nodejs"
             'git')
source=("git+${url}.git#tag=v${pkgver}"
        "tun-tap.tar.gz::https://crates.io/api/v1/crates/tun-tap/0.1.4/download"
        "tun-tap-update-tokio.patch"
        "mingw-build.patch"
        "reproducible-builds.patch")
sha256sums=('9d82eba29ea066ba762d0994b3986dcb1cf99deaefe851ccd33a12bb1a2dab37'
            '9a477a4e9367c72ac875d23cd07ac99ffa932497d8428767fed0cfa27bbabe50'
            '096cb9b0a778b55d5bc57a784d6c707e3b3af2442cac6c09e757dfe3ea742148'
            '2da427fb3a7f929fa54cde5bcbd8ee906606b6f458ea7cd5732e6f1281cb5354'
            '7f08b41e0077a23b16eb2e9410ac953b293d1eeeaf1b83915a596ac437f81125')

prepare() {
  cd "${_realname}"
  rm -f rust-toolchain

  patch -Np1 -i "${srcdir}/reproducible-builds.patch"
  patch -Np1 -i "${srcdir}/mingw-build.patch"
  # update tokio in tun-tap dependency so outdated winapi@0.2.8, kerenel32-sys and ws2_32-sys are dropped
  rm ../tun-tap-0.1.4/Cargo.lock && mv -f ../tun-tap-0.1.4/Cargo.toml.orig ../tun-tap-0.1.4/Cargo.toml
  patch -d ../tun-tap-0.1.4 -Np1 -i "${srcdir}/tun-tap-update-tokio.patch"
  sed -i 's|^tun-tap = { version = "0.1.3"|tun-tap = { version = "0.1.4"|' lib/cli/Cargo.toml
  cat >> Cargo.toml <<END

[patch.crates-io]
tun-tap = { path = "../tun-tap-0.1.4" }
END

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

build() {
  cd "${_realname}"

  export OPENSSL_NO_VENDOR=1
  export WINAPI_NO_BUNDLED_LIBRARIES=1
  export WASMER_INSTALL_PREFIX=${MINGW_PREFIX}
  make ENABLE_SINGLEPASS=1 ENABLE_CRANELIFT=0
}

# tests require rustup binary
# check() {
#   cd "${_realame}"
#   make test-examples
# }

package() {
  cd "${_realname}"

  install -Dm755 target/release/wasmer "${pkgdir}${MINGW_PREFIX}/bin/wasmer"
  install -Dm755 target/release/wasmer.dll "${pkgdir}${MINGW_PREFIX}/bin/libwasmer-4.dll"
  install -Dm755 target/release/wasmer.dll.a "${pkgdir}${MINGW_PREFIX}/lib/libwasmer.dll.a"
  install -Dm755 target/release/libwasmer.a "${pkgdir}${MINGW_PREFIX}/lib/libwasmer.a"
  install -Dm644 lib/c-api/wasm.h "${pkgdir}${MINGW_PREFIX}/include/wasm.h"
  install -Dm644 lib/c-api/wasmer.h "${pkgdir}${MINGW_PREFIX}/include/wasmer.h"
  install -Dm644 lib/c-api/wasmer_wasm.h "${pkgdir}${MINGW_PREFIX}/include/wasmer_wasm.h"
  target/release/wasmer config --pkg-config | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/wasmer.pc"
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/wasmer/LICENSE"
}
