# carnet - note taking logbook helper.
# Copyright (C) 2024-2025, Étienne Mollier <emollier@emlwks999.eu>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

PROG = carnet
ACTIVITY = carnet_activity.pl
MANUAL = $(PROG:%=%.1)
MANGZ = $(MANUAL:%=%.gz)
ICONSRC = $(PROG:%=%.fodg)
ICON = $(PROG:%=%.svg)
VERSION != ./$(PROG) --version | sed -n '1s/carnet //p'
ARCHIVE = $(PROG)-$(VERSION)

PROVISIONAL_PREFIX != [ /root = $(HOME) ] && echo /usr/local || echo $(HOME)/.local
PREFIX ?= $(PROVISIONAL_PREFIX)
BINDIR ?= $(PREFIX)/bin
MANDIR ?= $(PREFIX)/share/man/man1
DESTDIR ?=
CURDIR ?= $(PWD)

RM ?= rm -vf
MKDIR ?= mkdir -p
INSTALL ?= install

all: man
.PHONY: all

help:
	@printf -- "Available targets are:\n\
	  * clean      erase autogenerated artifacts.\n\
	  * icon       create the svg icon from libreoffice drawing.\n\
	  * install    deploy carnet to its installation directory.\n\
	  * uninstall  remove carnet from its installation directory.\n\
	  * help       print this help message.\n\
	  * man        build the manual page.\n\
	  * read       quickly lookup the manual page.\n\
	  * test       quickly run through all carnet commands.\n\
	  * archive    compress the source to facilitate transfer.\n\
	  * sign       certify a fresh archive with gpg.\n\
	"
.PHONY: help

man: $(MANGZ)
.PHONY: man

# Restore files in the pristine state of the release tarball.
clean:
	$(RM) $(MANUAL) $(MANGZ) $(ICON) BUGS.roff
	$(RM) $(PROG)-*.tar.xz $(PROG)-*.tar.xz.asc
.PHONY: clean

$(MANUAL): $(PROG) BUGS.roff
	help2man \
		--name 'note taking logbook helper' \
		--section 1 \
		--locale C.UTF-8 \
		--include BUGS.roff \
		--output $(PROG:%=%.1) \
		--no-info \
		./$(PROG)

BUGS.roff: BUGS
	echo '[REPORTING BUGS]' > BUGS.roff
	sed 's/^$$/.PP/' BUGS >> BUGS.roff

$(MANGZ): $(MANUAL)
	gzip --verbose --force --best --keep $(MANUAL)

read: $(MANUAL)
	@man ./$(MANUAL)
.PHONY: read

icon: $(ICON)
.PHONY: icon

$(ICON): $(ICONSRC)
	libreoffice --nolockcheck --convert-to svg $(ICONSRC)

test:
	$(CURDIR)/scripts/test.sh
.PHONY: test

install: $(PROG) $(ACTIVITY) $(MANGZ)
	$(MKDIR) $(DESTDIR)$(BINDIR)
	$(INSTALL) $(PROG) $(DESTDIR)$(BINDIR)
	chmod 0755 $(DESTDIR)$(BINDIR)/$(PROG)
	$(INSTALL) $(ACTIVITY) $(DESTDIR)$(BINDIR)
	chmod 0755 $(DESTDIR)$(BINDIR)/$(ACTIVITY)
	$(MKDIR) $(DESTDIR)$(MANDIR)
	$(INSTALL) $(MANGZ) $(DESTDIR)$(MANDIR)
.PHONY: install

uninstall:
	$(RM) $(DESTDIR)$(BINDIR)/$(PROG)
	$(RM) $(DESTDIR)$(BINDIR)/$(ACTIVITY)
	$(RM) $(DESTDIR)$(MANDIR)/$(MANGZ)
.PHONY: uninstall

archive: $(ARCHIVE).tar.xz
.PHONY: archive

sign: $(ARCHIVE).tar.xz.asc
.PHONY: sign

$(ARCHIVE).tar.xz:
	$(MAKE) clean
	@if [ -d $(CURDIR)/../$(ARCHIVE)/ ] \
	; then echo 'warning: $(CURDIR)/../$(ARCHIVE)/ already exists.' \
	;      echo '         Stopping now, please examine the situation.' \
	;      exit 1 \
	; fi
	cp -a $(CURDIR)/ $(CURDIR)/../$(ARCHIVE)
	$(RM) -r $(CURDIR)/../$(ARCHIVE)/.git
	cd $(CURDIR)/.. && tar cvf $(ARCHIVE).tar $(ARCHIVE)
	mv -v $(CURDIR)/../$(ARCHIVE).tar .
	$(RM) -r $(CURDIR)/../$(ARCHIVE)/
	xz --best $(ARCHIVE).tar

$(ARCHIVE).tar.xz.asc: $(ARCHIVE).tar.xz
	gpg --armor --detach --sign $(ARCHIVE).tar.xz

# This command to produce carnet releases is to be reserved for use when
# maintaining the software via git, as the CHANGES file is automatically
# produced by git information before the publication.  For modifications
# and internal redistribution while only relying on tarballs, "archive"
# and "sign" targets may be preferrable.
release:
	$(MAKE) CHANGES
	@ set -e \
	; COMMIT_VERSION="$$(sed -n '5s/ *carnet: bump to version //p' CHANGES)" \
	; if [ "$${COMMIT_VERSION%.}" != "$(VERSION)" ] \
	; then echo "error: last commit mismatches carnet $(VERSION)." >&2 \
	;      echo "       Version caught:   '$${COMMIT_VERSION%.}'" >&2 \
	;      echo "       Version expected: '$(VERSION)'" >&2 \
	;      echo "       Make sure the last commit was for the version bump." >&2 \
	;      exit 1 \
	; fi
	$(MAKE) archive
	$(MAKE) sign
.PHONY: release

CHANGES: .git/COMMIT_EDITMSG
	git log > CHANGES

# Restore files in the pristine state of the git tree.  The difference
# with "clean" is that we remove files that cannot be commited, because
# they are generated from information pulled from the VCS.
purge: clean
	$(RM) CHANGES
.PHONY: purge
