#!/bin/sh
# Download the self-hosted web assets at their pinned versions into ../site/vendor,
# mirroring where they live on the server. A file is skipped if already fetched (or
# already uploaded, i.e. in ../site_uploaded/vendor); to re-fetch one (e.g. after
# changing a pinned version) delete it from both places.
# Run from anywhere: sh web/fetch.sh
set -e
mkdir -p "$(dirname "$0")/../../site/vendor"
cd "$(dirname "$0")/../../site/vendor"

RUBY_WASM_VERSION=2.9.3-2.9.4   # @ruby/wasm-wasi + @ruby/3.3-wasm-wasi

fetch() {
  [ -e "$1" ] || [ -e "../../site_uploaded/vendor/$1" ] || curl -fsSL -o "$1" "$2"
}

# d3 + graphviz (graphviz wasm is base91-embedded in graphviz.umd.js, so no separate .wasm)
fetch d3.v7.min.js    "https://d3js.org/d3.v7.min.js"
fetch graphviz.umd.js "https://unpkg.com/@hpcc-js/wasm@2.20.0/dist/graphviz.umd.js"
fetch d3-graphviz.js  "https://unpkg.com/d3-graphviz@5.6.0/build/d3-graphviz.js"

# ruby.wasm loader + the core interpreter (no stdlib; iogii needs no stdlib gems)
fetch browser.umd.js  "https://cdn.jsdelivr.net/npm/@ruby/wasm-wasi@${RUBY_WASM_VERSION}/dist/browser.umd.js"
fetch ruby.wasm       "https://cdn.jsdelivr.net/npm/@ruby/3.3-wasm-wasi@${RUBY_WASM_VERSION}/dist/ruby.wasm"
