BUILD_COMMIT       = $(shell git rev-parse HEAD)
BASE_IMAGE         = registry.fedoraproject.org/fedora-toolbox
BASE_IMAGE_VERSION = $(shell cat /etc/os-release | grep VERSION_ID | cut -d = -f 2)
REPOSITORY         = localhost/toolbox-dev-env
STARSHIP_COLOR     = $(shell cat shell/starship.$(HOSTNAME).color)

.PHONY: toolbox
toolbox:
	podman build ./toolbox/ --tag $(REPOSITORY):$(BUILD_COMMIT) --build-arg BASE_IMAGE="$(BASE_IMAGE)" --build-arg BASE_IMAGE_VERSION="$(BASE_IMAGE_VERSION)" --build-arg BUILD_COMMIT="$(BUILD_COMMIT)"
	podman tag $(REPOSITORY):$(BUILD_COMMIT) $(REPOSITORY):latest

	systemctl --user enable podman.socket --now
	systemctl --user enable podman.service --now

.PHONY: shell
shell:
	mkdir --parents ~/.local/share/fonts/
	curl -sSLo ~/.local/share/fonts/nerdfont.zip https://github.com/ryanoasis/nerd-fonts/releases/download/v3.4.0/AdwaitaMono.zip
	unzip -o ~/.local/share/fonts/nerdfont.zip -d ~/.local/share/fonts/
	rm ~/.local/share/fonts/nerdfont.zip
	fc-cache -f -v

	mkdir --parents ~/.local/bin
	curl -sSo /tmp/starship.sh https://starship.rs/install.sh
	sh /tmp/starship.sh --yes --bin-dir=$(HOME)/.local/bin

	mkdir --parents ~/.config/bashrc.d
	cp shell/scripts.d/*.sh ~/.config/bashrc.d/

	mkdir --parents ~/.config/completions.d
	cp shell/completions.d/*.completion ~/.config/completions.d/

	cp shell/bashrc.sh ~/.bashrc
	cp shell/inputrc ~/.inputrc
	cp shell/starship.toml ~/.config/starship.toml
	sed -i "s/#XXXXXX/$(STARSHIP_COLOR)/g" ~/.config/starship.toml

.PHONY: vscodium
vscodium:
	mkdir --parents ~/.config/VSCodium/User
	cp vscodium/settings.json ~/.config/VSCodium/User/settings.json
	cp vscodium/keybindings.json ~/.config/VSCodium/User/keybindings.json

	toolbox create --image $(REPOSITORY):latest vscodium-setup-temp
	toolbox run --container=vscodium-setup-temp codium --install-extension="tamasfe.even-better-toml"
	toolbox run --container=vscodium-setup-temp codium --install-extension="samuelcolvin.jinjahtml"
	toolbox run --container=vscodium-setup-temp codium --install-extension="yzhang.markdown-all-in-one"
	toolbox run --container=vscodium-setup-temp codium --install-extension="streetsidesoftware.code-spell-checker"
	toolbox run --container=vscodium-setup-temp codium --install-extension="github.vscode-github-actions"
	toolbox run --container=vscodium-setup-temp codium --install-extension="redhat.vscode-yaml"
	toolbox run --container=vscodium-setup-temp codium --install-extension="ms-python.debugpy"
	toolbox run --container=vscodium-setup-temp codium --install-extension="ms-python.python"
	toolbox run --container=vscodium-setup-temp codium --install-extension="detachhead.basedpyright"
	toolbox run --container=vscodium-setup-temp codium --install-extension="opentofu.vscode-opentofu"
	toolbox run --container=vscodium-setup-temp codium --install-extension="golang.go"
	toolbox run --container=vscodium-setup-temp codium --update-extensions
	podman stop vscodium-setup-temp
	podman rm vscodium-setup-temp

.PHONY: act-runner
act-runner:
	systemctl enable libvirtd --now
	mkdir --parents ~/.local/share/act-runner
	cp act-runner/act-runner.bu ~/.local/share/act-runner/act-runner.bu
	bash act-runner/rebuild.bash $(HOME)/.local/share/act-runner

.PHONY: install
install: toolbox shell vscodium act-runner;

