
BUILD_USAGE_WITH=w3m

INSTALL=install -c

STX2ANY=./scripts/stx2any
PREFIX=/usr
BASE=$(DESTDIR)$(PREFIX)
EXEDIR=$(BASE)/bin
LIBDIR=$(BASE)/share/stx2any
MANDIR=$(BASE)/share/man/man1
MSGDIR=$(LIBDIR)/messages
EMACSDIR=$(BASE)/share/emacs/site-lisp
SCRIPTS=stx2any strip_stx gather_stx_titles html2stx extract_usage_from_stx
MANPAGES:=$(SCRIPTS:=.1)
MESSAGES:=$(SCRIPTS:=.usage) version.msg
INST_SCRIPTS=$(SCRIPTS:=.inst)
DOCDIR=$(BASE)/share/doc/stx2any
DOCBASE:=stx2any Stx-doc Stx-ref strip_stx gather_stx_titles html2stx \
	accreditions extract_usage_from_stx
DOCBASE:=$(DOCBASE:%=examples/%)
DOCS:=$(DOCBASE:=.html) $(MANPAGES) examples/README.html
FORMATS=html man latex docbook-xml
COMMON=common.m4 markup.m4 begin.m4 end.m4 cleanup.m4 secure.m4 templ.m4 \
       emphasis.sed inline.sed block.sed quote.sed quote_us.sed wrapcalls.sed \
       gatherlinkmap.sed linking.sed quote_quotes.sed header.sed
STXDEFS:=$(FORMATS:=/make.m4) $(FORMATS:=/templ.m4) $(FORMATS:=/settings.sh) \
	$(COMMON:%=common/%)

.SUFFIXES:

all: chmod doc $(MESSAGES) $(INST_SCRIPTS) $(STXDEFS)

chmod:
	chmod +x scripts/* regression/run-tests

html2stx.inst: scripts/html2stx
	cp $< $@

%.inst: scripts/%
	sed -e "s#^BASE=.*#BASE="$(LIBDIR)"#" $< >$@

%.1: examples/%.txt $(STXDEFS)
	sed -e "s#PREFIX#$(PREFIX)#" $< | $(STX2ANY) -T man >$@

%.usage: examples/%.txt $(STXDEFS)
ifeq ($(BUILD_USAGE_WITH),w3m)
	./scripts/extract_usage_from_stx $< | $(STX2ANY) -T text >$@
else
	./scripts/extract_usage_from_stx $< >$@
endif

version.msg: debian/changelog
	sed -e 's#^\([^(]*\)(\([^)]*\)).*#\1\2#' -e '1q' $< >$@

%.m4: %.lm4 
	./scripts/strip_stx $< >$@

%.sh: %.lsh 
	./scripts/strip_stx $< >$@

%.sed: %.lsed
	./scripts/strip_stx $< >$@

test: chmod $(STXDEFS)
	./regression/run-tests

doc: chmod $(DOCS)

install: all
	$(INSTALL) -m 755 -d $(EXEDIR) $(LIBDIR) $(MSGDIR) $(MANDIR) $(DOCDIR) $(EMACSDIR)
	for i in $(INST_SCRIPTS); do \
		$(INSTALL) -m 755 $$i $(EXEDIR)/`basename $$i .inst`; \
	done
	$(INSTALL) -m 644 $(MANPAGES) $(MANDIR)
	$(INSTALL) -m 644 $(DOCS) $(DOCBASE:=.txt) $(MISC) $(DOCDIR)
	$(INSTALL) -m 644 stx-mode.el $(EMACSDIR)
	for i in $(FORMATS); do \
	  $(INSTALL) -m 755 -d $(LIBDIR)/$$i; \
	  $(INSTALL) -m 644 $$i/*.m4 $$i/*.sh $(LIBDIR)/$$i; \
	done
	$(INSTALL) -m 755 -d $(LIBDIR)/common
	$(INSTALL) -m 644 common/*.m4 common/*.sed $(LIBDIR)/common
	$(INSTALL) -m 644 $(MESSAGES) $(MSGDIR)

titles.m4: $(DOCBASE:=.txt) common/header.sed
	./scripts/gather_stx_titles -p examples -f txt -t html $^ > $@

examples/README.html: README titles.m4 $(STXDEFS)
	$(STX2ANY) --link-abbrevs titles.m4 $< > $@

examples/Stx-doc.html: examples/Stx-doc.txt titles.m4 $(STXDEFS)
	$(STX2ANY) --link-abbrevs titles.m4 $< > $@

examples/Stx-ref.html: examples/Stx-ref.txt titles.m4 $(STXDEFS)
	$(STX2ANY) --numbering on titles.m4 $< > $@

%.html: %.txt titles.m4 $(STXDEFS)
	$(STX2ANY) titles.m4 $< > $@

clean: gc clean-leavedocs
	rm -f $(DOCS)

gc:
	rm -f *.dvi *.toc *.log *.aux

clean-leavedocs:
	rm -f $(STXDEFS) $(INST_SCRIPTS) $(MESSAGES) titles.m4

.PHONY: clean gc clean-leavedocs all doc install test chmod

