S3_ENDPOINT ?= https://s3-hcm-r2.s3cloud.vn
S3_BUCKET ?= s3://omicslab/data/tools

WHEEL := dist/omicslab-1.4.0-py3-none-any.whl
WHEEL_S3_NAME := $(notdir $(WHEEL))

# Tools hosted on S3 for cloud VM bootstrap
TOOLS := nextflow goofys
TOOL_ARCHIVES := awscliv2.zip singularity.tar.gz

.PHONY: build upload upload-wheel upload-pixi upload-tools upload-tool-archives all clean

build: ## Build the SDK wheel
	pixi run python -c "import hatchling.build; hatchling.build.build_wheel('dist/')"

upload-wheel: build ## Build + upload wheel to S3
	aws s3 cp $(WHEEL) $(S3_BUCKET)/$(WHEEL_S3_NAME) --endpoint-url $(S3_ENDPOINT) --acl public-read

upload-pixi: ## Upload pixi config with wheel URL reference
	sed 's|omicslab = { path = ".", editable = true }|omicslab = { url = "$(S3_ENDPOINT)/omicslab/data/tools/$(WHEEL_S3_NAME)" }|' pixi.toml > /tmp/pixi.toml
	aws s3 cp /tmp/pixi.toml $(S3_BUCKET)/pixi.toml --endpoint-url $(S3_ENDPOINT) --acl public-read
	rm -f /tmp/pixi.toml

upload: upload-wheel upload-pixi ## Build and upload all SDK artifacts

upload-tools: ## Upload single-file tools to S3
	for f in $(TOOLS); do \
	  if [ -f "/tmp/$$f" ]; then \
	    aws s3 cp /tmp/$$f $(S3_BUCKET)/$$f --endpoint-url $(S3_ENDPOINT) --acl public-read; \
	  else \
	    echo "Warning: /tmp/$$f not found, skipping"; \
	  fi; \
	done

upload-tool-archives: ## Upload archive tools to S3
	for f in $(TOOL_ARCHIVES); do \
	  if [ -f "/tmp/$$f" ]; then \
	    aws s3 cp /tmp/$$f $(S3_BUCKET)/$$f --endpoint-url $(S3_ENDPOINT) --acl public-read; \
	  else \
	    echo "Warning: /tmp/$$f not found, skipping"; \
	  fi; \
	done

all: upload

clean: ## Clean build artifacts
	rm -rf dist/ *.egg-info
