# Assembles dist/ - a self-contained, runnable copy of System Shock -
# from the compiled engine (built via Docker, see build-image.sh/
# run-image.sh) and the game assets extracted from a purchased copy (see
# res/assets/extract_assets.sh). Building the engine needs the build-image
# (./build-image.sh, once); everything else here is plain file copying.
#
# `make package` instead builds a redistributable tarball that omits the
# proprietary game assets entirely (see res/assets/GET_ASSETS.txt, which
# it ships in their place) - this is what CI publishes.

DIST_DIR    := dist
ENGINE_OUT  := engine/.build-output
ASSETS_DIR  := res/assets/ss_ee
BUILD_DIR   := build
ARCH        := $(shell uname -m)

.PHONY: all dist build-image engine assets package apk clean

all: dist

# Builds the Docker build-image itself. Only needed once, or after
# build-image/Dockerfile changes.
build-image:
	./build-image.sh

# Compiles engine/ (the vendored Shockolate snapshot) - offline, since
# every dependency it needs was already baked into the build-image.
# Always re-run so `make dist`/`make package` reflect the current engine/
# source, same as a fresh checkout would.
#
# If we're already running inside the build-image (QUESTSHOCK_BUILD_IMAGE,
# set by its Dockerfile - true for a CI job using it as its container,
# which has no nested docker available), compile directly; otherwise
# shell out to ./run-image.sh, which docker-runs the image against this
# checkout.
engine:
	@if [ -n "$$QUESTSHOCK_BUILD_IMAGE" ]; then \
		bash build-image/build-engine.sh; \
	else \
		./run-image.sh; \
	fi

# Fails with a pointer to extract_assets.sh if the purchased game assets
# haven't been extracted yet.
assets:
	@if [ ! -d "$(ASSETS_DIR)/data" ] || [ ! -d "$(ASSETS_DIR)/sound" ]; then \
		echo "PREFLIGHT FAIL: $(ASSETS_DIR) not found - run res/assets/extract_assets.sh first" >&2; \
		echo "(see res/assets/extract_assets.sh for how to get the installer from gog.com)" >&2; \
		exit 1; \
	fi

dist: engine assets
	@echo "== Assembling $(DIST_DIR) =="
	rm -rf "$(DIST_DIR)/systemshock" "$(DIST_DIR)/lib" "$(DIST_DIR)/res" \
		"$(DIST_DIR)/shaders" "$(DIST_DIR)/run.sh"
	mkdir -p "$(DIST_DIR)/lib" "$(DIST_DIR)/res/data" "$(DIST_DIR)/res/sound"
	cp "$(ENGINE_OUT)/systemshock" "$(DIST_DIR)/"
	cp -a "$(ENGINE_OUT)/lib/." "$(DIST_DIR)/lib/"
	cp "$(ENGINE_OUT)/soundfont.sf2" "$(DIST_DIR)/res/"
	cp -a engine/shaders "$(DIST_DIR)/shaders"
	cp -a "$(ASSETS_DIR)/data/." "$(DIST_DIR)/res/data/"
	cp -a "$(ASSETS_DIR)/sound/." "$(DIST_DIR)/res/sound/"
	cp res/run.sh "$(DIST_DIR)/run.sh"
	chmod +x "$(DIST_DIR)/run.sh"
	@echo "== Done - run $(DIST_DIR)/run.sh to play =="

# Builds a versioned, redistributable Linux release tarball at
# dist/shockolate-<version>-linux-<arch>.tar.gz - everything needed to
# run except the proprietary game assets (res/GET_ASSETS.txt explains how
# to get those instead of shipping res/data/, res/sound/). Version
# defaults to the current git tag (vX.Y.Z, tag prefix stripped) - push a
# tag to drive a release. Override with `make package VERSION=1.2.3`, or
# just run it untagged for a local dev build (gets a 0.0.0-dev+<sha>
# placeholder version, with a warning).
package: engine
	@git config --global --add safe.directory "$$(pwd)" 2>/dev/null || true
	@V="$(VERSION)"; \
	if [ -z "$$V" ]; then \
		if TAG=$$(git describe --tags --exact-match --match 'v[0-9]*.[0-9]*.[0-9]*' 2>/dev/null); then \
			V=$${TAG#v}; \
		else \
			V="0.0.0-dev+$$(git rev-parse --short HEAD)"; \
			echo "WARNING: HEAD is not on a vX.Y.Z tag - building placeholder version $$V (push a tag to drive a real release version)" >&2; \
		fi; \
	fi; \
	case "$$V" in \
		[0-9]*.[0-9]*.[0-9]*) ;; \
		*) echo "PREFLIGHT FAIL: VERSION '$$V' is not a semantic version (expected X.Y.Z, optionally with a -pre+meta suffix)" >&2; exit 1;; \
	esac; \
	PKG_NAME="shockolate-$$V-linux-$(ARCH)"; \
	PKG_STAGE="$(BUILD_DIR)/package/$$PKG_NAME"; \
	echo "Packaging $$PKG_NAME"; \
	rm -rf "$$PKG_STAGE"; \
	mkdir -p "$$PKG_STAGE/lib" "$$PKG_STAGE/res"; \
	cp "$(ENGINE_OUT)/systemshock" "$$PKG_STAGE/"; \
	cp -a "$(ENGINE_OUT)/lib/." "$$PKG_STAGE/lib/"; \
	cp -a engine/shaders "$$PKG_STAGE/shaders"; \
	cp "$(ENGINE_OUT)/soundfont.sf2" "$$PKG_STAGE/res/"; \
	cp res/assets/GET_ASSETS.txt "$$PKG_STAGE/res/GET_ASSETS.txt"; \
	cp res/run.sh "$$PKG_STAGE/run.sh"; \
	chmod +x "$$PKG_STAGE/run.sh"; \
	cp LICENSE "$$PKG_STAGE/LICENSE"; \
	cp engine/LICENSE "$$PKG_STAGE/LICENSE.Shockolate"; \
	cp NOTICE.txt "$$PKG_STAGE/NOTICE.txt"; \
	mkdir -p "$(DIST_DIR)"; \
	tar -czf "$(DIST_DIR)/$$PKG_NAME.tar.gz" -C "$(BUILD_DIR)/package" "$$PKG_NAME"; \
	rm -rf "$(BUILD_DIR)/package"; \
	echo "Wrote $(DIST_DIR)/$$PKG_NAME.tar.gz"

# Builds the Quest APK (see build-image/build-apk.sh and
# android/engine-patches/ - engine/ itself is never modified; a patch is
# applied to a scratch copy at build time instead). Same
# QUESTSHOCK_BUILD_IMAGE detection as `engine`. Unlike every other target
# here, this needs network access at build time (Gradle/AGP's own
# dependency resolution) - see build-image/Dockerfile.
apk:
	@if [ -n "$$QUESTSHOCK_BUILD_IMAGE" ]; then \
		bash build-image/build-apk.sh; \
	else \
		./run-image.sh bash build-image/build-apk.sh; \
	fi

clean:
	rm -rf "$(DIST_DIR)" "$(BUILD_DIR)" "$(ENGINE_OUT)" \
		engine/build_ext engine/CMakeCache.txt engine/CMakeFiles \
		engine/cmake_install.cmake engine/Makefile engine/systemshock \
		engine/src/Libraries/CMakeFiles \
		android/engine.properties android/app/src/main/assets android/app/src/main/jniLibs \
		android/app/build android/.gradle android/app/.cxx
