# Makefile

REBOOT_MARKER := .setup-reboot-required

# Default target
all: stow

# Full Setup
setup:
	@set -e; \
	rm -f $(REBOOT_MARKER); \
	bash ./scripts/base.sh; \
	if [ -f $(REBOOT_MARKER) ]; then \
		rm -f $(REBOOT_MARKER); \
		echo "Package layering finished. Reboot, then rerun make setup."; \
		exit 0; \
	fi; \
	bash ./scripts/node.sh; \
	bash ./scripts/go.sh; \
	bash ./scripts/rust.sh; \
	bash ./scripts/python.sh; \
	if [ -f $(REBOOT_MARKER) ]; then \
		rm -f $(REBOOT_MARKER); \
		echo "Package layering finished. Reboot, then rerun make setup."; \
		exit 0; \
	fi; \
	bash ./scripts/r.sh; \
	if [ -f $(REBOOT_MARKER) ]; then \
		rm -f $(REBOOT_MARKER); \
		echo "Package layering finished. Reboot, then rerun make setup."; \
		exit 0; \
	fi; \
	$(MAKE) clean; \
	$(MAKE) stow; \
	echo "Full setup completed."

base:
	bash ./scripts/base.sh
	@echo "Base setup completed."

# Just stow the dotfiles
stow:
	stow . --target=$$HOME --ignore=".git" --ignore=".gitignore" --ignore="README.md" --ignore=".zsh_secrets" --ignore=".zsh_secrets.example" --ignore="LICENSE" --ignore="Makefile" --ignore="bin" --ignore="scripts"
	@echo "Dotfiles linked."

# Clean old files and links
clean:
	stow -D . --target=$$HOME
	@echo "Links removed."

# Pull Git Updates
update:
	git pull origin main
	$(MAKE) setup

# Language Setups
node:
	bash ./scripts/node.sh

go:
	bash ./scripts/go.sh

rust:
	bash ./scripts/rust.sh

python:
	bash ./scripts/python.sh

r:
	bash ./scripts/r.sh

cpp:
	bash ./scripts/cpp.sh

storagebox:
	bash ./scripts/storagebox.sh

# Docker Tests
test-ubuntu:
	@echo "Ubuntu Test"
	docker run -it --rm -e TERM=xterm-256color -v $(PWD):/root/dotfiles ubuntu:latest \
	bash -c "export DEBIAN_FRONTEND=noninteractive && \
	apt-get update && \
	apt-get install -y sudo git make curl && \
	cd /root/dotfiles && \
	make setup"

test-arch:
	@echo "Spawning Arch Container..."
	docker run -it --rm -e TERM=xterm-256color -v $(PWD):/root/dotfiles archlinux:latest \
	bash -c "pacman -Sy --noconfirm base-devel git make sudo && cd /root/dotfiles && make setup"

test-fedora:
	@echo "Spawning Fedora Container..."
	docker run -it --rm -e TERM=xterm-256color -v $(PWD):/root/dotfiles fedora:latest \
	bash -c "dnf install -y git make sudo curl which passwd procps-ng && cd /root/dotfiles && make setup"
