NAME = email-reminder
PY_BINNAME = email-reminder-editor
PY_FILES = $(PY_BINNAME) EmailReminder/*.py
GO_BINARIES = collect-reminders send-reminders
VERSION := $(shell grep "VERSION =" $(PY_BINNAME) | grep -o "'[0-9]*\.[0-9]*\.[0-9]*'" | sed "s/'//g")
APPNAME = $(NAME)-$(VERSION)

# Standard installation directories
DESTDIR ?=
PREFIX ?= /usr
BINDIR ?= $(PREFIX)/bin
SBINDIR ?= $(PREFIX)/sbin
DATADIR ?= $(PREFIX)/share
MANDIR ?= $(DATADIR)/man
SYSCONFDIR ?= /etc

GO ?= go
GOFLAGS ?=
GO_LDFLAGS ?=


.PHONY: build clean install test lint \
        pyflakes pydocstyle pycodestyle codespell pylint ruff isort bandit \
        release commitsign

build: $(GO_BINARIES)

internal/config/version: $(PY_BINNAME)
	echo -n "$(VERSION)" > $@

collect-reminders: internal/config/version
	$(GO) build $(GOFLAGS) -ldflags "$(GO_LDFLAGS)" -o $@ ./cmd/collect-reminders

send-reminders: internal/config/version
	$(GO) build $(GOFLAGS) -ldflags "$(GO_LDFLAGS)" -o $@ ./cmd/send-reminders

clean:
	rm -f internal/config/version
	rm -f $(GO_BINARIES)
	rm -f $(APPNAME).tar.gz
	rm -f $(APPNAME).tar.gz.asc
	find -name "*.pyc" -delete

install: build
	install -d $(DESTDIR)$(BINDIR)
	install -d $(DESTDIR)$(SBINDIR)
	install -d $(DESTDIR)$(DATADIR)/applications
	install -d $(DESTDIR)$(DATADIR)/metainfo
	install -d $(DESTDIR)$(MANDIR)/man1
	install -d $(DESTDIR)$(MANDIR)/man8
	install -m 0755 send-reminders $(DESTDIR)$(BINDIR)/
	install -m 0755 $(PY_BINNAME) $(DESTDIR)$(BINDIR)/
	install -m 0755 collect-reminders $(DESTDIR)$(SBINDIR)/
	install -m 0644 $(PY_BINNAME).1 $(DESTDIR)$(MANDIR)/man1/
	install -m 0644 send-reminders.1 $(DESTDIR)$(MANDIR)/man1/
	install -m 0644 collect-reminders.8 $(DESTDIR)$(MANDIR)/man8/
	install -m 0644 $(NAME).desktop $(DESTDIR)$(DATADIR)/applications/
	install -m 0644 net.launchpad.email_reminder.metainfo.xml $(DESTDIR)$(DATADIR)/metainfo/

test: internal/config/version
	$(GO) test $(GOFLAGS) ./...

pyflakes:
	@echo Running pyflakes...
	@pyflakes3 $(PY_FILES)

pydocstyle:
	@echo Running pydocstyle...
	@pydocstyle

pycodestyle:
	@echo Running pycodestyle...
	@pycodestyle $(PY_FILES)

codespell:
	@echo Running codespell...
	@codespell $(PY_FILES)

pylint:
	@echo Running pylint...
	@pylint $(PY_FILES)

ruff:
	@echo Running ruff...
	@ruff check $(PY_FILES)

isort:
	@echo Running isort...
	@isort --check-only --diff $(PY_FILES)

bandit:
	@echo Running bandit...
	@bandit --quiet -r .

lint: pycodestyle pydocstyle pyflakes pylint ruff isort codespell bandit
	@echo Validating appstream data...
	@appstreamcli validate --strict net.launchpad.email_reminder.metainfo.xml

$(APPNAME).tar.gz:
	git archive --format=tar.gz --prefix=$(APPNAME)/ -o $@ HEAD

release: $(APPNAME).tar.gz
	gpg -a --detach-sign $<

commitsign:
	git commit -a -m "Bump version and changelog for release"
	git tag -s $(APPNAME) -m "$(VERSION) release"
