# tinywallet - the browser wallet, its worker, and the tests that do not need a browser.
#
# The Go toolchain is picked from GOROOT when it is set; TinyGo builds the wasm side. The
# nostrino channel worker is built from ../nostrino so the two bundles sit together.

GO     ?= go
TINYGO ?= tinygo
BIN    ?= bin
WALLET ?= $(BIN)/tinywallet.wasm
CHANNEL ?= $(BIN)/nostrino-channel.wasm
NOSTRINO ?= ../nostrino

ifeq ($(wildcard $(GOROOT)/bin/go),)
  ifneq ($(wildcard $(HOME)/.local/goroot/bin/go),)
    GOROOT := $(HOME)/.local/goroot
  endif
endif
export GOROOT
export PATH := $(GOROOT)/bin:$(PATH)

.PHONY: all test race vet fmt wasm channel web clean

all: test wasm

## test: the host tests, which cover everything but the browser edges
test:
	$(GO) test ./...

## race: the same, with the race detector, which the store and domain tests want
race:
	$(GO) test -race ./...

vet:
	$(GO) vet ./...

fmt:
	gofmt -l .

## wasm: the wallet worker
wasm:
	@mkdir -p $(BIN)
	$(TINYGO) build -target wasm -o $(WALLET) ./cmd/tinywallet-wasm

## channel: the light-client worker, built from the repository beside this one
channel:
	@mkdir -p $(BIN)
	cd $(NOSTRINO) && $(TINYGO) build -target wasm -o $(abspath $(BIN))/nostrino-channel.wasm ./cmd/nostrino-channel

## web: assemble both wasm bundles and the page in bin/web
web: wasm channel
	@mkdir -p $(BIN)/web
	cp $(WALLET) $(CHANNEL) $(BIN)/web/
	cp "$$($(TINYGO) env TINYGOROOT)/targets/wasm_exec.js" $(BIN)/web/wasm_exec.js
	cp web/index.html web/wallet.js web/worker.js $(BIN)/web/
	@echo "serve: cd $(BIN)/web && python3 -m http.server 8080"

clean:
	rm -rf $(BIN)
