PKG_ID := $(shell yq -r .id manifest.yaml)
PKG_VERSION := $(shell yq -r .version manifest.yaml)

TS_FILES := $(shell find ./scripts -name \*.ts 2>/dev/null)

.DELETE_ON_ERROR:

all: verify

verify: $(PKG_ID).s9pk
	start-sdk verify s9pk $(PKG_ID).s9pk

install: $(PKG_ID).s9pk
	start-cli package install $(PKG_ID).s9pk

$(PKG_ID).s9pk: manifest.yaml instructions.md icon.png LICENSE scripts/embassy.js image.tar
	start-sdk pack

scripts/embassy.js: $(TS_FILES)
	deno run --allow-read --allow-write --allow-env --allow-net scripts/bundle.ts

image.tar: Dockerfile docker_entrypoint.sh
	docker buildx build --tag start9/$(PKG_ID)/main:$(PKG_VERSION) \
		--platform=linux/arm64/v8,linux/amd64 \
		--build-arg ARCH=$(shell uname -m) \
		-o type=oci,dest=image.tar .

clean:
	rm -f $(PKG_ID).s9pk image.tar scripts/embassy.js

.PHONY: all verify install clean
