NAME := vex-starfall-rust
WASM := target/wasm32-unknown-unknown/release/starfall.wasm
CART := $(NAME).wasm
BUNDLE := bundle/$(NAME)
BUNDLE_SRC := $(BUNDLE)/src
SCP_TARGET := c7.se:/var/www/play.c7.se/vex/
PUBLIC_URL := https://play.c7.se/vex/$(NAME)/

.PHONY: all build bundle deploy clean

all: build

build:
	cargo cart

# vex-web derives the bundle directory name from the cart's file name, so
# stage the wasm under the name we deploy to. vex-web only copies ./src on its
# own, so stage both crates (and the workspace files) into bundle/.../src.
bundle: build
	mkdir -p build
	cp $(WASM) build/$(CART)
	vex-web -bundle build/$(CART)
	mkdir -p $(BUNDLE_SRC)/starfall $(BUNDLE_SRC)/vex-sdk
	cp starfall/src/lib.rs starfall/Cargo.toml $(BUNDLE_SRC)/starfall/
	cp vex-sdk/src/lib.rs vex-sdk/build.rs vex-sdk/Cargo.toml $(BUNDLE_SRC)/vex-sdk/
	cp Cargo.toml Cargo.lock Makefile README.md vex.h $(BUNDLE_SRC)/
	cd bundle && rm -f $(NAME).zip && zip -qr $(NAME).zip $(NAME)

deploy: bundle
	scp -r $(BUNDLE) $(BUNDLE).zip $(SCP_TARGET)
	@echo '-> Uploaded to $(PUBLIC_URL)'

clean:
	rm -rf build bundle
	cargo clean
