# Hurry Curry! - a game about cooking
# Copyright (C) 2026 Hurry Curry! Contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#

INSTALL_PREFIX = /usr/local
HURRYCURRY_DATA_PATH = $(INSTALL_PREFIX)/share/hurrycurry
HURRYCURRY_DISTRIBUTION = unknown

search-PATH = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
version-match = $(shell $(1) --version | grep -q "$(2)")

ifndef JSR
    ifneq (,$(call search-PATH,deno))
	JSR = DENO_NO_UPDATE_CHECK=1 deno run
    else ifneq (,$(call search-PATH,node))
	JSR = node
    else
	$(error "Couldn't detect JSR, please specify or install deno or node")
    endif
endif

ifndef GODOT
    ifneq (,$(call search-PATH,godot))
        GODOT = godot
    else ifneq (,$(call search-PATH,godot-editor))
        GODOT = godot-editor
    else
        $(error "Couldn't detect GODOT, please specify or install godot 4")
    endif
endif
ifneq (,$(call version-match,$(GODOT),"\b4\\."))
    $(error "GODOT is not godot 4, please install godot 4 instead")
endif

RUN_CLIENT_WITH_GRAPHICS = $(GODOT) --path client --
RUN_CLIENT = $(GODOT) --path client --display-driver headless --

ESBUILD_FLAGS = --bundle --format=esm --target=esnext --loader:.ini=text --minify --sourcemap

BIN_BOOK_EXPORT   = target/release/hurrycurry-book-export
BIN_DISCOVER      = target/release/hurrycurry-discover
BIN_EDITOR        = target/release/hurrycurry-editor
BIN_LOCALE_EXPORT = target/release/hurrycurry-locale-export
BIN_REPLAYTOOL    = target/release/hurrycurry-replaytool
BIN_SERVER        = target/release/hurrycurry-server
BIN_TOOLS         = target/release/hurrycurry-tools

ALL_SERVER = \
    $(BIN_DISCOVER) \
    $(BIN_EDITOR) \
    $(BIN_REPLAYTOOL) \
    $(BIN_SERVER) \
    $(BIN_TOOLS)
ALL_BOOK = \
    target/book/book.html \
	target/book/assets/fonts/josefin-sans.woff2 \
    $(patsubst locale/%.ini,target/book/book.%.html,$(wildcard locale/*.ini))
ALL_DATA = \
    data/recipes/none.yaml \
    data/recipes/anticurry.yaml \
    data/recipes/default.yaml
ALL_CLIENT = \
    client/.godot/import-finished \
    client/client.pck \
    client/icons/adaptive-background.png \
    client/icons/adaptive-foreground.png
ALL_TEST_CLIENT = \
    test-client/main.js
ALL = \
    $(ALL_SERVER) \
    $(ALL_BOOK) \
    $(ALL_DATA) \
    $(ALL_CLIENT)

all: $(ALL) PHONY
all_server: $(ALL_SERVER) PHONY
all_book: $(ALL_BOOK) PHONY
all_data: $(ALL_DATA) PHONY
all_client: $(ALL_CLIENT) PHONY
all_test_client: $(ALL_TEST_CLIENT) PHONY

target/release/%: $(shell find server -type f -name "*.rs" -or -name "Cargo.*" -or -name '*.css') $(shell ls Cargo.*)
	+env \
	    HURRYCURRY_DATA_PATH=$(HURRYCURRY_DATA_PATH) \
	    HURRYCURRY_DISTRIBUTION=$(HURRYCURRY_DISTRIBUTION) \
	    cargo build --release --bin $(patsubst target/release/%,%,$@)
	touch $@

target/book/tiles/list: $(BIN_TOOLS) $(ALL_DATA)
	@mkdir -p $(dir $@)
	$(BIN_TOOLS) all-tiles > $@
target/book/items/list: $(BIN_TOOLS) $(ALL_DATA)
	@mkdir -p $(dir $@)
	$(BIN_TOOLS) all-items > $@
target/book/%/done_glb_export: target/book/%/list client/.godot/import-finished
	$(RUN_CLIENT) --render-$(shell basename $(dir $@)) ../$< --render-output ../$(dir $@) --render-format gltf --render-include-tool-base
	touch $@
ifndef BOOK_USE_BLENDER_RENDER
target/book/%/done: target/book/%/list client/.godot/import-finished
	$(RUN_CLIENT_WITH_GRAPHICS) --render-$(shell basename $(dir $@)) ../$< --render-output ../$(dir $@) --render-resolution 512 --render-include-tool-base
	touch $@
else
target/book/%/done: target/book/%/list target/book/%/done_glb_export
	blender -b -P server/book-export/src/bpy_glb_render.py -- $(dir $@) $< 512
	touch $@
endif
target/book/book.%.html: target/book/tiles/done target/book/items/done $(BIN_BOOK_EXPORT) $(ALL_DATA) locale/en.ini locale/%.ini
	$(BIN_BOOK_EXPORT) -o $@ -l $(shell basename -s .html $@ | cut -d '.' -f 2) -f html -m 5star
target/book/book.html: target/book/book.en.html
	cp -v $< $@
target/book/assets/fonts/josefin-sans.woff2: client/gui/resources/fonts/font-josefin-sans.woff2
	@mkdir -p $(dir $@)
	cp -v $< $@

data/recipes/none.yaml:
	echo > $@
data/recipes/anticurry.yaml: data/recipes/default.yaml
	sed --file=data/recipes/anticurry.sed < $< > $@
data/recipes/default.yaml: data/recipes/default.js
	$(JSR) $< > $@
data/recipes/%.gv.txt: data/recipes/%.yaml
	$(BIN_TOOLS) graph > $@
data/recipes/%.svg: data/recipes/%.gv.txt
	dot -Tsvg -Kdot < $< > $@

client/.godot/import-finished: $(shell find client -name '*.gd' -or -name '*.res')
	$(GODOT) --headless --import client/project.godot
	touch $@
client/icons/adaptive-background.png: 
	ffmpeg -f lavfi -i "color=color=#E28142,scale=432x432" -frames:v 1 -y $@
client/icons/adaptive-foreground.png: client/icons/main.png
	ffmpeg -f image2 -i $< -vf "scale=280x280,pad=432:432:(ow-iw)/2:(oh-ih)/2:0x00000000" -frames:v 1 -y $@
client/client.pck: client/.godot/import-finished client/icons/adaptive-background.png client/icons/adaptive-foreground.png
	$(GODOT) --headless --export-pack wasm32-unknown-unknown client.pck client/project.godot

test-client/main.js: test-client/main.ts test-client/assets/locale.json.gz $(wildcard test-client/*.ts)
	esbuild $< --outfile=$@ $(ESBUILD_FLAGS)
test-client/assets/locale.json: $(BIN_LOCALE_EXPORT) $(wildcard locale/*.ini)
	@mkdir -p test-client/locale
	$(BIN_LOCALE_EXPORT) export-json-pack $@ $(wordlist 2,99,$(^))
test-client/assets/locale.json.gz: test-client/assets/locale.json
	gzip -9kf $<
watch_test_client: all_test_client PHONY
	esbuild test-client/main.ts \
		$(ESBUILD_FLAGS) \
		--outfile=test-client/main.js \
		--watch --servedir=test-client --serve=127.0.0.1:8080

install: install_common install_server install_client PHONY
install_common: $(ALL_DATA) PHONY
	install -Dt$(HURRYCURRY_DATA_PATH)/         -m644 data/*.yaml
	install -Dt$(HURRYCURRY_DATA_PATH)/maps/    -m644 data/maps/*.yaml
	install -Dt$(HURRYCURRY_DATA_PATH)/recipes/ -m644 data/recipes/*.yaml
install_server: $(ALL_SERVER) PHONY
	install -Dt$(INSTALL_PREFIX)/bin/ -m755 $(BIN_DISCOVER) $(BIN_EDITOR) $(BIN_REPLAYTOOL) $(BIN_SERVER) $(BIN_TOOLS)
install_client: $(ALL_CLIENT) PHONY
	echo '#!/bin/sh' >$(INSTALL_PREFIX)/bin/hurrycurry
	echo 'exec godot --main-pack $(INSTALL_PREFIX)/lib/hurrycurry/client.pck "$$@"' >>$(INSTALL_PREFIX)/bin/hurrycurry
	chmod 755 $(INSTALL_PREFIX)/bin/hurrycurry
	install -Dt$(INSTALL_PREFIX)/lib/hurrycurry/ -m644 client/client.pck

clean: PHONY
	rm -rf client/.godot/
	rm -f client/icons/adaptive-background.png*
	rm -f client/icons/adaptive-foreground.png*
	rm -f client/client.pck
	rm -f data/recipes/none.yaml
	rm -f data/recipes/anticurry.yaml
	rm -f data/recipes/default.yaml
	rm -rf target/
	rm -rf test-client/locale/
	rm -f test-client/*.js

.DELETE_ON_ERROR:
.PHONY: PHONY
