# Makefile for the doclifter project
#
# SPDX-FileCopyrightText: Copyright Eric S. Raymond <esr@thyrsus.com>
# SPDX-License-Identifier: BSD-2-Clause

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

VERSION=$(shell sed <doclifter -n -e '/^version *= *"\(.*\)"/s//\1/p')

# 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 htmlclean check pylint spellcheck reflow
.PHONY: install uninstall version dist release refresh

# Build

all: doclifter.1 doclifter.html manlifter.1 manlifter.htm;

clean:
	rm -f doclifter.html manlifter.html doclifter.1 manlifter.1
	rm -f *.pyc docliftertest.xml foobar* fixed* *~ bugs.html
	rm -f index.html *.tar.gz *.md5 *old

htmlclean:
	rm *.html

# Validate

# Note: This will show a spurious diff if pic2plot is not installed.
check:
	@cd tests >/dev/null; make --quiet

pylint:
	@pylint --score=n doclifter

spellcheck:
	spellcheck local.dic doclifter.adoc manlifter.adoc

reflow:
	@black doclifter manlifter buglist.py

# Install

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

uninstall:
	rm -f $(DESTDIR)$(BINDIR)/doclifter
	rm -f $(DESTDIR)$(BINDIR)/manlifter
	rm -f $(DESTDIR)$(MANDIR)/man1/doclifter.1
	rm -f $(DESTDIR)$(MANDIR)/man1/manlifter.1

# Export

DOCS    = README.adoc COPYING TODO PATCHES \
		local.dic doclifter.adoc doclifter.1 manlifter.adoc manlifter.1
SOURCES = doclifter manlifter Makefile $(DOCS) tests/ doclifter-logo.png

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

doclifter-$(VERSION).md5: doclifter-$(VERSION).tar.gz
	@md5sum doclifter-$(VERSION).tar.gz >doclifter-$(VERSION).md5

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

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

release: doclifter-$(VERSION).tar.gz doclifter-$(VERSION).md5 doclifter.html manlifter.html
	@[ $(VERSION) = $(NEWSVERSION) ] || { echo "Version mismatch!"; exit 1; }
	shipper version=$(VERSION) | sh -e -x

refresh: htmlclean doclifter.html manlifter.html
	@[ $(VERSION) = $(NEWSVERSION) ] || { echo "Version mismatch!"; exit 1; }
	shipper -N -w version=$(VERSION) | sh -e -x

# This is used only for updating the bugs page on my website.
# It won't work for anyone else.
REMOTE=esr@login.ibiblio.org
UPDIR=/public/html/catb/esr/doclifter
update:
	problemgen.py >bugs.html
	ssh $(REMOTE) rm -fr $(UPDIR)/prepatch
	scp -r bugs.html prepatch/ $(REMOTE):$(UPDIR)

# end
