SDKDIR=$(HOME)/.dotnet/sdk/6.0.102# or whatever your sdk is
CSCPATH=$(SDKDIR)/Roslyn/bincore/csc.dll
NETSTANDARD=$(SDKDIR)/ref/netstandard.dll
CONFIG=$(SDKDIR)/vstest.console.runtimeconfig.json
DOTNET=DOTNET_CLI_TELEMETRY_OPTOUT=1; dotnet
CSC=$(DOTNET) $(CSCPATH) -reference:$(NETSTANDARD)
RUN=$(DOTNET) exec --runtimeconfig $(CONFIG)
CSC = mcs
RUN = mono
TERM = pdf enhanced font "Times-New-Roman, 12" size 12cm,7cm
CODE = $(filter %.cs,$^)
LIBS = $(addprefix -r:,$(filter %.dll,$^))
MKEXE = $(CSC) -target:exe -out:$@ $(LIBS) $(CODE)
MKLIB = $(CSC) -target:library -out:$@ $(LIBS) $(CODE)

default:Fig.pdf Out.noise.pdf Out.predict.pdf Out.recover.pdf Out.declip.pdf

Fig.pdf: out.data.txt Makefile
	echo '\
	set term $(TERM);\
	set out "$@";\
	set key left;\
	set xlabel "{/:Italic x}";\
	set ylabel "{/:Italic y}";\
	set title "Least-squares fit";\
	plot \
	 "$<" index 0 with errorbars title "data"\
	,"$<" index 1 with lines lt 1 title "F_c(x) = c_0+c_1x+c_2x^2"\
	,"$<" index 2 with lines lt 2 title "F_{c+Δc}(x)"\
	,"$<" index 3 with lines lt 3 title "F_{c-Δc}(x)"\
	'| tee log.gpi | gnuplot

Out.declip.pdf: out.declip.data Makefile
	echo '\
set term $(TERM);\
set out "$@";\
set xlabel "time";\
set ylabel "signal";\
plot [][:3]\
 "$<" index 0 with linespoints pt 7 ps 0.3 title "signal with clipped samples"\
,"$<" index 1 with points linecolor "red" pt 6 ps 0.8 title "clipped samples"\
,"$<" index 2 with linespoints pt 5 ps 0.3 title "declipped signal"\
,0.9 with lines linecolor "black" title "clipping threshold"\
,-0.9 with lines linecolor "black" notitle\
'|tee log.declip.gpi | gnuplot

Out.recover.pdf: out.recover.data Makefile
	echo '\
set term $(TERM);\
set out "$@";\
set xlabel "time";\
set ylabel "signal";\
plot [][:7]\
 "$<" index 0 with linespoints pt 7 ps 0.3 title "signal with missing samples"\
,"$<" index 1 with points pt 6 ps 0.9 title "missed samples"\
,"$<" index 2 with points pt 5 ps 0.3 title "recovered samples"\
,0 with lines notitle\
'|tee log.recover.gpi | gnuplot

Out.noise.pdf: out.noise.data Makefile
	echo '\
set term $(TERM);\
set out "$@";\
set xlabel "time";\
set ylabel "signal";\
plot \
 "$<" index 0 with points pt 7 ps 0.15 title "original signal"\
,"$<" index 1 with points pt 6 ps 0.3 title "signal with random noise"\
,"$<" index 2 with lines title "least squares filtered signal"\
'|gnuplot

Out.predict.pdf: out.predict.data Makefile
	echo '\
set term $(TERM);\
set out "$@";\
set xlabel "index";\
set ylabel "signal";\
set arrow from 50,-3 to 50,4 nohead lw 0.1 ;\
plot \
 "$<" index 0 with points pt 6 ps 0.5 title "signal"\
,"$<" index 1 with lines title "prediction"\
'|gnuplot

out.declip.data:main-declip.exe Makefile
	mono $< > $@

out.predict.data:main-predict.exe Makefile
	mono $< n:6 > $@

out.noise.data:main-smooth.exe
	mono $< > $@

out.data.txt:main.exe
	$(RUN) $< > $@ 2>log

main.exe: main.cs matlib.dll
	$(CSC) $< $(addprefix -reference:,$(filter %.dll,$^))
main-smooth.exe: main-smooth.cs matlib.dll
	$(CSC) $< $(addprefix -reference:,$(filter %.dll,$^))
main-predict.exe: main-predict.cs matlib.dll
	$(CSC) $< $(addprefix -reference:,$(filter %.dll,$^))
main-recover.exe: main-recover.cs matlib.dll ; $(MKEXE)
main-declip.exe: main-declip.cs matlib.dll ; $(MKEXE)
matlib.dll: \
	../matrix/matrix.cs\
	../matrix/vector.cs\
	../QR/givensqr.cs\
	../QR/gsqr.cs\
	../QR/qr.cs\
	./lsfit.cs
	$(MKLIB)

clean:
	$(RM) Out* *.dll *.exe log* out* *.pdf
