TOPDIR=..
include $(TOPDIR)/config.mk

SOURCES=index.html about.txt pixmap.txt loaders.txt filters.txt \
	basic_types.txt gfx.txt backends.txt gamma.txt grabbers.txt \
	environment_variables.txt debug.txt core.txt input.txt \
	gen.txt pixels.txt coordinate_system.txt coding_style.txt \
	get_put_pixel.txt blits.txt progress_callback.txt text.txt \
	event_queue.txt compilation.txt filters_resize.txt tasks.txt\
	filters_dithering.txt filters_python.txt spiv.txt core_common.txt \
	convert.txt news_1_0_0-rc1.txt loaders_io.txt signatures.txt widgets.txt \
	packages.txt bbox.txt utils.txt timers.txt priority_heap.txt backends_api.txt \
	backends_init.txt backends_display.txt

SOURCES+=backends_gpio.txt backends_display_waveshare_7_5_v2.txt backends_display_waveshare_3_7.txt \
	 backends_display_weact.txt

SOURCES+=hash_table.txt linked_list.txt vector.txt matrix.txt block_alloc.txt utf.txt markup.txt

SOURCES+=core_python.txt gfx_python.txt loaders_python.txt backends_python.txt

SOURCES+=widget_grid.txt widget_table.txt widget_button.txt widget_label.txt widgets_tattr.txt\
	 widget_checkbox.txt widget_choice.txt widgets_events.txt widget_tbox.txt widgets_json.txt\
	 widgets_alignment.txt widget_tabs.txt widget_frame.txt widget_pixmap.txt \
	 widgets_render_context.txt widgets_free.txt widget_pbar.txt widgets_dialog.txt \
	 dialog_msg.txt widget_classes.txt widgets_color_scheme.txt widgets_colors.txt \
	 widgets_disable.txt widget_markup.txt widget_hbox_vbox.txt widgets_timer.txt \
	 widgets_app_info.txt

EXAMPLE_SOURCES=$(wildcard example_*.txt)

ASCIIDOC_PARAMS=--conf-file asciidoc.conf

#
# Names of generated images for cleanup
#
GENIMAGES=discrete_linear_1D_convolution_alg1 discrete_linear_1D_convolution_alg2 \
	  discrete_linear_convolution discrete_linear_convolution_alg1 \
	  discrete_linear_convolution_alg2 laplacian_edge_sharpening laplacian_kernel

PAGES=$(subst .txt,.html,$(SOURCES))
PAGES+=$(subst .txt,.html,$(EXAMPLE_SOURCES))
PAGES+=examples.html

.PHONY: toolcheck clean doxygen

all: toolcheck doxygen examples.html $(PAGES)

$(PAGES): asciidoc.conf

#
# Dependencies
#
IMG_INCLUDES=images/blur/images.txt images/median/images.txt\
             images/edge_sharpening/images.txt images/gaussian_noise/images.txt

filters_python.html: $(IMG_INCLUDES)
filters.html: $(IMG_INCLUDES)

#
# Check if we have all the needed tools
#
toolcheck:
	@if [ -z `which asciidoc 2>/dev/null` ] ; then \
		echo "ERROR: Could not find 'asciidoc'" ; exit 127 ; fi
	@if [ -z `which source-highlight 2>/dev/null` ] ; then \
		echo "ERROR: Could not find 'source-highlight'" ; exit 127 ; fi
	@if [ ! -d "/usr/share/graphviz" ] ; then \
		echo "ERROR: Could not find 'graphviz'" ; exit 127 ; fi
	@if [ -z `which latex 2>/dev/null` ] ; then \
		echo "error: could not find 'latex'" ; exit 127 ; fi
	@if [ -z `which dvipng 2>/dev/null` ] ; then \
		echo "error: could not find 'dvipng'" ; exit 127 ; fi
	@if [ -z `which doxygen 2>/dev/null` ] ; then \
		echo "error: could not find 'doxygen'" ; exit 127 ; fi

doxygen:
	$(MAKE) -C ../include/
	doxygen


#
# Create page for each part of API description
#
$(PAGES): %.html: %.txt
	asciidoc $(ASCIIDOC_PARAMS) $<

#
# Autogenerate examples page
#
examples.txt: $(EXAMPLE_SOURCES) gen_examples_page.sh
	./gen_examples_page.sh examples.txt

#
# Clean up generated images
#
CLEAN+=$(patsubst %,%.md5,$(GENIMAGES))
CLEAN+=$(patsubst %,%.png,$(GENIMAGES))
CLEAN+=examples.txt

#
# Clean up generated pages
#
CLEAN+=$(PAGES)

clean:
	rm -f $(CLEAN)
	rm -rf ./api-docs

INSTALL_DOC=$(PAGES) *.png *.svg asciidoc.css images/*/*.png
install: $(INSTALL_DOC)

include $(TOPDIR)/install.mk
