
# Makefile for hdwalletpy workflow
# - Build a reusable Docker image
# - Compile wheels in container (wheelhouse)
# - Create host venv and install from wheelhouse
# - Optionally run inside a warm container

# ---------- Config ----------
IMAGE := python-build-env:3.11
CONTAINER := hdwallet
WORKDIR := /app
VENV_HOST := .venv_host
VENV_CONTAINER := /opt/venv
WHEELHOUSE := wheelhouse

# ---------- Help ----------
.PHONY: help
help:
    @echo "Targets:"
    @echo "  make build-image      - Build Docker image with build tools and python3-venv"
    @echo "  make wheels           - Build dependency wheels into ./$(WHEELHOUSE)"
    @echo "  make venv-host        - Create host venv $(VENV_HOST) and install from wheelhouse"
    @echo "  make up               - Start a warm container named $(CONTAINER)"
    @echo "  make shell            - Open a shell in the warm container"
    @echo "  make venv-container   - Create venv inside container at $(VENV_CONTAINER) and install requirements"
    @echo "  make down             - Stop and remove the warm container"
    @echo "  make clean-venv       - Remove host venv $(VENV_HOST)"
    @echo "  make clean-wheelhouse - Remove local wheel cache $(WHEELHOUSE)"
    @echo "  make clean            - Clean venv and wheelhouse"

# ---------- Build reusable image ----------
.PHONY: build-image
build-image:
    docker build -t $(IMAGE) .

# ---------- Build wheels in container ----------
.PHONY: wheels
wheels: requirements.txt
    docker run --rm -it -v "$$PWD":$(WORKDIR) -w $(WORKDIR) $(IMAGE) bash -lc '\
        python -m pip install --upgrade pip setuptools wheel && \
        mkdir -p $(WHEELHOUSE) && \
        python -m pip wheel -r requirements.txt -w $(WHEELHOUSE) \
    '

# ---------- Host venv from wheelhouse ----------
.PHONY: venv-host
venv-host: requirements.txt
    python3 -m venv $(VENV_HOST)
    . $(VENV_HOST)/bin/activate && \
    python -m pip install --upgrade pip setuptools wheel && \
    python -m pip install --no-index --find-links=$(WHEELHOUSE) -r requirements.txt && \
    python --version

# ---------- Warm container lifecycle ----------
.PHONY: up
up:
    docker run -dit -v "$$PWD":$(WORKDIR) -w $(WORKDIR) --name $(CONTAINER) $(IMAGE) bash

.PHONY: shell
shell:
    docker exec -it $(CONTAINER) bash

.PHONY: venv-container
venv-container: requirements.txt
    docker exec -it $(CONTAINER) bash -lc '\
        test -x $(VENV_CONTAINER)/bin/python || ( \
          python -m venv $(VENV_CONTAINER) && \
          . $(VENV_CONTAINER)/bin/activate && \
          python -m pip install --upgrade pip setuptools wheel && \
          python -m pip install -r requirements.txt \
        ) && \
        . $(VENV_CONTAINER)/bin/activate && python --version \
    '

.PHONY: down
down:
    - docker rm -f $(CONTAINER)

# ---------- Cleanup ----------
.PHONY: clean-venv
clean-venv:
    rm -rf $(VENV_HOST)

.PHONY: clean-wheelhouse
clean-wheelhouse:
    rm -rf $(WHEELHOUSE)

.PHONY: clean
clean: clean-venv clean-wheelhouse

