# Version control
VC = git

# Util
RM = rm -f

# Downloaders
REPO_SRC = https://git.mylloon.fr/Anri/confOS/raw/branch/main
WGET     = wget -q --show-progress
CURL     = curl -s

# Checkers
ARCH = $(shell grep -c "archlinux" /proc/version)
WSL  = $(shell grep -c "WSL2" /proc/version)

# Hide some verbose messages from Make
MAKEFLAGS += --no-print-directory

# Location
WIN_USER           = anri
PFx86              = /mnt/c/Program\ Files\ \(x86\)
WIN_HOME           = /mnt/c/Users/$(WIN_USER)
APPDATA            = $(WIN_HOME)/AppData/Roaming
WIN_STEAM_USERDATA = $(filter-out "userdata",$(shell find $(PFx86)/Steam/userdata -maxdepth 1 -type d -printf "\"%f\" "))
CSGO_CFG           = $(PFx86)/Steam/steamapps/common/Counter-Strike\ Global\ Offensive/csgo/cfg

all: full-sync


# Pull latest changes
refresh:
	@echo "Pull latest changes from repository..."
	@$(VC) pull
	@echo


# Pull and sync
full-sync:
	@$(MAKE) refresh
	@$(MAKE) sync


# .config
sync-dotconfig:
# Fish
	@mkdir -p $(HOME)/.config/fish/conf.d
	@mkdir -p $(HOME)/.config/fish/functions

	@$(WGET) $(REPO_SRC)/.config/fish/functions/fish_prompt.fish -O $(HOME)/.config/fish/functions/fish_prompt.fish
	@$(WGET) $(REPO_SRC)/.config/fish/conf.d/promp_color.fish -O $(HOME)/.config/fish/conf.d/promp_color.fish

	@$(WGET) $(REPO_SRC)/.config/fish/conf.d/abbr.fish -O $(HOME)/.config/fish/conf.d/abbr.fish
	@$(WGET) $(REPO_SRC)/.config/fish/conf.d/alias.fish -O $(HOME)/.config/fish/conf.d/alias.fish

# Micro
	@$(WGET) $(REPO_SRC)/.config/micro/settings.json -O $(HOME)/.config/micro/settings.json

# END
	@echo ".config directory synced!"
	@echo


# .emacs.d
sync-emacs:
	@mkdir -p $(HOME)/.emacs.d

	@$(WGET) $(REPO_SRC)/.emacs.d/init.el -O $(HOME)/.emacs.d/init.el

# END
	@echo "Emacs config synced!"
	@echo


# .dotfiles
sync-dotfiles:
# .gitconfig
	@$(WGET) $(REPO_SRC)/dotfiles/.gitconfig -O $(HOME)/.gitconfig

# .ocamlformat
	@$(WGET) $(REPO_SRC)/dotfiles/.ocamlformat -O $(HOME)/.ocamlformat

# .clang-format
	@$(WGET) $(REPO_SRC)/dotfiles/.clang-format -O $(HOME)/.clang-format

# End
	@echo "dotfiles synced!"
	@echo


# Arch distro specific
sync-arch:
# Fish
	@$(WGET) $(REPO_SRC)/.config/fish/config_arch.fish -O $(HOME)/.config/fish/config.fish
	@$(WGET) $(REPO_SRC)/.config/fish/conf.d/abbr.fish -O $(HOME)/.config/fish/conf.d/abbr.fish
	@$(CURL) $(REPO_SRC)/.config/fish/conf.d/abbr_arch.fish >> $(HOME)/.config/fish/conf.d/abbr.fish

# Emoji Picker
	@$(WGET) $(REPO_SRC)/.config/gazatu.xyz/emoji-picker.ini -O $(HOME)/.config/gazatu.xyz/emoji-picker.ini

# Firefox
	-@$(RM) $(HOME)/.mozilla/firefox/*.default-release/user.js
	@$(WGET) $(REPO_SRC)/.mozilla/firefox/user.js -P $(HOME)/.mozilla/firefox/*.default-release
	@$(CURL) $(REPO_SRC)/.mozilla/firefox/user-arch.js >> $(wildcard $(HOME)/.mozilla/firefox/*.default-release)/user.js

# VSCodium
	@$(WGET) $(REPO_SRC)/.config/VSCodium/product.json -O $(HOME)/.config/VSCodium/product.json

# Pacman hooks
	@sudo mkdir -p /etc/pacman.d/hooks
	@sudo $(WGET) $(REPO_SRC)/pacman.d/hooks/stop-wine-associations.hook -P /etc/pacman.d/hooks/stop-wine-associations.hook


# Windows Subsystem for Linux specific
sync-wsl:
# Fish
	@$(WGET) $(REPO_SRC)/.config/fish/config_wsl.fish -O $(HOME)/.config/fish/config.fish
	@$(CURL) $(REPO_SRC)/.config/fish/conf.d/abbr_wsl.fish >> $(HOME)/.config/fish/conf.d/abbr.fish

# Unison
	@mkdir -p $(HOME)/.unison
	@$(WGET) $(REPO_SRC)/.unison/default.prf -O $(HOME)/.unison/default.prf


# Windows through WSL
sync-windows:
# Scripts
	@$(WGET) $(REPO_SRC)/windows/ctmpf.bat -O $(WIN_HOME)/Documents/Local/ctmpf.bat
	@$(WGET) $(REPO_SRC)/windows/blockthespot.bat -O $(WIN_HOME)/Documents/Local/blockthespot.bat
	@$(WGET) $(REPO_SRC)/windows/update.bat -O $(WIN_HOME)/Documents/Local/update.bat
	@$(WGET) $(REPO_SRC)/windows/fix_nvidia_wsl.bat -O $(WIN_HOME)/Documents/Local/fix_nvidia_wsl.bat
	@echo "(Windows) Scripts synced!"
	@echo

# CSGO (only if installed)
	@if [ -d $(CSGO_CFG) ]; then \
		$(WGET) $(REPO_SRC)/games/csgo/autoexec.cfg -O $(CSGO_CFG)/autoexec.cfg; \
		$(WGET) $(REPO_SRC)/games/csgo/myconfig.cfg -O $(CSGO_CFG)/myconfig.cfg; \
		$(WGET) $(REPO_SRC)/games/csgo/customgame.cfg -O $(CSGO_CFG)/customgame.cfg; \
		$(foreach d,$(WIN_STEAM_USERDATA), $(WGET) $(REPO_SRC)/games/csgo/video.txt -O $(PFx86)/Steam/userdata/$(d)/730/local/cfg/video.txt;) \
		echo "(Windows) CS:GO synced!"; \
		echo; \
	fi
# Firefox
	@$(WGET) $(REPO_SRC)/.mozilla/firefox/user.js -O $(APPDATA)/Mozilla/Firefox/Profiles/*.default-release/user.js
	@echo "(Windows) Firefox synced!"
	@echo


sync:
# Check if at least the platform is supported
ifneq (1, $(filter 1, $(ARCH) $(WSL)))
	@echo "Unsupported platform"
else

# Call target of compatible with all supported platforms
	@$(MAKE) sync-dotconfig
	@$(MAKE) sync-dotfiles
	@$(MAKE) sync-emacs


# Specific to Arch
ifeq ($(ARCH), 1)
	@$(MAKE) sync-arch

	@echo "Arch synced!"
	@echo

# Specific to WSL
else ifeq ($(WSL), 1)
	@$(MAKE) sync-wsl

	@echo "WSL synced!"
	@echo

# Windows
	@$(MAKE) sync-windows

	@echo "Windows synced!"
	@echo
endif

# END
	@echo "🎉 Sync complete!"
endif