SHELL := /bin/bash
VERSION := $(shell cat VERSION)
PREVIEW_PORT ?= 45989

.PHONY: fmt test vet build android-debug android-local-install browser-worker run smoke preflight package install-local status-local stop-local
.PHONY: test-selfhost source-archive
test-selfhost:
	python3 -m unittest discover -s scripts -p selfhost_test.py

source-archive:
	python3 scripts/source-archive.py --output dist/vimatee-source-$(VERSION).tar.gz
fmt:
	gofmt -w ./cmd ./internal ./web

test:
	go test -race ./...

vet:
	go vet ./...

build:
	mkdir -p dist
	CGO_ENABLED=0 go build -trimpath -o dist/relay-core ./cmd/relay-core
	CGO_ENABLED=0 go build -trimpath -o dist/relay-device ./cmd/relay-device
	CGO_ENABLED=0 go build -trimpath -o dist/vimatee-llm-bridge ./cmd/vimatee-llm-bridge

android-debug:
	cd android && ./gradlew --no-daemon :app:assembleDebug

android-local-install:
	curl --fail --silent --show-error --max-time 5 --output /dev/null "http://127.0.0.1:$(PREVIEW_PORT)/app/"
	adb get-state
	$(MAKE) android-debug
	adb reverse tcp:38082 tcp:$(PREVIEW_PORT)
	adb install -r android/app/build/outputs/apk/debug/app-debug.apk
	adb shell am start -n com.vimalinx.vimatee.local/com.vimalinx.vimatee.MainActivity

browser-worker:
	docker build -t vimatee-browser-worker:local browser-worker

run:
	go run ./cmd/relay-core

smoke:
	./scripts/smoke.sh

preflight:
	./scripts/preflight.sh

package: test vet build
	./scripts/package.sh

install-local:
	./scripts/install-local.sh

status-local:
	docker compose ps

stop-local:
	docker compose stop
