# --- Configuration ---
# Uses a single Dockerfile with flatpak-builder inside
IMAGE_NAME := aseprite-flatpak-builder
DOCKERFILE := Dockerfile
TARGET_DIR := ${PWD}/target
OUTPUT_FILE := ${TARGET_DIR}/aseprite.flatpak
BUILD_DIR := ${PWD}/build
SRC_DIR := ${PWD}/src

# Host-side cache directories for Flatpak builder and ccache
# These will be mounted into the container
FLATPAK_BUILDER_CACHE_DIR := ${PWD}/.flatpak-builder-cache
FLATPAK_STATE_DIR := ${FLATPAK_BUILDER_CACHE_DIR}/state
FLATPAK_CACHE_DIR := ${FLATPAK_BUILDER_CACHE_DIR}/cache
CCACHE_DIR := ${PWD}/.ccache

# Get current user and group IDs to avoid permission issues with Docker
USER_ID := $(shell id -u)
GROUP_ID := $(shell id -g)

# --- 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 using prepare_sources.sh ---"
	./prepare_sources.sh
	./prepare_sources.sh --check-integrity

# Build the Docker image which runs flatpak-builder inside
# Implicitly depends on sources being ready after prepare-sources runs
.PHONY: build-image
build-image:
	@echo "--- Building Aseprite Flatpak builder image (${IMAGE_NAME}) ---"
	@docker build \
		--build-arg UID=${USER_ID} \
		--build-arg GID=${GROUP_ID} \
		-t ${IMAGE_NAME} -f ${DOCKERFILE} .

# Run flatpak-builder inside a privileged container with proper user permissions
.PHONY: run-flatpak-builder
run-flatpak-builder:
	@echo "--- Running flatpak-builder inside container ---"
	@mkdir -p ${BUILD_DIR} ${FLATPAK_STATE_DIR} ${FLATPAK_CACHE_DIR} ${CCACHE_DIR}
	@docker run \
		--rm \
		--privileged \
		--device /dev/fuse \
		-u ${USER_ID}:${GROUP_ID} \
		-v ${SRC_DIR}:/sources:ro \
		-v ${BUILD_DIR}:/build \
		-v ${FLATPAK_STATE_DIR}:/home/builder/.local/share/flatpak-builder \
		-v ${FLATPAK_CACHE_DIR}:/home/builder/.cache/flatpak-builder \
		-v ${CCACHE_DIR}:/home/builder/.cache/ccache \
		-v ${PWD}/com.aseprite.Aseprite.yaml:/build/com.aseprite.Aseprite.yaml:ro \
		-w /build \
		${IMAGE_NAME} \
		sh -c "flatpak-builder --disable-rofiles-fuse --state-dir=/home/builder/.local/share/flatpak-builder --ccache build-dir com.aseprite.Aseprite.yaml --repo=repo && if [ -d 'build-dir/files/third_party/externals/emsdk' ]; then rm -rf build-dir/files/third_party/externals/emsdk; fi"

# Create the Flatpak bundle from the repo built in the previous step
.PHONY: create-bundle
create-bundle: run-flatpak-builder
	@echo "--- Creating Flatpak bundle ---"
	@docker run \
		--rm \
		--privileged \
		--device /dev/fuse \
		-u ${USER_ID}:${GROUP_ID} \
		-v ${BUILD_DIR}:/build \
		-w /build \
		${IMAGE_NAME} \
		sh -c "flatpak build-bundle repo aseprite.flatpak com.aseprite.Aseprite"

# Copy the final bundle from the intermediate directory to the output directory
.PHONY: extract-flatpak
extract-flatpak: create-bundle
	@echo "--- Copying Flatpak bundle to target directory ---"
	@mkdir -p ${TARGET_DIR}
	@cp ${BUILD_DIR}/aseprite.flatpak ${OUTPUT_FILE}
	@echo "Aseprite Flatpak bundle is in ${OUTPUT_FILE}"

# Prepare build target: Prepare sources, build Docker image
.PHONY: prepare
prepare: prepare-sources build-image
	@echo "--- Prepare complete ---"

# Main build target: Run builder, create bundle, extract
.PHONY: build
build: build-image run-flatpak-builder create-bundle extract-flatpak
	@echo "--- Build complete ---"

# Clean up only the source files and downloaded content
.PHONY: clean
clean:
	@echo "--- Cleaning source files ---"
	@rm -rf ${SRC_DIR}
	@echo "Source files cleaned. Run 'prepare-sources' to download them again."
