###--------------------------------------------
### DEST : directory where to put binaries
### ARCH : faust architecture file

system	:= $(shell uname -s)
dspsrc  := $(wildcard *.dsp)
cppsrc  := $(addprefix $(DEST), $(dspsrc:.dsp=.cpp))


### check what type of applications to build (MacOSX Darwin or Linux)
ifeq ($(system), Darwin)
appls	:= $(addprefix $(DEST),  $(dspsrc:.dsp=.app))
SPEC	:= -spec macx-g++
else
appls	:= $(addprefix $(DEST),  $(dspsrc:.dsp=))
SPEC	:= 
endif
### --------------------------------------------


### allocate a unique directory
TDR := $(shell mktemp -d -t FAUST.XXXXXX)
TMP = $(TDR)/$(<:.dsp=)
### --------------------------------------------


all : $(appls)


### Darwin 
$(DEST)%.app : %.dsp
	install -d $(TMP)
	faust -a $(ARCH) $(VEC) $< -o $(TMP)/$<.cpp
	cd $(TMP); qmake -project "$(DEFS)" "INCLUDEPATH+=/usr/local/lib/faust/" "INCLUDEPATH+=/usr/local/lib/faust/osclib" "LIBS+=$(LIB)" "HEADERS+=/usr/local/lib/faust/gui/faustqt.h" 
	cd $(TMP); qmake $(SPEC)
	make -C $(TMP)
	rm -rf $@
	mv $(TMP)/$(<:.dsp=.app) $(DEST)
	rm -rf $(TDR)


### Linux
hdir1 := $(wildcard /usr/local/lib/faust/gui/faustqt.h)
hdir2 := $(wildcard /usr/share/faust/faustqt.h)
hdir3 := $(wildcard /usr/local/lib/faust/faustqt.h)
hdir23 := $(if $(hdir2),$(dir $(hdir2)),$(dir $(hdir3)))
hdir := $(if $(hdir1),$(dir $(hdir1)),$(hdir23))
qm4 := $(shell which qmake-qt4)
qm := $(if $(qm4),$(qm4),qmake)

$(DEST)% : %.dsp
	rm -rf $(TMP)
	install -d $(TMP)
	faust -a $(ARCH) $(VEC) $< -o $(TMP)/$<.cpp
	cd $(TMP); $(qm) -project "$(DEFS)" "INCLUDEPATH+=/usr/local/lib/faust/" "INCLUDEPATH+=/usr/local/lib/faust/osclib" "LIBS+=$(LIB)" "HEADERS+=$(hdir)faustqt.h" 
	cd $(TMP); $(qm) $(SPEC)
	make -C $(TMP)
	mv $(TMP)/$(<:.dsp=) $@
	rm -rf $(TMP)

clean:
	rm -rf $(DEST)