# ENV

BUILD_DIR ?= build
TARGET ?= forge
RUN_ARGS ?= -tf --project=project -is=480 --demo
VERSION ?= invar-$(shell grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' forge-steel/configure.ac)

# FILES

forge-steel/src/main.c:
	$(MAKE) -s submodule-init

forge-steel/hashmap.c/hashmap.c:
	$(MAKE) -s submodule-init

forge-steel/log.c/src/log.c:
	$(MAKE) -s submodule-init

forge-steel/include/linmath.h:
	$(MAKE) -s submodule-init

$(BUILD_DIR)/$(TARGET): forge-steel/src/main.c forge-steel/hashmap.c/hashmap.c forge-steel/log.c/src/log.c forge-steel/include/linmath.h
	mkdir -p $(BUILD_DIR)
	gcc \
		forge-steel/src/*.h forge-steel/src/*.c \
		-Iforge-steel/include \
		forge-steel/hashmap.c/hashmap.c \
		forge-steel/log.c/src/log.c \
		-lm -lGL -lglfw -lasound \
		-Wall -Wextra \
		-Wno-format-truncation \
		-DGLFW_INCLUDE_NONE \
		-DGLFW_NATIVE_INCLUDE_NONE \
		-DLOG_USE_COLOR \
		-DVERSION=\"$(VERSION)\" \
		-DOPENGL_MAJOR_VERSION=3 \
		-DOPENGL_MINOR_VERSION=1 \
		-o build/$(TARGET) \
		-O3

# ACTIONS

.PHONY: install
install:
	sudo apt update
	sudo apt install -y weston libglfw3-dev libgl-dev libasound2-dev libv4l-dev
	sudo usermod -a -G render $(shell whoami)

.PHONY: clean
clean:
	rm -rf $(BUILD_DIR)

.PHONY: build
build: $(BUILD_DIR)/$(TARGET)

.PHONY: run
run: $(BUILD_DIR)/$(TARGET)
	$(BUILD_DIR)/$(TARGET) $(RUN_ARGS)

.PHONY: submodule-init
submodule-init:
	git submodule update --init --recursive

.PHONY: start
start:
	weston --shell=kiosk-shell.so --xwayland -- $(BUILD_DIR)/$(TARGET) $(RUN_ARGS)
