DLLS = $(addprefix -reference:,$(filter %.dll,$^))
CODE = $(filter %.cs,$^)
MKEXE = mcs -target:exe -out:$@ $(DLLS) $(CODE)
MKDLL = mcs -target:library -out:$@ $(DLLS) $(CODE)

Higgs.svg: plot.data Makefile
	gnuplot -e "\
	set term $(subst .,,$(suffix $@)) background rgb 'white';\
	set title 'Higgs production signal';\
	set xlabel 'Energy [GeV]';\
	set ylabel 'Signal [arb.u.]';\
	plot 'higgs.data' using 1:2:3 with errorbars title 'experiment'\
	, '$<' using 1:2 with lines title 'Breit-Wigner fit' ;\
	" > $@

plot.data:main.exe
	mono $< <higgs.data 2>Out.txt 1>$@

main.exe: main.cs qnewton.dll matlib.dll
	$(MKEXE)

LIBDIR=../../matlib
LIBSRC=\
	$(LIBDIR)/matrix/vector.cs\
	$(LIBDIR)/matrix/matrix.cs
matlib.dll: $(LIBSRC)
	$(MKDLL)

qnewton.dll: \
	$(LIBDIR)/minim/qnewton.cs \
	$(LIBDIR)/minim/simplex.cs \
	$(LIBDIR)/optim/swarm.cs \
	$(LIBDIR)/optim/rndstart.cs \
	$(LIBDIR)/optim/prernd.cs \
	$(LIBDIR)/optim/bbpso.cs \
	matlib.dll
	$(MKDLL)

clean:
	$(RM) *.exe *.dll [Oo]ut* [Ll]og* *.svg

comma:=,
empty:=
space:=$(empty) $(empty)
clist = $(subst $(space),$(comma),$(1))
