DOCDIR=../dist/doc

PANDOC=pandoc
PDFLATEX=pdflatex


# List text files in the order in which you want them to appear in the
# complete manual:
SOURCES=README.mdwn INSTALL.mdwn themes.mdwn hacking.mdwn TODO.mdwn modules.mdwn scripts.mdwn faq.mdwn COPYING.mdwn colophon.mdwn
OTHER=
PYTHON_SOURCES:=$(shell find .. -name \*.py)
TODO_SOURCES=$(patsubst TODO.mdwn,,$(SOURCES)) $(PYTHON_SOURCES)
MAN_SOURCES=$(patsubst COPYING.mdwn,copyright_notice00,$(SOURCES))

NEWLINE_SOURCES=$(patsubst %,% oneline.txt,$(SOURCES))
NEWLINE_MAN_SOURCES=$(patsubst %,% oneline.txt,$(MAN_SOURCES))

HTML=plinth.html $(patsubst %.mdwn,%.html,$(SOURCES))
HTML_PART=$(patsubst %.html,%.part.html,$(HTML))
LATEX=plinth.tex $(patsubst %.mdwn,%.tex,$(SOURCES))
PDF=plinth.pdf $(patsubst %.mdwn,%.pdf,$(SOURCES))
MAN=plinth.1

## If you want PDF or Latex files, do them separately.  They're not made by default.

OUTPUTS=$(HTML) $(MAN) $(HTML_PART)
DIST_OUTPUT=$(patsubst %,$(DOCDIR)/%,$(OUTPUTS))

# Yes, do it twice. TODO created during the process requires a second run
default:
	make all
all: oneline.txt $(OUTPUTS) Makefile

$(DOCDIR)/%: %
	cp $< $@
dist: $(DIST_OUTPUT)
###############################################################################
oneline.txt: Makefile
	perl -e 'print "\n"' > oneline.txt

$(SOURCES):
	@rm -f $@
	if [ -f ../$(patsubst %.mdwn,%.md,$@) ]; then \
		ln -s ../$(patsubst %.mdwn,%.md,$@) $@; \
	else \
		ln -s ../$(patsubst %.mdwn,%,$@) $@; \
	fi

../TODO : $(TODO_SOURCES) Makefile
	grep -ro --exclude=.git* --exclude=plinth.1 --exclude=*.tex --exclude=*.html \
	     --exclude=README.mdwn --exclude=INSTALL.mdwn --exclude-dir=build \
	     --exclude=TODO.mdwn --exclude=COPYING.mdwn --exclude-dir=debian \
	     "TODO\:.*" ../* 2>/dev/null | \
	     sed -e "s/TODO\://g" | \
	     sed -e "s/^..\//* /g" | \
	     sed -e 's/"""$$//g' | \
	     sed -e 's/<\/p>$$//g' \
	     > ../TODO
###############################################################################
##
## MAN PAGES
##
$(MAN): $(SOURCES) ../TODO
	@csplit -s -f copyright_notice COPYING.mdwn '/##/'
	cat $(NEWLINE_MAN_SOURCES) | perl -pe 'BEGIN { $$/=undef } $$_ =~ s/\n\n#\s.*/\n/gm; $$_ =~ s/\n\n#/\n\n/gm; $$_ =~ s/(\n\n#\s.*)/uc($$1)/gme' > .make_man
	$(PANDOC) -s -t man -o $@ .make_man
	@rm -f copyright_notice0? .make_man
manpages: $(MAN)
###############################################################################
##
## LaTeX
##
%.tex: %.mdwn
	$(PANDOC) -s --toc -f markdown --standalone -o $@ $<

hacking.tex: hacking.mdwn ../TODO
	$(PANDOC) -s --toc -f markdown -o $@ hacking.mdwn ../TODO

plinth.tex: $(NEWLINE_SOURCES) ../TODO
	$(PANDOC) -s --toc -f markdown -o $@ $(NEWLINE_SOURCES)

latex: $(LATEX)
###############################################################################
##
## HTML
##


# This gets us the html sections complete with TOC, but without the
# HTML and head section boilerplate.  /help/page uses the parts.
%.part.html: %.html
	csplit -s -f $@ $< '%.*<body>%'
	sed '1d' $@00 > $@01
	csplit -s -f $@ $@01 '/<\/body>/'
	mv $@00 $@
	rm $@01
%.html: %.mdwn Makefile
	$(PANDOC) -s --toc -f markdown -o $@ $<

hacking.html: hacking.mdwn ../TODO Makefile
	$(PANDOC) -s --toc -o $@ -f markdown hacking.mdwn ../TODO

#plinth.html: $(NEWLINE_SOURCES) ../TODO style.css Makefile
#	$(PANDOC) -s --toc -c style.css -o $@ -f markdown $(NEWLINE_SOURCES)

plinth.html: $(NEWLINE_SOURCES) ../TODO Makefile
	@csplit -s -f copyright_notice COPYING.mdwn '/##/'
	$(PANDOC) -s --toc -o $@ -f markdown $(NEWLINE_MAN_SOURCES)
	@rm -f copyright_notice0? .make_man

html: $(HTML) $(HTML_PART)
###############################################################################
%.pdf: %.tex
	$(PDFLATEX) -interaction=batchmode $< >/dev/null
	$(PDFLATEX) -interaction=batchmode $< >/dev/null  # yes, do it twice so the toc works

pdf: $(PDF)
###############################################################################

clean-latex:
	rm -f *.log *.out *.aux *.toc *.tex

clean: clean-latex
	rm -f $(OUTPUTS) README.mdwn INSTALL.mdwn TODO.mdwn COPYING.mdwn \
	     copyright_notice0? \#*\# ../TODO oneline.txt *.pdf
