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

SRCS	= $(wildcard *.hx) \
				$(wildcard tinylib/*.hx) \
				$(wildcard pdb/*.hx) \
				$(wildcard tinylib/shaders/*.hx) \
				$(wildcard tinylib/primitives/*.hx)

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) )
##########################################
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
