# terraria-server-manager
#
#   make check                 syntax-check the scripts (and shellcheck, if installed)
#   make deb                   build build/terraria-server-manager_<version>_all.deb
#   sudo make install          install directly (default PREFIX=/usr/local), no package
#   sudo make uninstall        remove what 'make install' installed (keeps your data)
#   make clean                 remove build/

NAME        := terraria-server-manager
VERSION     := $(shell cat VERSION)

PREFIX      ?= /usr/local
SYSCONFDIR  ?= /etc
BINDIR      := $(PREFIX)/bin
UNITDIR     := $(PREFIX)/lib/systemd/system
SYSUSERSDIR := $(PREFIX)/lib/sysusers.d
TMPFILESDIR := $(PREFIX)/lib/tmpfiles.d
DOCDIR      := $(PREFIX)/share/doc/$(NAME)
DATADIR     := $(PREFIX)/share/$(NAME)
COMPLETIONDIR := $(PREFIX)/share/bash-completion/completions

# Fill in version and install paths
SUBST = sed -e 's|@VERSION@|$(VERSION)|g' -e 's|@BINDIR@|$(BINDIR)|g' -e 's|@DOCDIR@|$(DOCDIR)|g'

MAINT_SCRIPTS := packaging/deb/postinst packaging/deb/prerm packaging/deb/postrm
STAGE         := build/deb-root
DEB           := build/$(NAME)_$(VERSION)_all.deb

.PHONY: all check install uninstall deb clean

all: check

check:
	bash -n bin/terractl
	bash -n completions/bash/terractl
	for f in $(MAINT_SCRIPTS); do sh -n $$f; done
	@if command -v shellcheck >/dev/null; then \
		shellcheck bin/terractl completions/bash/terractl $(MAINT_SCRIPTS); \
	else \
		echo "shellcheck not installed; skipped (apt install shellcheck)"; \
	fi

install:
	install -d $(DESTDIR)$(BINDIR) $(DESTDIR)$(UNITDIR) $(DESTDIR)$(SYSUSERSDIR) \
		$(DESTDIR)$(TMPFILESDIR) $(DESTDIR)$(DOCDIR) $(DESTDIR)$(DATADIR) \
		$(DESTDIR)$(COMPLETIONDIR) $(DESTDIR)$(SYSCONFDIR)/default
	$(SUBST) bin/terractl > $(DESTDIR)$(BINDIR)/terractl
	chmod 755 $(DESTDIR)$(BINDIR)/terractl
	$(SUBST) systemd/terraria@.service > $(DESTDIR)$(UNITDIR)/terraria@.service
	install -m 644 systemd/terraria@.socket $(DESTDIR)$(UNITDIR)/
	install -m 644 config/sysusers.d/terractl.conf $(DESTDIR)$(SYSUSERSDIR)/
	install -m 644 config/tmpfiles.d/terractl.conf $(DESTDIR)$(TMPFILESDIR)/
	install -m 644 docs/guide.md $(DESTDIR)$(DOCDIR)/README.md
	install -m 644 tools/windows/export-tmod-mods.ps1 $(DESTDIR)$(DATADIR)/
	install -m 644 completions/bash/terractl $(DESTDIR)$(COMPLETIONDIR)/terractl
	@# Never overwrite local settings
	@if [ -e $(DESTDIR)$(SYSCONFDIR)/default/terractl ]; then \
		echo "keeping existing $(DESTDIR)$(SYSCONFDIR)/default/terractl"; \
	else \
		install -m 644 config/default/terractl $(DESTDIR)$(SYSCONFDIR)/default/terractl; \
	fi
	@# On a real install (not staging for a package), set up the user and directories
	@if [ -z "$(DESTDIR)" ]; then \
		systemd-sysusers terractl.conf && \
		systemd-tmpfiles --create terractl.conf && \
		if [ -d /run/systemd/system ]; then systemctl daemon-reload; fi && \
		echo "Installed terractl $(VERSION). See $(DOCDIR)/README.md"; \
	fi

uninstall:
	rm -f $(DESTDIR)$(BINDIR)/terractl \
		$(DESTDIR)$(UNITDIR)/terraria@.service $(DESTDIR)$(UNITDIR)/terraria@.socket \
		$(DESTDIR)$(SYSUSERSDIR)/terractl.conf $(DESTDIR)$(TMPFILESDIR)/terractl.conf \
		$(DESTDIR)$(COMPLETIONDIR)/terractl
	rm -rf $(DESTDIR)$(DOCDIR) $(DESTDIR)$(DATADIR)
	@if [ -z "$(DESTDIR)" ] && [ -d /run/systemd/system ]; then systemctl daemon-reload; fi
	@echo "Removed. Kept: $(SYSCONFDIR)/default/terractl, /var/lib/terraria and the terraria user."

deb: check
	rm -rf $(STAGE)
	$(MAKE) --no-print-directory install DESTDIR=$(CURDIR)/$(STAGE) PREFIX=/usr
	install -d -m 755 $(STAGE)/DEBIAN
	install -m 644 packaging/deb/conffiles $(STAGE)/DEBIAN/
	install -m 755 $(MAINT_SCRIPTS) $(STAGE)/DEBIAN/
	sed -e 's|@VERSION@|$(VERSION)|' \
	    -e "s|@INSTALLED_SIZE@|$$(du -sk --exclude=DEBIAN $(STAGE) | cut -f1)|" \
	    packaging/deb/control > $(STAGE)/DEBIAN/control
	find $(STAGE) -type d -exec chmod 755 {} +
	dpkg-deb --build --root-owner-group $(STAGE) $(DEB)

clean:
	rm -rf build
