# Makefile for help2man

# Copyright (C) 2012, 2014 Free Software Foundation, Inc.

# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.

# Written by Brendan O'Dea <bod@debian.org>

srcdir = @srcdir@
auxdir = @auxdir@
prefix = @prefix@
exec_prefix = @exec_prefix@

bindir = @bindir@
libdir = @libdir@
pkglibdir = $(libdir)/help2man
datarootdir = @datarootdir@
mandir = @mandir@
datadir = @datadir@
infodir = @infodir@
localedir = $(datadir)/locale

export SHELL = @SHELL@
export VPATH = .:$(srcdir)

DESTDIR =
LINGUAS = $(basename $(notdir $(wildcard $(srcdir)/po/*.po)))
LINGUAS_TEXI = $(basename $(notdir $(wildcard $(srcdir)/po-texi/*.po)))

CC = @CC@
PERL = @PERL@
LIBS = @LIBS@
FIND_VPATH = $(auxdir)/find-vpath  # locate a file in $VPATH
MKINSTALLDIRS = $(auxdir)/mkinstalldirs
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_SCRIPT = @INSTALL_SCRIPT@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_INFO = @INSTALL_INFO@
LOOKUP_LOCALE = $(auxdir)/lookup-locale  # get full locale from extension
MAKEINFO = @MAKEINFO@
MSGFMT = @MSGFMT@
XGETTEXT = @XGETTEXT@
MSGMERGE = @MSGMERGE@
PO4A_UPDATEPO = @PO4A_UPDATEPO@
PO4A_TRANSLATE = @PO4A_TRANSLATE@

# Tweaks to extracted texinfo catalog, and generated output.
FIXUP_TEXI_POT = @PERL@ $(auxdir)/fixup-texi-pot
FIXUP_TEXI_TRANS = @PERL@ $(auxdir)/fixup-texi-trans

target = help2man
preload = bindtextdomain

all: $(target) man info @extra_make_all@

install: all install_base @extra_make_install@

install_base:
	$(MKINSTALLDIRS) $(DESTDIR)$(bindir)
	$(INSTALL_SCRIPT) $(target) $(DESTDIR)$(bindir)
	$(MKINSTALLDIRS) $(DESTDIR)$(mandir)/man1
	$(INSTALL_DATA) $$($(FIND_VPATH) $(target).1) $(DESTDIR)$(mandir)/man1
	$(MKINSTALLDIRS) $(DESTDIR)$(infodir)
	$(INSTALL_DATA) $$($(FIND_VPATH) $(target).info) \
	    $(DESTDIR)$(infodir)/$(target).info

	if test -f $(DESTDIR)$(infodir)/dir; \
	then \
	    $(INSTALL_INFO) --info-dir=$(DESTDIR)$(infodir) \
		$(DESTDIR)$(infodir)/$(target).info; \
	fi

install_preload: preload
	$(MKINSTALLDIRS) $(DESTDIR)$(pkglibdir)
	$(INSTALL_PROGRAM) $(preload).so $(DESTDIR)$(pkglibdir)

install_l10n: msg_l10n man_l10n info_l10n
	set -e; \
	for lang in $(LINGUAS); \
	do \
	    $(MKINSTALLDIRS) $(DESTDIR)$(localedir)/$$lang/LC_MESSAGES; \
	    $(INSTALL_DATA) $$($(FIND_VPATH) po/$$lang.gmo) \
		$(DESTDIR)$(localedir)/$$lang/LC_MESSAGES/$(target).mo; \
	    $(MKINSTALLDIRS) $(DESTDIR)$(mandir)/$$lang/man1; \
	    $(INSTALL_DATA) $$($(FIND_VPATH) $(target).$$lang.1) \
		$(DESTDIR)$(mandir)/$$lang/man1/$(target).1; \
	done; \
	for lang in $(LINGUAS_TEXI); \
	do \
	    $(MKINSTALLDIRS) $(DESTDIR)$(infodir); \
	    $(INSTALL_DATA) $$($(FIND_VPATH) $(target)-$$lang.info) \
		$(DESTDIR)$(infodir)/$(target)-$$lang.info; \
	    if test -f $(DESTDIR)$(infodir)/dir; \
	    then \
		$(INSTALL_INFO) --info-dir=$(DESTDIR)$(infodir) \
		    $(DESTDIR)$(infodir)/$(target)-$$lang.info; \
	    fi; \
	done

uninstall:
	if test -f $(DESTDIR)$(infodir)/dir; \
	then \
	    for file in $(DESTDIR)$(infodir)/$(target).info \
		$(DESTDIR)$(infodir)/$(target)-*.info; \
		test -f $$file || continue; \
		$(INSTALL_INFO) --delete --info-dir=$(DESTDIR)$(infodir) $$file; \
	    done; \
	fi
	rm -f $(DESTDIR)$(bindir)/$(target) \
	    $(DESTDIR)$(pkglibdir)/$(preload).so \
	    $(DESTDIR)$(mandir)/man1/$(target).1 \
	    $(DESTDIR)$(mandir)/*/man1/$(target).1 \
	    $(DESTDIR)$(localedir)/*/LC_MESSAGES/$(target).mo \
	    $(DESTDIR)$(infodir)/$(target).info
	    $(DESTDIR)$(infodir)/$(target)-*.info

clean:
	-rm -f $(target) $(target).h2m $(target).*.h2m $(preload).so *.tmp*
	-rm -rf localetmp

mostlyclean: clean
	rm -f $(target).dvi $(target).aux $(target).cp $(target).cps \
	    $(target).fn $(target).ky $(target).log $(target).pg $(target).toc \
	    $(target).tp $(target).vr po/*.po~ po-texi/*.po~ po-texi/*.pot~

distclean: mostlyclean
	-rm -rf config.cache config.log config.status Makefile autom4te.cache

realclean: distclean
	rm -f $(target)-*.texi $(target).info $(target)-*.info $(target).1 \
	    $(target).*.1 po/*.gmo

maintainer-clean: realclean
	rm -f $(srcdir)/configure

$(target): $(srcdir)/$(target).PL
	$(PERL) $? @extra_extract_args@

$(target).h2m: $(srcdir)/$(target).h2m.PL
	$(PERL) $?

preload: $(preload).so
$(preload).so: $(srcdir)/$(preload).c
	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ -fPIC -shared $? $(LIBS)

man: $(target).1
$(target).1: $(srcdir)/$(target).PL $(srcdir)/$(target).h2m.PL
	$(MAKE) $(target) $(target).h2m
	./$(target) --include=$(target).h2m --output=$@ ./$(target)

msg_l10n: $(addprefix po/,$(addsuffix .gmo,$(LINGUAS)))
po/%.gmo: $(srcdir)/po/%.po
	test -d po || mkdir po
	$(MSGFMT) -o $@ $?

localetmp/%/LC_MESSAGES/$(target).mo: po/%.gmo
	lang=$(patsubst localetmp/%/LC_MESSAGES/$(target).mo,%,$@); \
	$(MKINSTALLDIRS) localetmp/$$lang/LC_MESSAGES && \
	$(INSTALL_DATA) po/$$lang.gmo localetmp/$$lang/LC_MESSAGES/$(target).mo

man_l10n: $(addprefix $(target).,$(addsuffix .1,$(LINGUAS)))
$(target).%.1: $(srcdir)/$(target).PL $(srcdir)/$(target).h2m.PL \
    $(srcdir)/po/%.po
	lang=$(patsubst $(target).%.1,%,$@); \
	$(MAKE) $(target) $(preload).so \
	    localetmp/$$lang/LC_MESSAGES/$(target).mo $(target).$$lang.h2m; \
	LD_PRELOAD=./$(preload).so LOCALEDIR=localetmp TEXTDOMAIN=help2man \
	    ./$(target) --include=$$($(FIND_VPATH) $(target).$$lang.h2m) \
		--output=$@ ./$(target)

$(target).%.h2m: $(srcdir)/$(target).h2m.PL $(srcdir)/po/%.po
	set -e; \
	lang=$(patsubst $(target).%.h2m,%,$@); \
	locale=$$($(LOOKUP_LOCALE) $$lang $(srcdir)/locales); \
	$(MAKE) localetmp/$$lang/LC_MESSAGES/$(target).mo; \
	$(PERL) $(srcdir)/$(target).h2m.PL --locale="$$locale" \
	    --message-dir=localetmp --output=$@

info: $(target).info
$(target).info: $(srcdir)/$(target).texi
	$(MAKEINFO) $? -o $@

info_l10n: $(addprefix $(target)-,$(addsuffix .info,$(LINGUAS_TEXI)))
$(target)-%.info: $(target)-%.texi
	set -e; \
	lang=$(patsubst $(target)-%.info,%,$@); \
	locale=$$($(LOOKUP_LOCALE) $$lang $(srcdir)/locales); \
	$(MAKEINFO) --output=$@.tmp --document-language="$$locale" $?
	$(FIXUP_TEXI_TRANS) -o $@ $@.tmp
	rm -f $@.tmp

$(target)-%.texi: $(srcdir)/po-texi/%.po $(srcdir)/help2man.texi
	$(PO4A_TRANSLATE) -f texinfo -m $(srcdir)/help2man.texi \
	    -p $(filter %.po,$^) -l $@ -k 60

dvi: $(target).dvi
$(target).dvi: $(srcdir)/$(target).texi
	$(TEXI2DVI) $?

Makefile: $(srcdir)/Makefile.in
	./config.status

update-po: $(srcdir)/po/$(target).pot $(srcdir)/po-texi/$(target)-texi.pot
	set -e; \
	for po in $(srcdir)/po/*.po; \
	do \
	    echo -n "Updating $$po "; \
	    $(MSGMERGE) -U $$po $(srcdir)/po/$(target).pot; \
	done; \
	for po in $(srcdir)/po-texi/*.po; \
	do \
	    echo -n "Updating $$po "; \
	    $(MSGMERGE) -U $$po $(srcdir)/po-texi/$(target)-texi.pot; \
	done

$(srcdir)/po/$(target).pot: $(srcdir)/$(target).PL $(srcdir)/$(target).h2m.PL
	$(MAKE) $(target)
	$(XGETTEXT) -L Perl -k_ -kN_ -cTranslators: \
	    --msgid-bugs-address="Brendan O'Dea <bug-help2man@gnu.org>" -o $@ \
	    $(target) $(srcdir)/$(target).h2m.PL

$(srcdir)/po-texi/$(target)-texi.pot: $(srcdir)/$(target).texi
	$(PO4A_UPDATEPO) -f texinfo \
	    --msgid-bugs-address="Brendan O'Dea <bug-help2man@gnu.org>" \
	    -p $@.tmp -m $?
	$(FIXUP_TEXI_POT) -o $@ $@.tmp
	rm -f $@.tmp

.PHONY: all clean distclean dvi info info_l10n install install_base \
    install_l10n install_preload maintainer-clean man man_l10n mostlyclean \
    msg_l10n preload realclean uninstall update-po

.SUFFIXES:
