#!/bin/bash
set -e

webgl_flag=""

TEMP=$(getopt --long -o "g" "$@")
eval set -- "$TEMP"
while true; do
  case "$1" in
  -g)
    webgl_flag="web-webgl,"
    shift 2
    ;;
  *)
    break
    ;;
  esac
done

export RUSTUP_TOOLCHAIN=nightly-2021-12-07-x86_64-unknown-linux-gnu

rustup component add rust-src --toolchain nightly-2021-12-07-x86_64-unknown-linux-gnu

function plain_build() {
  cargo build --features "$webgl_flag" --target wasm32-unknown-unknown -Z build-std=std,panic_abort
  # TODO: Untested: --reference-types
  wasm-bindgen --target web --out-dir dist/mapr-pain-bindgen target/wasm32-unknown-unknown/debug/mapr.wasm
}

function wasm_pack_build() {
  ~/Downloads/wasm-pack-v0.10.1-x86_64-unknown-linux-musl/wasm-pack build . \
        --release --target web --out-dir dist/mapr -- \
        --features "$webgl_flag" -Z build-std=std,panic_abort
}

wasm_pack_build

#xdg-open "https://localhost:5555/web/mapr.html"
#python3 tools/tls-http-server.py
