# Simple task runner for WarpDrive plugins

WASM_TARGET := wasm32-unknown-unknown
PROFILE ?= release
INSTALL_DIR ?= plugins

PLUGIN_NAMES := grpc-auth http-echo ws-echo

ifeq ($(PROFILE),release)
PROFILE_FLAG := --release
PROFILE_DIR := release
else
PROFILE_FLAG := --profile $(PROFILE)
PROFILE_DIR := $(PROFILE)
endif

.PHONY: all wasm install clean run $(addprefix wasm-,$(PLUGIN_NAMES))

all: wasm

run:
	cargo run

wasm: $(addprefix wasm-,$(PLUGIN_NAMES))

wasm-grpc-auth:
	cargo build --target $(WASM_TARGET) $(PROFILE_FLAG) --manifest-path examples/plugins/grpc-auth/Cargo.toml

wasm-http-echo:
	cargo build --target $(WASM_TARGET) $(PROFILE_FLAG) --manifest-path examples/plugins/http-echo/Cargo.toml

wasm-ws-echo:
	cargo build --target $(WASM_TARGET) $(PROFILE_FLAG) --manifest-path examples/plugins/ws-echo/Cargo.toml

install: wasm
	mkdir -p $(INSTALL_DIR)
	cp -f examples/plugins/grpc-auth/target/$(WASM_TARGET)/$(PROFILE_DIR)/grpc_auth_plugin.wasm $(INSTALL_DIR)/
	cp -f examples/plugins/http-echo/target/$(WASM_TARGET)/$(PROFILE_DIR)/http_echo_plugin.wasm $(INSTALL_DIR)/
	cp -f examples/plugins/ws-echo/target/$(WASM_TARGET)/$(PROFILE_DIR)/ws_echo_plugin.wasm $(INSTALL_DIR)/

clean:
	@for plugin in $(PLUGIN_NAMES); do \
		cargo clean --manifest-path examples/plugins/$$plugin/Cargo.toml; \
	done
