# The source-highlight language defs and lang.map dir
HL_DIR=/usr/share/source-highlight

LOC_HL_DIR=../docs/src/source-highlight
LOC_LANG_MAP=$(LOC_HL_DIR)/local/lang.map

# Languages for which we provide our own .lang files.
# This includes overriding languages listed in HL_DIR.
EMCLANGS=$(wildcard $(LOC_HL_DIR)/*.lang)

# Grep arg to suppress user defined langs already in lang.map
GREPARG= $(patsubst %, -e %,$(EMCLANGS))

# Examples
SOURCE_HIGHLIGHT_PDF_TARGETS := $(patsubst %.adoc, %.pdf, $(wildcard $(LOC_HL_DIR)/*.txt))
SOURCE_HIGHLIGHT_HTML_TARGETS := $(patsubst %.adoc, %.html ,$(wildcard $(LOC_HL_DIR)/*.txt))

# Tests with HTML output without going through asciidoc
TEST_SRCS := hal-test.hal ini-test.ini ngc-test.ngc
TEST_TARGETS := hal-test.html ini-test.html ngc-test.html


TARGET=$(LOC_HL_DIR)/local/lang.map
ASCIIDOC_ARGS=-a source_highlight_dir=local -f emc-langs-source-highlight.conf

$(TARGET): $(EMCLANGS) $(HL_DIR)/lang.map
ifeq (,$(findstring lang.map,$(wildcard $(HL_DIR)/*)))#
	$(error $(HL_DIR)/lang.map does not exist - are you sure \
	the 'source-highlight'  package is installed?)
endif
	rm -rf $(LOC_HL_DIR)/local
	cp -r $(HL_DIR) $(LOC_HL_DIR)/local
	mv $(LOC_HL_DIR)/local/lang.map $(LOC_HL_DIR)/local/lang.map.dist
	cp $(LOC_HL_DIR)/*.lang $(LOC_HL_DIR)/local
	grep -v $(GREPARG) $(LOC_HL_DIR)/local/lang.map.dist >$(TARGET)
	for i in $(EMCLANGS); do \
             echo `basename $$i .lang` '=' `basename $$i` >>$(TARGET) ; \
        done

examples: $(TARGET) $(SOURCE_HIGHLIGHT_HTML_TARGETS) $(SOURCE_HIGHLIGHT_PDF_TARGETS) tests


tests: 	$(TARGET) $(TEST_TARGETS)


%.html:	%.ini
	source-highlight --data-dir=local --input $< --output $@

%.html:	%.ngc
	source-highlight --data-dir=local --input $< --output $@

%.html:	%.hal
	source-highlight --data-dir=local --input $< --output $@

docclean: clean-source-highlight
clean-source-highlight:
	-rm -rf local $(SOURCE_HIGHLIGHT_HTML_TARGETS) $(SOURCE_HIGHLIGHT_PDF_TARGETS) $(TEST_TARGETS)

$(SOURCE_HIGHLIGHT_PDF_TARGETS):  %.pdf: %.adoc
	a2x -v -f pdf $<

$(SOURCE_HIGHLIGHT_HTML_TARGETS): %.html: %.adoc
	asciidoc $(ASCIIDOC_ARGS) -v  $<
