# Variables for build tools
CMAKE=/usr/bin/cmake
MESON=/usr/bin/meson
# Paths for installed apps
INCLUDE_SYS_PATH=/usr/include
# Components and libraries
COMPONENTS=hyprcursor hyprlang hyprlock hyprpaper hyprutils wl-clipboard \
		   hyprwayland-scanner tomlplusplus libxcb-errors

# Set the default target to build
.DEFAULT_GOAL := all

### Makefile targets for hyprwm libraries ###

# Build and install tomlplusplus if it's not on the system
tomlplusplus:
	git clone https://github.com/marzer/tomlplusplus.git
__Tomlplusplus = $(INCLUDE_SYS_PATH)/toml++
$(__Tomlplusplus): tomlplusplus
	@echo "Building and installing tomlplusplus..."
	cd tomlplusplus && $(MESON) setup build --buildtype=release --prefix=/usr
	ninja -C tomlplusplus/build
	sudo $(MESON) install -C tomlplusplus/build

# Build hyprcursor if the .hpp file is not installed (required hyprlang)
# Library for hyprwm
__Hyprcursor = $(INCLUDE_SYS_PATH)/hyprcursor/
$(__Hyprcursor): $(__Tomlplusplus) $(__Hyprlang) 
	$(CMAKE) --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release \
		-DCMAKE_INSTALL_PREFIX:PATH=/usr -S ./hyprcursor -B ./hyprcursor/build
	$(CMAKE) --build ./hyprcursor/build --config Release --target all \
		-j`nproc 2>/dev/null || getconf _NPROCESSORS_CONF`
	@echo "Installing library hyprcursor..."
	sudo $(CMAKE) --install hyprcursor/build

# Build hyprutils if the .hpp file is not installed
# Library for hyprwm
__Hyprutils = $(INCLUDE_SYS_PATH)/hyprutils
$(__Hyprutils):
	$(CMAKE) --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release \
		-DCMAKE_INSTALL_PREFIX:PATH=/usr -S ./hyprutils -B ./hyprutils/build
	$(CMAKE) --build ./hyprutils/build --config Release --target all \
		-j`nproc 2>/dev/null || getconf _NPROCESSORS_CONF`
	sudo $(CMAKE) --install hyprutils/build

# Build hyprlang if the .hpp file is not installed
# Library for hyprwm
__Hyprlang = $(INCLUDE_SYS_PATH)/hyprlang.hpp
$(__Hyprlang): $(__Hyprutils)
	$(CMAKE) --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release \
		-DCMAKE_INSTALL_PREFIX:PATH=/usr -S ./hyprlang -B ./hyprlang/build
	$(CMAKE) --build ./hyprlang/build --config Release --target hyprlang \
		-j`nproc 2>/dev/null || getconf _NPROCESSORS_CONF`
	# At this point install hyprlang since it is a library other apps need
	@echo "Installing library hyprlang..."
	sudo $(CMAKE) --install ./hyprlang/build

__Hyprwayland-scanner = /usr/lib64/cmake/hyprwayland-scanner
$(__Hyprwayland-scanner):
	$(CMAKE) -DCMAKE_INSTALL_PREFIX=/usr -S ./hyprwayland-scanner \
		-B hyprwayland-scanner/build
	$(CMAKE) --build ./hyprwayland-scanner/build -j \
		`nproc 2>/dev/null || getconf _NPROCESSORS_CONF`
	@echo "Installing library hyprwayland-scanner..."
	sudo $(CMAKE) --install ./hyprwayland-scanner/build

libxcb-errors:
	git clone https://gitlab.freedesktop.org/xorg/lib/libxcb-errors.git
__Libxcb-errors = /usr/lib/pkgconfig/xcb-errors.pc
$(__Libxcb-errors): libxcb-errors
	@echo "Building and install library libxcb-errors..."
	cd libxcb-errors && { \
		git submodule update --init ; \
		/bin/sh autogen.sh ; \
		/bin/sh configure --prefix=/usr ; \
		make && sudo make install ; } 

### Makefile targets for hyprwm apps ###

# Build Hyprland if it's not installed on the system
__Hyprland = Hyprland/build/Hyprland
$(__Hyprland): $(__Hyprlang) $(__Hyprcursor) $(__Hyprwayland-scanner) \
	$(__Libxcb-errors)
	@echo "Building Hyprland..."
	sudo $(MAKE) -C Hyprland all

# Build hyprlock if it's not installed on the system
__Hyprlock = hyprlock/build/hyprlock
$(__Hyprlock): $(__Hyprlang)
	@echo "Building hyprlock..."
	$(CMAKE) --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release \
		-S ./hyprlock -B ./hyprlock/build
	$(CMAKE) --build ./hyprlock/build --config Release --target hyprlock \
		-j`nproc 2>/dev/null || getconf _NPROCESSORS_CONF`

# Build hyprpaper if it's not installed on the system
__Hyprpaper = hyprpaper/build/hyprpaper
$(__Hyprpaper): $(__Hyprlang)
	@echo "Building hyprpaper..."
	$(CMAKE) --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release \
		-DCMAKE_INSTALL_PREFIX:PATH=/usr -S ./hyprpaper -B ./hyprpaper/build
	$(CMAKE) --build ./hyprpaper/build --config Release --target hyprpaper \
		-j`nproc 2>/dev/null || getconf _NPROCESSORS_CONF`

# Build wl-clipboard if it's not installed on the system
__Wl_clipboard = wl-clipboard/build/src
$(__Wl_clipboard)/wl-copy $(__Wl_clipboard)/wl-paste &: 
	cd wl-clipboard && $(MESON) setup --prefix=/usr build
	ninja -C wl-clipboard/build

### PHONY targets that a user would run ###
all:
	@if git submodule status | grep --quiet '^-' ; then\
		echo "Git submodules have not been initialised. Please run: git \
submodule update --init --recursive" ; exit 1 ;\
	fi
	@cd Hyprland/subprojects && git submodule update --init
	$(MAKE) build

build: $(__Hyprlock) $(__Hyprpaper) $(__Wl_clipboard)/wl-copy $(__Hyprland)

install: build
	@echo "Installing hyprlock..."
	sudo $(CMAKE) --install hyprlock/build
	@echo "Installing hyprpaper"
	sudo $(CMAKE) --install hyprpaper/build
	@echo "Installing wl-clipboard"
	sudo $(MESON) install -C wl-clipboard/build
	@echo "Installing Hyprland"
	sudo $(MAKE) -C Hyprland install

update:
	@echo "Updating module components..."
	git submodule foreach \
		'git pull origin $$(git rev-parse --abbrev-ref HEAD)'
	# In case these libraries haven't been pulled yet, ignore the exit code
	-cd tomlplusplus && git pull origin $$(git rev-parse --abbrev-ref HEAD)
	-cd libxcb-error && git pull origin $$(git rev-parse --abbrev-ref HEAD)
	@echo "Please note that if tomlplusplus or libxcb-errors throw an error \
		for not being found, this is defined behaviour since these libraries \
		are cloned into the project later. You can safely ignore this."

test: build
	@echo "Be real, you don't want to do this, so we'll abort."
	$(error No test-suite implemented)

clean:
	@echo "Cleaning up from build process..."
	@echo "Cleaning Hyprland with Makefile."
	$(MAKE) -C Hyprland clear
	rm -rf $(addsuffix /build, $(COMPONENTS)) >/dev/null 2>&1

.PHONY : all build install test clean update
