#	Makefile for bs, the Battleships game
#
# SPDX-License-Identifier: BSD-2-Clause
# SPDX-FileCopyrightText: (C) Eric S. Raymond <esr@thyrsus.com>

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

VERSION=$(shell sed -n <NEWS.adoc '/::/s/^\([0-9][^:]*\).*/\1/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 .6

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

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

# Build

bs: bs.c
	$(CC) $(CFLAGS) -DVERSION='"$(VERSION)"' -o bs bs.c -lncurses

clean:
	rm -f bs bs.6 bs*.tar.gz bs*.rpm *~ *.html

# Validate

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

cppcheck:
	@cppcheck --quiet --template=gcc --enable=all --suppress=checkersReport --suppress=missingIncludeSystem --suppress=redundantContinue --suppress=knownConditionTrueFalse bs.c

spellcheck:
	@spellcheck bs.adoc

# Install/uninstall

# Note: does not install desktop file.
install: bs.6 uninstall
	install -m 0755 -d $(DESTDIR)$(BINDIR)
	install -m 0755 -d $(DESTDIR)$(MANDIR)/man6
	install -m 0755 -d $(DESTDIR)//usr/share/applications/
	install -m 0755 -d $(DESTDIR)/usr/share/icons/hicolor/32x32/apps/
	install -m 0755 -d $(DESTDIR)/usr/share/appdata
	install -m 0755 bs $(DESTDIR)$(MANDIR)/
	install -m 0644 bs.6 $(DESTDIR)$(MANDIR)/man6/
	install -m 0644 bs.desktop $(DESTDIR)/usr/share/applications/
	install -m 0644 battleship.png $(DESTDIR)/usr/share/icons/hicolor/32x32/apps/
	install -m 0644 bs.adoc $(DESTDIR)/usr/share/appdata/

uninstall:
	rm -f $(BINDIR)/bs /usr/share/man/man6/bs.6

# Export

SOURCES = README.adoc COPYING NEWS.adoc control bs.c Makefile bs.adoc battleship.png bs.desktop

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

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

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

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

# end
