# --- OS Detection ---
# Attempt to detect host OS family. Fallback to 'arch'.
# This logic checks /etc/os-release for ID or ID_LIKE fields.
# Use $(strip ...) to remove potential leading/trailing whitespace from shell output.
DETECTED_DISTRO := $(strip $(shell \
    if [ -f /etc/os-release ]; then \
        . /etc/os-release; \
        if echo "$$ID$$ID_LIKE" | grep -q -e arch; then \
            echo arch; \
        elif echo "$$ID$$ID_LIKE" | grep -q -e debian -e ubuntu; then \
            echo debian; \
        elif echo "$$ID$$ID_LIKE" | grep -q -e fedora; then \
            echo fedora; \
        else \
            echo arch; \
        fi; \
    else \
        echo arch; \
    fi \
))

# Set default TARGET_DISTRO based on detection, allowing user override
TARGET_DISTRO ?= $(DETECTED_DISTRO)

# --- Configuration ---
IMAGE_NAME := docker-aseprite-${TARGET_DISTRO}
DOCKERFILE := Dockerfile.${TARGET_DISTRO}
OUTPUT_DIR := ${PWD}/output
TARGET_DIR_IN_IMAGE := /target/aseprite/build/bin

# --- Main Targets ---
# Default target
all: build

# Prepare source files by running the script
# Use .PHONY to ensure it always runs if called directly
.PHONY: prepare-sources
prepare-sources:
	@echo "--- Preparing sources ---"
	@./prepare_sources.sh

# Build the Docker image for the target distribution
# Depends on sources being ready (though prepare-sources isn't a file dependency)
build-image:
# --- DEBUGGING ---
	@echo "DEBUG: TARGET_DISTRO='${TARGET_DISTRO}'"
	@echo "DEBUG: DOCKERFILE='${DOCKERFILE}'"
# --- END DEBUGGING ---
ifeq (,$(wildcard ${DOCKERFILE}))
	$(error Dockerfile for target distribution '${TARGET_DISTRO}' (${DOCKERFILE}) not found)
endif
	@echo "--- Building Docker image (${IMAGE_NAME}) using ${DOCKERFILE} ---"
	@docker build -t ${IMAGE_NAME} -f ${DOCKERFILE} .

# Extract the compiled binary from the image
# Depends on the image being built
.PHONY: extract-binary
extract-binary: build-image
	@echo "--- Extracting Aseprite binary from ${IMAGE_NAME} ---"
	@mkdir -p ${OUTPUT_DIR}/bin
	@CONTAINER_ID=$$(docker create ${IMAGE_NAME}) && \
	 docker cp "$${CONTAINER_ID}:${TARGET_DIR_IN_IMAGE}/." "${OUTPUT_DIR}/bin/" && \
	 docker rm "$${CONTAINER_ID}" > /dev/null
	@echo "Aseprite binary extracted to ${OUTPUT_DIR}/bin"

# Main build target: Prepare sources, build image, extract binary for selected distro
.PHONY: build
build: prepare-sources build-image extract-binary
	@echo "--- Build complete for ${TARGET_DISTRO} ---"
	@echo "Aseprite binary is in ${OUTPUT_DIR}/bin"

# --- Specific Distribution Targets ---
.PHONY: build-arch build-debian build-fedora

# Build for Arch Linux (default if not detected otherwise)
build-arch:
	$(MAKE) build TARGET_DISTRO=arch

# Build for Debian/Ubuntu based systems
build-debian:
	$(MAKE) build TARGET_DISTRO=debian

# Build for Fedora based systems
build-fedora:
	$(MAKE) build TARGET_DISTRO=fedora

# --- Utility Targets ---
# Clean up downloaded sources and output directory
.PHONY: clean
clean:
	@echo "--- Cleaning up ---"
	@rm -rf ./src
	@rm -rf ${OUTPUT_DIR}
	@echo "Cleanup complete."
