CFLAGS = -O -Wall -std=gnu11
#CFLAGS += `gsl-config --cflags`
CFLAGS += $(shell gsl-config --cflags)
LDLIBS = $(shell gsl-config --libs)

.PHONEY:default
default: erf.graph.png erf.pyxplot.png erf.gnuplot.png

erf.graph.png: out.data.txt Makefile input.data.txt
	awk '{print $$1, $$2}' $< > line1
	awk '{print $$1, $$3}' $< > line2
	awk '{print $$1, $$4}' $< > line3
	awk '{print $$1, $$2}' input.data.txt > line4
	graph \
	--output-format png --bitmap-size 1024x768 \
	--x-label "x" --y-label "y" \
	--top-label "Error function" \
	--line-mode 1 line1 \
	--line-mode 2 line2 \
	--line-mode 3 line3 \
	--symbol 4 line4 \
	> $@
	$(RM) line*

erf.gnuplot.png: tmp.gpi
	cat $< | sed 's/pyxplot/gnuplot/' | gnuplot

erf.pyxplot.png: out.data.txt Makefile input.data.txt
	echo '\
	set terminal png;\
	set output "$@";\
	set key top left;\
	set tics out;\
	set xlabel "x";\
	set ylabel "y";\
	set title "Error function";\
	plot \
	 "$<" using 1:2 with line title "erf from math.h"\
	,"$<" using 1:3 with line title "gsl-sf-erf"\
	,"$<" using 1:4 with line title "myerf"\
	,"input.data.txt" using 1:2 with points pointtype 2 title "tabulated data"\
	' | tee tmp.gpi | pyxplot

tmp.gpi:erf.pyxplot.png
	touch $@

out.data.txt: main
	./$< > $@

main: main.o myerf.o
main.o myerf.o: myerf.h

.PHONEY:clean
clean:
	$(RM) *.o main *.png out.data.txt tmp.gpi

.PHONEY:test
test:
	echo $(CFLAGS)
	echo $(LDLIBS)
