.PHONY: python_clean_online_fxreader_vpn

host_deps:
	./m.py host_deps

python_lint:
	./m.py mypy -- -f vscode 2>&1 | less

python_tests:
	./m.py tests

#python_clean_online_fxreader_vpn:
#	rm -fr \
#		deps/com.github.aiortc.aiortc/src/online_fxreader/vpn/dist;

PYTHON_PROJECTS ?= \
	deps/com.github.aiortc.aiortc/ \
	deps/com.github.aiortc.aiortc/src/online_fxreader/vpn/ \
	python

INSTALL_ROOT ?= ~/.local/bin

#python_clean: python_clean_online_fxreader_vpn
python_clean_env:
	rm -fr \
		$(INSTALL_ROOT)/env3;

python_put_env:
	[[ -d $(INSTALL_ROOT)/env3 ]] || (\
		uv venv --system-site-packages --seed $(INSTALL_ROOT)/env3 && \
		$(INSTALL_ROOT)/env3/bin/python3 -m pip install --force-reinstall uv \
	);

python_clean_dist:
	for o in $(PYTHON_PROJECTS); do \
		[[ -d $$o/dist ]] || continue; \
		echo $$o/dist; \
		rm -fr $$o/dist; \
	done

python_clean: python_clean_dist python_clean_env

UV_ARGS ?= --offline
python_put_dist:
	for f in \
		$(PYTHON_PROJECTS); do \
		[[ -d $$f/dist ]] && continue; \
		echo $$f; \
		python3 -m build -n $$f; \
		$(INSTALL_ROOT)/env3/bin/python3 -m uv pip install $(UV_ARGS) $$f/dist/*.whl; \
	done
	ln -sf $(INSTALL_ROOT)/env3/bin/online-fxreader-pr34-commands $(INSTALL_ROOT)/commands

PYTHON_PROJECTS_NAMES ?= online.fxreader.pr34
python_whl:
	for f in $(PYTHON_PROJECTS_NAMES); do \
		./m.py deploy:wheel -o releases/whl -p $$f; \
	done

python_put: python_put_dist python_put_env

dotfiles_put:
	mkdir -p $(INSTALL_ROOT)
	cp dotfiles/.local/bin/gnome-shortcuts-macbook-air $(INSTALL_ROOT)/
	mkdir -p ~/.sway
	cp dotfiles/.sway/config ~/.sway/config
	cp dotfiles/.zshenv ~/.zshenv
	cp dotfiles/.zshrc ~/.zshrc
	cp dotfiles/.vimrc ~/.vimrc
	cp dotfiles/.tmux.conf ~/.tmux.conf
	cp dotfiles/.py3.vimrc ~/.py3.vimrc
	cp dotfiles/.py3.vimrc ~/.py3.vimrc
	cp dotfiles/.gitconfig ~/.gitconfig
	cp -rp \
		dotfiles/.ipython/profile_default/ipython_config.py \
		~/.ipython/profile_default/ipython_config.py
	D1=Code\ -\ OSS; \
	for p in \
		"dotfiles/.config/$$D1/User/keybindings.json" \
		"dotfiles/.config/$$D1/User/settings.json"; do \
		commands install -f -p "dotfiles/.config/$$D1"  -s "$$p" -t ~/.config/"$$D1"; \
	done
	#commands install -f -p dotfiles -s dotfiles/ -t ~/.config/

PLATFORM ?= macbook_air_2012
PLATFORM_TMP ?= tmp/platform_dotfiles/$(PLATFORM)

dotfiles_put_platform:
	@echo to be installed
	find platform_dotfiles/$(PLATFORM);
	echo remove $(PLATFORM_TMP)'?'; read; sudo rm -fr $(PLATFORM_TMP)
	sudo mkdir -p $(PLATFORM_TMP)
	sudo cp -rp -T platform_dotfiles/$(PLATFORM)/ $(PLATFORM_TMP)
	sudo chown -R root:root $(PLATFORM_TMP)
	sudo cp -rp -T $(PLATFORM_TMP) /
	sudo udevadm control --reload
	sudo systemctl daemon-reload

dotfiles_fetch:
	commands install -f -p ~ -s ~/.config/katerc -t dotfiles
	commands install -f -p ~ -s ~/.mime.types -t dotfiles
	commands install -f -p ~ -s ~/.config/rofi/config.rasi -t dotfiles
	commands install -f -p ~ -s ~/.config/Code\ -\ OSS/User/keybindings.json -t dotfiles
	commands install -f -p ~ -s ~/.config/Code\ -\ OSS/User/settings.json -t dotfiles

DOTFILES_VERSION ?= 0.1

dotfiles_deploy:
	mkdir -p releases/tar
	tar -cvf - \
		dotfiles \
	| xz --compress -9 --stdout > \
	releases/tar/dotfiles-$(DOTFILES_VERSION).tar.xz

systemd:
	/usr/bin/env python3 d1/systemd.py
	for d in tmp/d1; do \
		(\
			cd $$d; \
			for i in *.service *.timer; do \
				sudo ln -s -f $$PWD/$$i /etc/systemd/system/$$i; \
			done; \
		); \
	done
	sudo systemctl daemon-reload

venv:
	uv venv
	uv pip install -p .venv \
	-r requirements.txt

venv_compile:
	uv pip compile --generate-hashes \
	requirements.in > requirements.txt

MYPY_SOURCES ?= \
	d1/cpanel.py
mypy:
	. .venv/bin/activate && \
	mypy --strict --follow-imports silent \
	$(MYPY_SOURCES)
