#
# makefile
#
# Written by Taiji Yamada <taiji@aihara.co.jp>
#
# usage:
#	% make
#	% make all clean
#	% make all distclean
#
CXXFLAGS=-O3 -W -Wall -std=c++17
EXES=\
bifurcation-aihara_chaotic_neuron_map	\
bifurcation-henon_map	\

ALL=$(EXES)	\
$(EXES:%=v%.dat) $(EXES:%=v%.eps) $(EXES:%=v%.pdf) $(EXES:%=v%.svg)	\
$(EXES:%=p%.dat) $(EXES:%=p%.eps) $(EXES:%=p%.pdf) $(EXES:%=p%.svg)	\
$(EXES:%=bv%.dat) $(EXES:%=bv%.eps) $(EXES:%=bv%.pdf) $(EXES:%=bv%.svg)	\
$(EXES:%=bp%.dat) $(EXES:%=bp%.eps) $(EXES:%=bp%.pdf) $(EXES:%=bp%.svg)	\

all: $(ALL)

v%.dat: % makefile
	./$< -t 100 -i 500 -b 500 | dd of=$@ 2> /dev/null

bv%.dat: % makefile
	./$< -t 100 -i 500 -b 500 -B | dd of=$@ 2> /dev/null

p%.dat: % makefile
	./$< -t 100 -i 2000 -b 2000 -r 2000 -P | dd of=$@ 2> /dev/null

bp%.dat: % makefile
	./$< -t 100 -i 2000 -b 2000 -r 2000 -P -B | dd of=$@ 2> /dev/null

XRANGE=[0:1]
YRANGE=[-1:1]

vbifurcation-henon_map.% pbifurcation-henon_map.% bvbifurcation-henon_map.% bpbifurcation-henon_map.%: XRANGE=[1:1.4]
vbifurcation-henon_map.% pbifurcation-henon_map.% bvbifurcation-henon_map.% bpbifurcation-henon_map.%: YRANGE=[-1.4:1.4]

v%.eps p%.eps bv%.eps bp%.eps: SERIF=Times-Roman
v%.eps p%.eps bv%.eps bp%.eps: SLANT=Times-Italic

v%.pdf p%.pdf bv%.pdf bp%.pdf: SERIF="Times New Roman"
v%.pdf p%.pdf bv%.pdf bp%.pdf: SLANT=:Italic

v%.svg p%.svg bv%.svg bp%.svg: SERIF="Times New Roman"
v%.svg p%.svg bv%.svg bp%.svg: SLANT=:Italic

#
define VPLOT_EPS
set xlabel '{/$(SERIF) parameter {/$(SLANT) a}}';
set ylabel '{/$(SERIF) state value {/$(SLANT) x}}';
set size square;
set xrange $(XRANGE);
set yrange $(YRANGE);
set mono;
if (!bin_flag) {
  plot input with dots notitle;
}
else {
  plot input binary format="%double%double" with dots notitle;
}
endef
export VPLOT_EPS

v%.eps: v%.dat
	GNUTERM='postscript eps enhanced level3 fontscale 1' gnuplot -e "set output '$@'; input='$<'; bin_flag=0; $$VPLOT_EPS"
#	GNUTERM='epscairo enhanced fontscale 1' gnuplot -e "set output '$@'; input='$<'; bin_flag=0; $$VPLOT_EPS"
	perl -pe "s/\/Pnt {.*} def/\/Pnt {stroke gsave 0 setlinecap 10 setlinewidth M 10 0 V stroke grestore} def/" -i '$@'

bv%.eps: bv%.dat
	GNUTERM='postscript eps enhanced level3 fontscale 1' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$VPLOT_EPS"
#	GNUTERM='epscairo enhanced fontscale 1' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$VPLOT_EPS;"
	perl -pe "s/\/Pnt {.*} def/\/Pnt {stroke gsave 0 setlinecap 10 setlinewidth M 10 0 V stroke grestore} def/" -i '$@'

#
define VPLOT_PDF
set xlabel '{/"Times New Roman" parameter {/$(SLANT) a}}';
set ylabel '{/"Times New Roman" state value {/$(SLANT) x}}';
set size square;
set xrange $(XRANGE);
set yrange $(YRANGE);
set mono;
if (!bin_flag) {
  plot input with dots notitle;
}
else {
  plot input binary format="%double%double" with dots notitle;
}
endef
export VPLOT_PDF

v%.pdf: v%.dat
	GNUTERM='pdf enhanced fontscale .425' gnuplot -e "set output '$@'; input='$<'; bin_flag=0; $$VPLOT_PDF"
#	GNUTERM='pdfcairo enhanced fontscale 1' gnuplot -e "set output '$@'; input='$<'; bin_flag=0; $$VPLOT_PDF"

bv%.pdf: bv%.dat
	GNUTERM='pdf enhanced fontscale .425' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$VPLOT_PDF"
#	GNUTERM='pdfcairo enhanced fontscale 1' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$VPLOT_PDF"

#
define VPLOT_SVG
set xlabel '{/$(SERIF) parameter {/$(SLANT) a}}';
set ylabel '{/$(SERIF) state value {/$(SLANT) x}}';
set size square;
set xrange $(XRANGE);
set yrange $(YRANGE);
set mono;
if (!bin_flag) {
  plot input with dots notitle;
}
else {
  plot input binary format="%double%double" with dots notitle;
}
endef
export VPLOT_SVG

v%.svg: v%.dat
	GNUTERM='svg enhanced fontscale 1' gnuplot -e "set output '$@'; input='$<'; bin_flag=0; $$VPLOT_SVG"
#	perl -pe "s/<circle id='gpDot' (.*)\/>/<circle id='gpDot' \1 fill='currentColor'\/>/" -i '$@'
	perl -pe "s/<circle id='gpDot' r='[^']*' (.*)\/>/<rect id='gpDot' width='$(shell printf '%g\n' $$(echo 530/500 | bc -l))' height='$(shell printf '%g\n' $$(echo 530/500 | bc -l))' \1 fill='currentColor'\/>/" -i '$@'

bv%.svg: bv%.dat
	GNUTERM='svg enhanced fontscale 1' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$VPLOT_SVG"
#	perl -pe "s/<circle id='gpDot' (.*)\/>/<circle id='gpDot' \1 fill='currentColor'\/>/" -i '$@'
	perl -pe "s/<circle id='gpDot' r='[^']*' (.*)\/>/<rect id='gpDot' width='$(shell printf '%g\n' $$(echo 530/500 | bc -l))' height='$(shell printf '%g\n' $$(echo 530/500 | bc -l))' \1 fill='currentColor'\/>/" -i '$@'

#
define PPLOT_EPS
set palette gray negative;
unset colorbox;
set xlabel '{/$(SERIF) parameter {/$(SLANT) a}}';
set ylabel '{/$(SERIF) state value {/$(SLANT) x}}';
set size square;
set xrange $(XRANGE);
set yrange $(YRANGE);
set mono;
if (!bin_flag) {
  plot input with image notitle;
}
else {
  plot input binary format="%double%double%char" with image notitle;
}
endef
export PPLOT_EPS

p%.eps: p%.dat
	GNUTERM='postscript eps enhanced level3 fontscale 1' gnuplot -e "set output '$@'; input='$<'; bin_flag=0; $$PPLOT_EPS"
#	GNUTERM='epscairo enhanced fontscale 1' gnuplot -e "set output '$@'; input='$<'; bin_flag=0; $$PPLOT_EPS"
	perl -pe "s/\/Pnt {.*} def/\/Pnt {stroke gsave 0 setlinecap 10 setlinewidth M 10 0 V stroke grestore} def/" -i '$@'

bp%.eps: bp%.dat
	GNUTERM='postscript eps enhanced level3 fontscale 1' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$PPLOT_EPS"
#	GNUTERM='epscairo enhanced fontscale 1' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$PPLOT_EPS"
	perl -pe "s/\/Pnt {.*} def/\/Pnt {stroke gsave 0 setlinecap 10 setlinewidth M 10 0 V stroke grestore} def/" -i '$@'

#
define PPLOT_PDF
set palette gray negative;
unset colorbox;
set xlabel '{/"Times New Roman" parameter {/$(SLANT) a}}';
set ylabel '{/"Times New Roman" state value {/$(SLANT) x}}';
set size square;
set xrange $(XRANGE);
set yrange $(YRANGE);
set mono;
if (!bin_flag) {
  plot input with image notitle;
}
else {
  plot input binary format="%double%double%char" with image notitle;
}
endef
export PPLOT_PDF

p%.pdf: p%.dat
	GNUTERM='pdf enhanced fontscale .425' gnuplot -e "set output '$@'; input='$<'; bin_flag=0; $$PPLOT_PDF"
#	GNUTERM='pdfcairo enhanced fontscale 1' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$PPLOT_PDF"

bp%.pdf: bp%.dat
	GNUTERM='pdf enhanced fontscale .425' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$PPLOT_PDF"
#	GNUTERM='pdfcairo enhanced fontscale 1' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$PPLOT_PDF"

#
define PPLOT_SVG
set palette gray negative;
unset colorbox;
set xlabel '{/$(SERIF) parameter {/$(SLANT) a}}';
set ylabel '{/$(SERIF) state value {/$(SLANT) x}}';
set size square;
set xrange $(XRANGE);
set yrange $(YRANGE);
set mono;
if (!bin_flag) {
  plot input with image notitle;
}
else {
  plot input binary format="%double%double%char" with image notitle;
}
endef
export PPLOT_SVG

p%.svg: p%.dat
	GNUTERM='svg enhanced fontscale 1' gnuplot -e "set output '$@'; input='$<'; bin_flag=0; $$PPLOT_SVG"
#	perl -pe "s/<circle id='gpDot' (.*)\/>/<circle id='gpDot' \1 fill='currentColor'\/>/" -i '$@'
	perl -pe "s/<circle id='gpDot' r='[^']*' (.*)\/>/<rect id='gpDot' width='1' height='1' \1 fill='currentColor'\/>/" -i '$@'

bp%.svg: bp%.dat
	GNUTERM='svg enhanced fontscale 1' gnuplot -e "set output '$@'; input='$<'; bin_flag=!0; $$PPLOT_SVG"
#	perl -pe "s/<circle id='gpDot' (.*)\/>/<circle id='gpDot' \1 fill='currentColor'\/>/" -i '$@'
	perl -pe "s/<circle id='gpDot' r='[^']*' (.*)\/>/<rect id='gpDot' width='1' height='1' \1 fill='currentColor'\/>/" -i '$@'

#
clean:
	rm -f $(EXES) $(EXES:%=v%.dat) $(EXES:%=p%.dat) .DS_Store
	rm -f $(EXES:%=bv%.dat) $(EXES:%=bv%.eps) $(EXES:%=bv%.pdf) $(EXES:%=bv%.svg)
	rm -f $(EXES:%=bp%.dat) $(EXES:%=bp%.eps) $(EXES:%=bp%.pdf) $(EXES:%=bp%.svg)

distclean: clean
	rm -f $(ALL)
