# Build the library

.PHONY: all clean

all: lib/libplayit.sh.2

lib/libplayit.sh.2: src/*/*.sh
	mkdir --parents lib
	find src -type f -name '*.sh' -print0 | LC_ALL=C sort -z | xargs -0 cat > lib/libplayit.sh.2

clean:
	rm --force lib/libplayit.sh.2


# Install the command. library, and man pages

.PHONY: install uninstall

## Set the default install paths
UID := $(shell id --user)
ifeq ($(UID),0)
    prefix = /usr/local
else
    prefix = $(HOME)/.local
endif
bindir = $(prefix)/bin
libdir = $(prefix)/lib
datadir = $(prefix)/share
mandir = $(datadir)/man
zshdir = $(datadir)/zsh
fishdir = $(datadir)/fish

install: all
	install -D --mode=755 play.it $(DESTDIR)$(bindir)/play.it
	install -D --mode=644 lib/libplayit.sh.2 $(DESTDIR)$(libdir)/play.it/libplayit.sh.2
	install -D --mode=644 man/man6/play.it.6 $(DESTDIR)$(mandir)/man6/play.it.6
	install -D --mode=644 man/fr/man6/play.it.6 $(DESTDIR)$(mandir)/fr/man6/play.it.6
	# Library compatibility link
	mkdir --parents $(DESTDIR)$(datadir)/play.it
	ln --symbolic --relative --force \
		$(DESTDIR)$(libdir)/play.it/libplayit.sh.2 \
		$(DESTDIR)$(datadir)/play.it/libplayit2.sh

uninstall: uninstall-completion
	rm --force \
		$(DESTDIR)$(bindir)/play.it \
		$(DESTDIR)$(libdir)/play.it/libplayit.sh.2 \
		$(DESTDIR)$(mandir)/man6/play.it.6 \
		$(DESTDIR)$(mandir)/fr/man6/play.it.6
	rmdir --parents --ignore-fail-on-non-empty \
		$(DESTDIR)$(bindir) \
		$(DESTDIR)$(libdir)/play.it \
		$(DESTDIR)$(mandir)/man6 \
		$(DESTDIR)$(mandir)/fr/man6
	# Library compatibility link
	rm --force $(DESTDIR)$(datadir)/play.it/libplayit2.sh
	rmdir --parents --ignore-fail-on-non-empty $(DESTDIR)$(datadir)/play.it


# Install shell completion

.PHONY: install-completion-all install-completion-zsh install-completion-fish uninstall-completion

install-completion-all: install-completion-zsh install-completion-fish

install-completion-zsh:
	install -D --mode=644 shell-completions/zsh/_play.it $(DESTDIR)$(zshdir)/vendor-completions/_play.it

install-completion-fish:
	install -D --mode=644 shell-completions/fish/play.it.fish $(DESTDIR)$(fishdir)/vendor_completions.d/play.it.fish

uninstall-completion:
	rm --force $(DESTDIR)$(zshdir)/vendor-completions/_play.it
	rm --force $(DESTDIR)$(fishdir)/vendor_completions.d/play.it.fish
	test -d $(DESTDIR)$(zshdir)/vendor-completions && \
		rmdir --parents --ignore-fail-on-non-empty $(DESTDIR)$(zshdir)/vendor-completions || true
	test -d $(DESTDIR)$(fishdir)/vendor_completions.d && \
		rmdir --parents --ignore-fail-on-non-empty $(DESTDIR)$(fishdir)/vendor_completions.d || true


# Release preparation

.PHONY: dist

## The generated tarball is signed with gpg by default,
## NO_SIGN should be set to a non-0 value to skip the signature.
NO_SIGN := 0

dist: VERSION = $(shell head --lines=1 CHANGELOG)
dist: TARBALL = play.it-$(VERSION).tar.gz
dist: TAR_OPTIONS := --sort=name --mtime=2017-06-14 --owner=root:0 --group=root:0 --use-compress-program='gzip --no-name'
dist: CHANGELOG LICENSE README.md Makefile play.it man/man6/play.it.6 man/fr/man6/play.it.6 shell-completions/zsh/_play.it shell-completions/fish/play.it.fish src/*/*.sh tests/regression/*.sh
	mkdir --parents dist
	LC_ALL=C tar cf dist/$(TARBALL) $(TAR_OPTIONS) \
		   CHANGELOG \
		   LICENSE \
		   README.md \
		   Makefile \
		   play.it \
		   man/man6/play.it.6 \
		   man/fr/man6/play.it.6 \
		   shell-completions/zsh/_play.it \
		   shell-completions/fish/play.it.fish \
		   src/*/*.sh \
		   tests/regression/*.sh
ifeq ($(NO_SIGN),0)
	rm --force dist/$(TARBALL).asc
	gpg --armor --detach-sign dist/$(TARBALL)
endif


# Run tests, including:
# - regression tests
# - man page syntax check

.PHONY: check check-regression check-man-syntax

check: check-regression

REGRESSION_SCRIPTS := $(wildcard tests/regression/2.*.sh)
REGRESSION_TESTS := $(addprefix check-regression_, $(REGRESSION_SCRIPTS))
.PHONY: $(REGRESSION_TESTS)
check-regression: $(REGRESSION_TESTS)
$(REGRESSION_TESTS): check-regression_%: %
	./$<

check: check-man-syntax

check-man-syntax:
	man --warnings --encoding=UTF-8 --local-file --troff-device=utf8 --ditroff man/man6/play.it.6 >/dev/null
	man --warnings --encoding=UTF-8 --local-file --troff-device=utf8 --ditroff man/fr/man6/play.it.6 >/dev/null
