PROJECT_NAME := pvcollect

PREFIX ?= /usr/bin

CONFIG_DIR := /etc/$(PROJECT_NAME)
SYSTEM_DIR := /usr/lib/systemd/system

CONFIG_FILE := config/config.json
BIN_FILE := bin/$(PROJECT_NAME)
UNIT_FILE := pvcollect.service
README_FILE := README.md

.PHONY: all
all:
	mkdir -p bin
	go build -buildvcs=false -o $(BIN_FILE)

.PHONY: clean
clean:
	go clean
	rm -rf bin

.PHONY: install
install: all
	@if [ -f $(CONFIG_DIR)/$(notdir $(CONFIG_FILE)) ]; then \
		echo "$(CONFIG_DIR)/$(notdir $(CONFIG_FILE)) already exists - skipping..."; \
	else \
		install -d $(CONFIG_DIR); \
		install -m 0644 $(CONFIG_FILE) $(CONFIG_DIR); \
		echo "install -d $(CONFIG_DIR)"; \
		echo "install -m 0644 $(CONFIG_FILE) $(CONFIG_DIR)"; \
	fi
	install -d $(PREFIX)
	install -m 0755 $(BIN_FILE) $(PREFIX)
	install -d $(SYSTEM_DIR)
	install -m 0644 $(UNIT_FILE) $(SYSTEM_DIR)

.PHONY: uninstall
uninstall:
	rm -rf $(CONFIG_DIR)
	rm -rf $(SYSTEM_DIR)/$(UNIT_FILE)
	rm -rf $(PREFIX)/$(PROJECT_NAME)

.PHONY: package
package: all
	tar cvzf $(PROJECT_NAME).tar.gz $(CONFIG_FILE) $(BIN_FILE) $(UNIT_FILE) $(README_FILE)
