IMAGE ?= greptime-alpine-builder
RUNTIME_IMAGE ?= greptime-alpine
GREPTIMEDB_REF ?= main
ALPINE_VERSION ?= 3.23
RUST_VERSION ?= 1.90.0
OUT_DIR ?= dist

.PHONY: artifact image build inspect run clean

artifact:
	mkdir -p $(OUT_DIR)
	docker build \
		--target artifact \
		--build-arg GREPTIMEDB_REF=$(GREPTIMEDB_REF) \
		--build-arg ALPINE_VERSION=$(ALPINE_VERSION) \
		--build-arg RUST_VERSION=$(RUST_VERSION) \
		--output type=local,dest=$(OUT_DIR) .
	chmod +x $(OUT_DIR)/greptime

build:
	docker build \
		--target builder \
		--build-arg GREPTIMEDB_REF=$(GREPTIMEDB_REF) \
		--build-arg ALPINE_VERSION=$(ALPINE_VERSION) \
		--build-arg RUST_VERSION=$(RUST_VERSION) \
		-t $(IMAGE):$(GREPTIMEDB_REF) .

image:
	docker build \
		--target runtime \
		--build-arg GREPTIMEDB_REF=$(GREPTIMEDB_REF) \
		--build-arg ALPINE_VERSION=$(ALPINE_VERSION) \
		--build-arg RUST_VERSION=$(RUST_VERSION) \
		-t $(RUNTIME_IMAGE):$(GREPTIMEDB_REF) .

inspect: artifact
	file $(OUT_DIR)/greptime
	ldd $(OUT_DIR)/greptime || true
	ls -lh $(OUT_DIR)/greptime $(OUT_DIR)/greptime.debug

run: image
	docker run --rm -it \
		-p 127.0.0.1:4000-4003:4000-4003 \
		-v "$$(pwd)/greptimedb_data:/greptimedb_data" \
		$(RUNTIME_IMAGE):$(GREPTIMEDB_REF) \
		standalone start \
		--http-addr 0.0.0.0:4000 \
		--rpc-bind-addr 0.0.0.0:4001 \
		--mysql-addr 0.0.0.0:4002 \
		--postgres-addr 0.0.0.0:4003

clean:
	rm -rf $(OUT_DIR)