# showkey -- keystroke echoer
#
# SPDX-FileCopyrightText: (C) Eric S. Raymond <esr@thyrsus.com>
# SPDX-License-Identifier: MIT

PREFIX      ?= /usr/local
BINDIR      ?= $(PREFIX)/bin
DATADIR     ?= $(PREFIX)/share
MANDIR      ?= $(DATADIR)/man

CFLAGS += -O

VERSION=$(shell sed -n <NEWS '/^[0-9]/s/:.*//p' | head -1)

# Rules

# Note: to suppress the footers with timestamps being generated in HTML,
# we use "-a nofooter".
# To debug asciidoc problems, you may need to run "xmllint --nonet --noout --valid"
# on the intermediate XML that throws an error.
.SUFFIXES: .html .adoc .1

.adoc.1:
	asciidoctor -D. -a nofooter -b manpage $<
.adoc.html:
	asciidoctor -D. -a nofooter -a webfonts! $<

.PHONY: all clean check cppcheck spellcheck reflow
.PHONY: install uninstall version dist release refresh

# Build

all:  showkey

showkey: showkey.c
	$(CC) -DREVISION=$(VERSION) $(CFLAGS) showkey.c -o showkey

clean:
	rm -f showkey showkey.o showkey.1 *.html *.tar.gz

cppcheck:
	@cppcheck --quiet -DREVISION=$(VERSION) showkey.c

spellcheck:
	@spellcheck showkey.adoc

reflow:
	@clang-format --style="{IndentWidth: 8, UseTab: ForIndentation}" -i $$(find . -name "*.[ch]")

# Install/uninstall

install:
	install -d $(DESTDIR)$(BINDIR)
	install -m 755 showkey $(DESTDIR)$(BINDIR)/showkey
	install showkey.1 $(DESTDIR)$(MANDIR)/man1/showkey.1

uninstall:
	rm -f $(DESTDIR)$(BINDIR)/showkey
	install -d $(DESTDIR)$(MANDIR)
	install -d $(DESTDIR)$(MANDIR)/man1
	rm -f $(DESTDIR)$(MANDIR)/man1/showkey.1

# Export

SOURCES = README.adoc COPYING NEWS control Makefile showkey.c showkey.adoc

showkey-$(VERSION).tar.gz: $(SOURCES)
	mkdir showkey-$(VERSION)
	cp -r $(SOURCES) showkey-$(VERSION)
	tar -czf showkey-$(VERSION).tar.gz showkey-$(VERSION)
	rm -fr showkey-$(VERSION)
	ls -l showkey-$(VERSION).tar.gz

version:
	@echo $(VERSION)

dist: showkey-$(VERSION).tar.gz

release: showkey-$(VERSION).tar.gz showkey.html
	shipper version=$(VERSION) | sh -e -x

refresh: showkey.html
	shipper -N -w version=$(VERSION) | sh -e -x

# end
