# Variables for build tools
CMAKE=/bin/cmake
MESON=/bin/meson
# Paths for installed apps
BIN_SYSTEM_PATH=/usr/local/bin
INC_SYSTEM_PATH=/usr/include

# Set the default target to build
default-target: build

# Build Hyprland if it's not installed on the system
__Hyperland = $(BIN_SYSTEM_PATH)/hyprland
$(__Hyperland): $(__Hyperlang) $(__Hypercursor)
	if [ ! -d Hyprland ]; then
		git submodule init
		git submodule update --remote Hyprland
	fi
	$(MAKE) -C Hyprland all 

# Build hyprcursor if the .hpp file is not installed (required hyprlang)
__Hypercursor = $(INC_SYSTEM_PATH)/hyprcursor.hpp
$(__Hypercursor): $(__Hyperlang) 
	if [ ! -d hyprcursor ]; then
		git submodule init
		git submodule update --remote hyprcursor
	fi
	$(CMAKE) --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release \
		-DCMAKE_INSTALL_PREFIX:PATH=/usr -S . -B ./hyprcursor/build
	$(CMAKE) --build ./build --config Release --target all \
		-j`nproc 2>/dev/null || getconf _NPROCESSORS_CONF`
	@echo "Installing library hyprcursor..."
	sudo $(CMAKE) --install hyprcursor/build

# Build hyprlang if the .hpp file is not installed
__Hyprlang = $(INC_SYSTEM_PATH)/hyprlang.hpp 
$(__Hyprlang): 
	if [ ! -d hyprlang ]; then
		git submodule init
		git submodule update --remote hyprlang
	fi
	# built using cmake
	$(CMAKE) --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release \
		-DCMAKE_INSTALL_PREFIX:PATH=/usr -S . -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

# Build hyprlock if it's not installed on the system
__Hyprlock = $(BIN_SYSTEM_PATH)/hyprlock
$(__Hyprlock): $(__Hyprlang)
	if [ ! -d hyprlock ]; then
		git submodule init
		git submodule update --remote hyprlock
	fi
	$(CMAKE) --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release \
		-S . -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 = $(BIN_SYSTEM_PATH)/hyprpaper
hyprpaper: $(__Hyprlang)
	if [ ! -d hyprpaper ]; then
		git submodule init
		git submodule update --remote hyprpaper
	fi
	$(CMAKE) --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release \
		-DCMAKE_INSTALL_PREFIX:PATH=/usr -S . -B ./hyprpaper/build
	$(CMAKE) --build ./hyprpaper/build --config Release --target hyprpaper \
		-j`nproc 2>/dev/null || getconf _NPROCESSORS_CONF`

__Wl_clipboard = $(BIN_SYSTEM_PATH)/wl-copy $(BIN_SYSTEM_PATH)/wl-paste
$(__Wl_clipboard): 
	if [ ! -d wl-clipboard ]; then
		git submodule init
		git submodule update --remote wl-clipboard
	fi
	$(MESON) setup -C wl-clipboard build
	ninja -C wl-clipboard/build

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

.PHONY : deps
deps: $(__Tomlplusplus)
	@echo "Installing system dependencies..."
	sudo eopkg it -y libcairo-devel file-devel libdrm-devel libglvnd-devel \
		libjpeg-turbo-devel librsvg-devel libwebp-devel libzip-devel \
		mesalib-devel pam-devel pango-devel waybar wayland-devel \
		wayland-protocols-devel wlroots-devel libxkbcommon-devel

### PHONY targets that a user would run ###
.PHONY : build
build: deps $(__Hyprcursor) $(__Hyprlang) $(__Hyprlock) $(__Hyprpaper) \
	$(__Wl-clipboard) $(__Hyprland)

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


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