topdir := $(dir $(lastword $(MAKEFILE_LIST)))
include $(topdir)/config.mk
pkgconfdir ?= $(sysconfdir)/libretools.d
pkglibexecdir ?= $(libexecdir)/libretools

# Misc variables for use in recipes ############################################

# Usage: <INPUT $(edit) >OUTPUT
# Expand m4_include macros to use librelib
# This is used when using sources grabbed from devtools
edit = sed -e 's|m4_include(lib/\(.*\))|. $$(librelib \1)|'

# Usage <INPUT $(pofmt) >OUTPUT
# Normalize a .po(t) file
pofmt = msguniq -Fi

xgettext-keywords-sh += --keyword={_,print}
xgettext-keywords-sh += --keyword={msg,msg2,warning,error,stat_busy,die}
xgettext-keywords-sh += --keyword={lock,slock}:3

xgettext-sh = xgettext --omit-header --from-code=UTF-8 -L shell -o -
xgettext-sh-std = $(xgettext-sh) $(xgettext-keywords-sh) $^

pot-remove-wrap = tr '\n' '\r' | sed 's/"\r\s*"//g' | tr '\r' '\n'
html-whitespace-collapse = sed -r -e 's/(\\n|\\t|\t)/ /g' -e 's/  +/ /g'
xgettext-sh-prose = $(xgettext-sh) -k --keyword={prose,bullet,flag:2} $^ | $(pot-remove-wrap) | $(html-whitespace-collapse) | sed '/^\#, sh-format/d'

# Usage: as a variable
# relative path to `/` from $(bindir)
# used for symlinking libexec files
rootdir = $(shell sed -r 's|^/||;s|[^/]+|..|g'<<<$(bindir))

# Detect things aboud the director we're in ####################################

progs += $(filter-out $(no-progs),$(shell find . -maxdepth 1 -type f -executable -printf '%f\n'))
confs += $(filter-out $(no-conf) ,$(wildcard *.conf))
mans  += $(filter-out $(no-mans) ,$(patsubst %.ronn,%,$(wildcard *.ronn)))
docs  += $(filter-out $(no-docs) ,$(wildcard *.md) $(wildcard *.ronn))
libexecs +=
libs  +=

copy_files +=
build_files += $(progs) $(confs) $(mans) $(patsubst %.in,%,$(copy_files))
install_files += $(addprefix $(DESTDIR)$(bindir)/,$(progs)) \
                 $(addprefix $(DESTDIR)$(pkgconfdir)/,$(confs)) \
                 $(addprefix $(DESTDIR)$(pkglibexecdir)/,$(libexecs) $(libs)) \
                 $(addprefix $(DESTDIR)$(docdir)/libretools/,$(docs)) \
                 $(addprefix $(DESTDIR)$(mandir)/man1/,$(filter %.1,$(mans))) \
                 $(addprefix $(DESTDIR)$(mandir)/man2/,$(filter %.2,$(mans))) \
                 $(addprefix $(DESTDIR)$(mandir)/man3/,$(filter %.3,$(mans))) \
                 $(addprefix $(DESTDIR)$(mandir)/man4/,$(filter %.4,$(mans))) \
                 $(addprefix $(DESTDIR)$(mandir)/man5/,$(filter %.5,$(mans))) \
                 $(addprefix $(DESTDIR)$(mandir)/man6/,$(filter %.6,$(mans))) \
                 $(addprefix $(DESTDIR)$(mandir)/man7/,$(filter %.7,$(mans))) \
                 $(addprefix $(DESTDIR)$(mandir)/man8/,$(filter %.8,$(mans)))
clean_files += $(patsubst %.in,%,$(copy_files)) $(copy_files) $(mans) $(wildcard *.pot)

pots  += $(filter-out $(no-pots) ,$(progs))
pot_files += $(addsuffix .pot,$(pots))

# Set the default target #######################################################

all: PHONY build

# Copy (files from devtools) ###################################################

copy: PHONY $(copy_files)

%.in: $(devtoolsdir)/%.in
	cp $< $@
%.in: $(devtoolsdir)/lib/%
	cp $< $@

# Build ########################################################################

build: PHONY $(build_files)

%: %.ronn
	ronn --roff $(RONNFLAGS) < '$<' > '$@'
%.html: %.ronn
	ronn --html $(RONNFLAGS) < '$<' > '$@'
%: %.in
	@echo "GEN $@"
	@$(edit) <"$<" >"$@" || { rm -f -- '$@'; false; }
	@chmod 755 "$@" || { rm -f -- '$@'; false; }

# Build ########################################################################

pot: everything.pot

everything.pot: $(pot_files)
	cat $^ | $(pofmt) > '$@'

%.pot: %
	{ $(xgettext-sh-std); $(xgettext-sh-prose); } | $(pofmt) > '$@'

# Install ######################################################################

install: PHONY $(install_files)

# We sort the explicit targets because sort also removes duplicates; in a few
# cases, a target could be listed twice, which is a warning.

# bindir: $(progs) vs $(libexecs)
$(sort $(addprefix $(DESTDIR)$(bindir)/,$(filter-out $(libexecs),$(progs)))): $(DESTDIR)$(bindir)/%: %
	install -Dm755 '$<' '$@'
$(sort $(addprefix $(DESTDIR)$(bindir)/,$(libexecs))): $(DESTDIR)$(bindir)/%: %
	install -d '$(@D)'
	ln -sf '$(rootdir)$(pkglibexecdir)/$(@F)' "$@"
# pkglibexecdir: $(libexecs) vs $(libs)
$(sort $(addprefix $(DESTDIR)$(pkglibexecdir)/,$(libexecs))): $(DESTDIR)$(pkglibexecdir)/%: %
	install -Dm755 '$<' '$@'
$(sort $(addprefix $(DESTDIR)$(pkglibexecdir)/,$(libs))): $(DESTDIR)$(pkglibexecdir)/%: %
	install -Dm644 '$<' '$@'
# everything else
$(DESTDIR)$(pkgconfdir)/%: %
	install -Dm644 '$<' '$@'
$(DESTDIR)$(docdir)/libretools/%: %
	install -Dm644 '$<' '$@'
$(DESTDIR)$(mandir)/man1/%.1: %.1
	install -Dm644 '$<' '$@'
$(DESTDIR)$(mandir)/man2/%.2: %.2
	install -Dm644 '$<' '$@'
$(DESTDIR)$(mandir)/man3/%.3: %.3
	install -Dm644 '$<' '$@'
$(DESTDIR)$(mandir)/man4/%.4: %.4
	install -Dm644 '$<' '$@'
$(DESTDIR)$(mandir)/man5/%.5: %.5
	install -Dm644 '$<' '$@'
$(DESTDIR)$(mandir)/man6/%.6: %.6
	install -Dm644 '$<' '$@'
$(DESTDIR)$(mandir)/man7/%.7: %.7
	install -Dm644 '$<' '$@'
$(DESTDIR)$(mandir)/man8/%.8: %.8
	install -Dm644 '$<' '$@'

# Clean ########################################################################

clean: PHONY clean-hook
	rm -f -- $(clean_files)

clean-hook: PHONY

# Boiler-plate #################################################################

FORCE: PHONY
PHONY:
.PHONY: FORCE PHONY