SUDO ?= sudo

TOP_SOURCES := $(shell git ls-files | grep -vE 'README|Makefile|^config|^\.git|^\.travis' | grep -v /)
CONFIG_SOURCES := $(shell git ls-files config)
LOCAL_BIN_SOURCES := $(shell git ls-files local/bin)
LOCAL_BG_SOURCES := $(shell git ls-files local/share/backgrounds)
TARGETS := \
	$(patsubst %,$(HOME)/.%,$(TOP_SOURCES)) \
	$(patsubst %,$(HOME)/.%,$(CONFIG_SOURCES)) \
	$(patsubst %,$(HOME)/.%,$(LOCAL_BIN_SOURCES)) \
	$(patsubst %,$(HOME)/.%,$(LOCAL_BG_SOURCES)) \
	$(HOME)/.config/i3/config \
	/etc/udev/rules.d/90-trackman-marble.rules \
	/usr/local/bin/handle-trackman-marble

$(HOME)/.%: %
	$(RM) '$@' && mkdir -p $(dir $@) && ln -svf '$(PWD)/$^' '$@'

.PHONY: all
all: $(HOME)/.config $(HOME)/.local/bin $(HOME)/.local/share/backgrounds $(TARGETS)

$(HOME)/.config:
	mkdir -p $@

$(HOME)/.local/bin:
	mkdir -p $@

$(HOME)/.local/share/backgrounds:
	mkdir -p $@

/etc/udev/rules.d/90-trackman-marble.rules: udev/rules.d/90-trackman-marble.rules
	$(SUDO) install -m 0644 -v udev/rules.d/90-trackman-marble.rules $@

/usr/local/bin/handle-trackman-marble: udev/handle-trackman-marble
	$(SUDO) install -m 0755 -v udev/handle-trackman-marble $@

.PHONY: echo
echo:
	@echo TOP_SOURCES=$(TOP_SOURCES)
	@echo CONFIG_SOURCES=$(CONFIG_SOURCES)
	@echo LOCAL_BIN_SOURCES=$(LOCAL_BIN_SOURCES)
	@echo TARGETS=$(TARGETS)
