HAXE	= /usr/bin/haxe
HAXELIB	= /usr/bin/haxelib
TEMPLATE	= $(CURDIR)/template/template.hxml
TEMPLATE_STRING	= "@BASENAME@"

SRCS_WM	= $(wildcard *.hx)
SRCS_TL	= $(wildcard tinylib/*.hx)
SRCS_TL_PR	= $(wildcard tinylib/primitives/*.hx)
SRCS_TL_SH	= $(wildcard tinylib/shaders/*.hx)
SRCS_WU	= $(wildcard wmutil/*.hx)
SRCS_PDB	= $(wildcard pdb/*.hx)
SRCS	= $(SRCS_WM) $(SRCS_TL) $(SRCS_TL_PR) $(SRCS_TL_SH) \
				$(SRCS_WU) $(SRCS_PDB)

SAMPLE_DIR	= ../samples
SAMPLES_XML	= $(wildcard $(SAMPLE_DIR)/*.xml)
SAMPLES_SWF = $(foreach file, $(SAMPLES_XML), $(file:.xml=.swf) )
PDBSAMPLE_DIR	= ../samples_pdb
PDBSAMPLES_PDB	= $(wildcard $(PDBSAMPLE_DIR)/*.pdb)
PDBSAMPLES_SWF = $(foreach file, $(PDBSAMPLES_PDB), $(file:.pdb=.swf) )

## DOX related
DOX_INPUT	= xml
DOX_OUTPUT	= api
DOX_INCLUDES	= $(foreach file, $(wildcard $(SRCS_WM)),-in $(basename $(notdir $(file)))) \
								-in tinylib -in pdb -in wmutil

##########################################
all:
	@echo "usage: make (target swf file name)"
	@echo "       (target file without suffix).xml is required to build swf file"
%.swf : %.hxml %.xml $(SRCS)
	$(HAXE) $<
%.hxml : %.xml
	@sed -e s/$(TEMPLATE_STRING)/`echo $< | sed -e "s/\\//\\\\\\\\\//g" -e "s/.xml$$//"`/ $(TEMPLATE) > $@; \
	PNGFILE=`echo $< | sed -e "s/.xml$$/.png/"`; \
	JPGFILE=`echo $< | sed -e "s/.xml$$/.jpg/"`; \
	if [ -f $$PNGFILE ]; then \
	  echo "-resource $$PNGFILE@image" >> $@; \
	elif [ -f $$JPGFILE ]; then \
	  echo "-resource $$JPGFILE@image" >> $@; \
	fi
samples: $(SAMPLES_SWF) $(PDBSAMPLES_SWF)
$(PDBSAMPLE_DIR)/%.swf: $(PDBSAMPLE_DIR)/%.hxml $(PDBSAMPLE_DIR)/%.pdb $(SRCS)
	$(HAXE) $<
clean-samples:
	rm -f $(SAMPLE_DIR)/*.swf
	rm -f $(PDBSAMPLE_DIR)/*.swf
dox:
	mkdir -p $(DOX_INPUT)
	$(HAXE) dox.hxml
	$(HAXELIB) run dox -r $(CURDIR)/$(DOX_OUTPUT) -i $(DOX_INPUT) -o $(DOX_OUTPUT) $(DOX_INCLUDES)
