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

_realname=wasmer
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.4.0
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')
options=('!lto')
source=("git+${url}.git#tag=v${pkgver}"
        "corosensei.tar.gz::https://crates.io/api/v1/crates/corosensei/0.1.4/download"
        "tun-tap.tar.gz::https://crates.io/api/v1/crates/tun-tap/0.1.4/download"
        "corosensei-fix-compilation.patch"
        "tun-tap-update-tokio.patch"
        "reproducible-builds.patch")
sha256sums=('1ab4b4b98cd1442b8b23336f78f7e0f2e167985bd50c5f923dbc6eeee56a9d1d'
            '80128832c58ea9cbd041d2a759ec449224487b2c1e400453d99d244eead87a8e'
            '9a477a4e9367c72ac875d23cd07ac99ffa932497d8428767fed0cfa27bbabe50'
            '1636cded85171dc138c68e81336823e2db1e269e744c428426ab05157b41cf34'
            '684f6a479385e3646b82d5d6588d9290a78708a59dec4aab7af76a0f23aaa249'
            '7f08b41e0077a23b16eb2e9410ac953b293d1eeeaf1b83915a596ac437f81125')

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

  patch -Np1 -i "../reproducible-builds.patch"
  # update old dependencies in corosensei and tun-tap crates so outdated winapi@0.2.8,
  # kernel32-sys, ws2_32-sys and windows-sys@0.33.0 are dropped; fix compile error in corosensei
  mv -f ../tun-tap-0.1.4/Cargo.toml.orig ../tun-tap-0.1.4/Cargo.toml
  mv -f ../corosensei-0.1.4/Cargo.toml.orig ../corosensei-0.1.4/Cargo.toml
  patch -d ../corosensei-0.1.4 -Np1 -i "../corosensei-fix-compilation.patch"
  patch -d ../tun-tap-0.1.4 -Np1 -i "../tun-tap-update-tokio.patch"
  cat >> Cargo.toml <<END

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

  cargo update -p corosensei -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=1 ENABLE_LLVM=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"
}
