#
# Make file to install/uninstall AsciiDoc
#

.NOTPARALLEL:

INSTALL = @INSTALL@
INSTALL_PROG = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
PACKAGE_TARNAME = @PACKAGE_TARNAME@
SED = @SED@

prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
datadir = @datadir@
docdir = @docdir@
sysconfdir = @sysconfdir@
datarootdir = @datarootdir@
mandir=@mandir@
srcdir = @srcdir@

VPATH = @srcdir@

ASCIIDOCCONF = $(sysconfdir)/asciidoc

prog = asciidoc.py a2x
progdir = $(bindir)

vimdir = @sysconfdir@/vim

manp = $(wildcard doc/*.1)
manpdir = $(mandir)/man1

conf = $(wildcard *.conf)
confdir = $(ASCIIDOCCONF)

filtersdir = $(ASCIIDOCCONF)/filters

codefilter = filters/code/code-filter.py
codefilterdir = $(filtersdir)/code
codefilterconf = filters/code/code-filter.conf
codefilterconfdir = $(filtersdir)/code

graphvizfilter = filters/graphviz/graphviz2png.py
graphvizfilterdir = $(filtersdir)/graphviz
graphvizfilterconf = filters/graphviz/graphviz-filter.conf
graphvizfilterconfdir = $(filtersdir)/graphviz

musicfilter = filters/music/music2png.py
musicfilterdir = $(filtersdir)/music
musicfilterconf = filters/music/music-filter.conf
musicfilterconfdir = $(filtersdir)/music

sourcefilterconf = filters/source/source-highlight-filter.conf
sourcefilterconfdir = $(filtersdir)/source

docbook = $(wildcard docbook-xsl/*.xsl)
docbookdir = $(ASCIIDOCCONF)/docbook-xsl

dblatex = $(wildcard dblatex/*.xsl) $(wildcard dblatex/*.sty)
dblatexdir = $(ASCIIDOCCONF)/dblatex

css = $(wildcard stylesheets/*.css)
cssdir = $(ASCIIDOCCONF)/stylesheets

js = $(wildcard javascripts/*.js)
jsdir = $(ASCIIDOCCONF)/javascripts

callouts = $(wildcard images/icons/callouts/*)
calloutsdir = $(ASCIIDOCCONF)/images/icons/callouts

icons = $(wildcard images/icons/*.png) images/icons/README
iconsdir = $(ASCIIDOCCONF)/images/icons

doc = $(wildcard README*) $(wildcard BUGS*) $(wildcard INSTALL*) $(wildcard CHANGELOG*)

DATATARGETS = manp conf docbook dblatex css js callouts icons codefilterconf musicfilterconf sourcefilterconf graphvizfilterconf
PROGTARGETS = prog codefilter musicfilter graphvizfilter
TARGETS = $(DATATARGETS) $(PROGTARGETS) doc

INSTDIRS = $(TARGETS:%=%dir)

.PHONY: $(TARGETS)

all: build

# create directories used during the install
$(INSTDIRS):
	$(INSTALL) -d $(DESTDIR)/$($@)

$(PROGTARGETS): % : %dir
	$(INSTALL_PROG) $($@) $(DESTDIR)/$($<)/

$(DATATARGETS): % : %dir
	$(INSTALL_DATA) $($@) $(DESTDIR)/$($<)/

docs:
	$(INSTALL) -d $(DESTDIR)/$(docdir)
	$(INSTALL_DATA) $(doc) $(DESTDIR)/$(docdir)
	$(INSTALL) -d $(DESTDIR)/$(docdir)/docbook-xsl
	$(INSTALL_DATA) docbook-xsl/asciidoc-docbook-xsl.txt $(DESTDIR)/$(docdir)/docbook-xsl
	$(INSTALL) -d $(DESTDIR)/$(docdir)/dblatex
	$(INSTALL_DATA) dblatex/dblatex-readme.txt $(DESTDIR)/$(docdir)/dblatex
	$(INSTALL) -d $(DESTDIR)/$(docdir)/stylesheets
	$(INSTALL_DATA) $(css) $(DESTDIR)/$(docdir)/stylesheets
	$(INSTALL) -d $(DESTDIR)/$(docdir)/javascripts
	$(INSTALL_DATA) $(js) $(DESTDIR)/$(docdir)/javascripts
	$(INSTALL) -d $(DESTDIR)/$(docdir)/images
	( cd images && \
		cp -R * $(DESTDIR)/$(docdir)/images )
	$(INSTALL) -d $(DESTDIR)/$(docdir)/doc
	( cd doc && \
		cp -R * $(DESTDIR)/$(docdir)/doc )
	$(INSTALL) -d $(DESTDIR)/$(docdir)/examples/website
	( cd examples/website && \
		cp -R * $(DESTDIR)/$(docdir)/examples/website )

progsymlink:
	(cd $(DESTDIR)/$(progdir); ln -sf asciidoc.py asciidoc)

fixconfpath:
	@for f in $(prog); do \
		echo "Fixing CONF_DIR in $$f"; \
		$(SED) "s#^CONF_DIR = '.*'#CONF_DIR = '$(ASCIIDOCCONF)'#; s#^CONF_DIR=.*#CONF_DIR=$(ASCIIDOCCONF)#" $$f > $$f.out; \
		mv $$f.out $$f; \
	done

install-vim:
	@for d in $(DESTDIR)/$(vimdir) /etc/vim; do \
		if ! test -d $$d; then continue; fi ; \
		echo "installing Vim files in $$d" ; \
		$(INSTALL) -d $$d/syntax ; \
		$(INSTALL_DATA) vim/syntax/asciidoc.vim $$d/syntax ; \
		$(INSTALL) -d $$d/ftdetect ; \
		$(INSTALL_DATA) vim/ftdetect/asciidoc_filetype.vim $$d/ftdetect ; \
	done

uninstall-vim:
	@for d in $(DESTDIR)/$(vimdir) /etc/vim; do \
		if ! test -d $$d; then continue; fi ; \
		echo "uninstalling Vim files in $$d" ; \
		rm -f $$d/syntax/asciidoc.vim ; \
		rm -f $$d/ftdetect/asciidoc_filetype.vim ; \
	done


build: fixconfpath

install: all $(PROGTARGETS) $(DATATARGETS) progsymlink install-vim

uninstall: uninstall-vim
	rm -f $(DESTDIR)/$(progdir)/asciidoc
	rm -f $(DESTDIR)/$(progdir)/asciidoc.py
	rm -f $(DESTDIR)/$(progdir)/a2x
	rm -f $(DESTDIR)/$(mandir)/asciidoc.1
	rm -f $(DESTDIR)/$(mandir)/a2x.1
	rm -rf $(DESTDIR)/$(confdir)
	rm -rf $(DESTDIR)/$(docdir)

test:
	@echo "Nothing to see here...Move along."
