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

default: Out.graph.svg Out.gnuplot.svg

Out.graph.svg: Out.txt erf.data.txt Makefile
	>log.erf.txt
	cat erf.data.txt | while read c1 c2 c3; do \
		echo -$$c1 -$$c2 >> log.erf.txt; done
	cat erf.data.txt | while read c1 c2 c3; do \
		echo $$c1 $$c2 >> log.erf.txt; done
	graph \
		--height-of-plot 0.5 \
		--upward-shift 0.4 \
		--output-format $(subst .,,$(suffix $@)) \
		--top-label "Error function" \
		--x-label "x" \
		--y-label "erf(x)" \
		--grid-style 3 \
		"$<" \
		--line-mode 0 \
		--symbol 4 \
		"log.erf.txt" \
	>$@

Out.gnuplot.svg: Out.txt erf.data.txt Makefile
	echo '\
		set terminal $(subst .,,$(suffix $@)) background "white"; \
		set output "$@"; \
		set grid;\
		set key left;\
		set xlabel "x";\
		set ylabel "erf(x)";\
		set title "Error function";\
		plot \
			 "$<" with lines notitle \
			,"erf.data.txt" with points pointtype 6 notitle \
			,"erf.data.txt" using (-($$1)):(-($$2)) with points pointtype 6 not; \
	'| tee log.gpi | gnuplot

Out.txt: main.exe
	mono $< > $@

main.exe: main.cs sfuns.dll ; $(MKEXE)
sfuns.dll : sfuns.cs ; $(MKLIB)

clean:
	$(RM) *.exe *.dll [Oo]ut* log*
