# ENV

PROJECT_DIR ?= $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
BUILD_DIR ?= $(PROJECT_DIR)/build
FORGE_SRC ?= $(PROJECT_DIR)/forge-steel
TARGET ?= forge
RUN_ARGS ?= --project=$(PROJECT_DIR)/project --internal-size=576 -nmr -t=10 --auto-save
VERSION ?= invar-$(shell grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' $(FORGE_SRC)/configure.ac)
SOURCES ?= $(FORGE_SRC)/src/*.h $(FORGE_SRC)/src/*.c $(FORGE_SRC)/hashmap.c/hashmap.c $(FORGE_SRC)/log.c/src/log.c
CFLAGS ?= -Ofast -Ofast -march=native -flto -funroll-loops -fprefetch-loop-arrays -fno-exceptions -fopenmp -I$(FORGE_SRC)/include -DGLFW_INCLUDE_NONE -DGLFW_NATIVE_INCLUDE_NONE -DLOG_USE_COLOR -DVERSION=\"$(VERSION)\" -DOPENGL_MAJOR_VERSION=3 -DOPENGL_MINOR_VERSION=1
LDLIBS ?= -lm -lGL -lglfw -lasound

# FILES

$(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)
	$(CC) $(SOURCES) $(CFLAGS) $(LDLIBS) -o $(BUILD_DIR)/$(TARGET)

# ACTIONS

.PHONY: remove-passwd
remove-passwd:
	sudo passwd -d $(shell whoami)

.PHONY: apt-install
apt-install:
	sudo apt update
	sudo apt install -y weston seatd kitty git libglfw3-dev libgl-dev libasound2-dev libv4l-dev python3-pip python3-smbus i2c-tools

.PHONY: pip-install
pip-install:
	pip3 install luma.oled --break-system-packages

.PHONY: setup-weston
setup-weston:
	ln -fs $(PROJECT_DIR)/system/weston.ini $(HOME)/.config/weston.ini

.PHONY: remove-motd
remove-motd:
	sudo rm -rf /etc/motd

.PHONY: disable-cloud-init
disable-cloud-init:
	sudo systemctl disable cloud-init.service
	sudo systemctl mask cloud-init.service
	sudo touch /etc/cloud/cloud-init.disabled

.PHONY: setup-services
setup-services:
	sudo ln -fs $(PROJECT_DIR)/system/getty@tty1.service /etc/systemd/system/getty@tty1.service
	sudo systemctl daemon-reload
	sudo systemctl enable getty@tty1.service seatd.service

.PHONY: setup-bashrc
setup-bashrc:
	$(PROJECT_DIR)/line_in_file.sh "make -C $(PROJECT_DIR) --no-print-directory start" "$(HOME)/.bashrc"

.PHONY: install
install: remove-passwd apt-install pip-install setup-weston remove-motd disable-cloud-init setup-services setup-bashrc

.PHONY: clean
clean:
	$(RM) -r $(BUILD_DIR)

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

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

.PHONY: start-bg
start-bg:
	@python3 $(PROJECT_DIR)/i2c.py >> $(PROJECT_DIR)/python.log&

.PHONY: start-fg
start-fg:
	@echo "on" | sudo tee /sys/class/drm/card1-Composite-1/status || true
	@/usr/bin/weston --shell=kiosk-shell.so --xwayland --log=$(PROJECT_DIR)/weston.log -- kitty --hold make --no-print-directory -C $(PROJECT_DIR) run

.PHONY: start
start: start-bg start-fg

# HOST ONLY

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

.PHONY: host-chmod-opt
host-chmod-opt:
	sudo chmod a+rwx /run/media/$(shell whoami)/rootfs/opt/

.PHONY: host-rsync
host-rsync: host-chmod-opt
	rsync -a $(PWD) /run/media/$(shell whoami)/rootfs/opt/

.PHONY: host-setup-custom
host-setup-custom:
	cp $(PROJECT_DIR)/system/config.ini /run/media/$(shell whoami)/bootfs/custom.txt

.PHONY: host-reset-config
host-reset-config:
	sed -i '/include custom.txt/d' /run/media/$(shell whoami)/bootfs/config.txt

.PHONY: host-setup-config
host-setup-config: host-setup-custom host-reset-config
	echo "include custom.txt" >> /run/media/$(shell whoami)/bootfs/config.txt

.PHONY: host-reset-cmdline
host-reset-cmdline:
	sed -i '2,$$d' /run/media/$(shell whoami)/bootfs/cmdline.txt

.PHONY: host-setup-cmdline
host-setup-cmdline: host-reset-cmdline
	cat $(PROJECT_DIR)/system/cmdline.txt >> /run/media/$(shell whoami)/bootfs/cmdline.txt

.PHONY: host-unmount
host-unmount:
	sudo umount -R /run/media/$(shell whoami)/rootfs
	sudo umount -R /run/media/$(shell whoami)/bootfs

.PHONY: host-install
host-install: host-submodule-update host-rsync host-setup-config host-setup-cmdline host-unmount

.PHONY: host-install-reset
host-install-reset: host-submodule-update host-rsync host-reset-config host-reset-cmdline host-unmount
