#!/bin/bash

#set -x

i=5

Xvfb :3 >/dev/null 2>&1 &
xpid=$!
export DISPLAY=:3

rm for8ga3out_${i}.lst

for((type=0;type<2;type++))
do
for((shift=0;shift<40;shift++))
do
echo type: $type shift: $shift
cat >/tmp/for8.asc <<EOF
_b_e_g_i_n
   _r_e_a_l clock1;
   _b_o_o_l_e_a_n clock;
   _i_n_t_e_g_e_r i,j,n;
   n:=10000;
   pack(clock, 0, 41, 0);
   pack(clock,
	  0,  9,  0,
	 10, 19,  1,
	 20, 25, 62,
	 30, 35, 17,
	 39, 39,  1,
	 40, 40,  1);
   gier(clock);
   _f_o_r i:=1 _s_t_e_p 1 _u_n_t_i_l n _d_o
   _b_e_g_i_n
EOF
if [ $type -eq 0 ]
then
  echo "_f_o_r j:=1 _s_t_e_p 1 _u_n_t_i_l ${i} " >>/tmp/for8.asc
else
      echo "_f_o_r j:=1" >>/tmp/for8.asc
for((j=2;j<=i;j++))
do
  echo ",$j" >>/tmp/for8.asc
done
fi
cat >>/tmp/for8.asc <<EOF
      _d_o;
   _e_n_d;
   clock1:=gier(clock);
   output(|<ndd|>,${i},$type,$shift);
   output(|<nddddd.dd|>,clock1);
   outcr;
EOF
for((k=0;k<shift;k++))
do
echo "   j:=j;" >>/tmp/for8.asc
done
cat >>/tmp/for8.asc <<EOF
_e_n_d;
EOF

D=for8.demo
cat >$D <<EOF
LOADCONFIG ../configs/hjælpga3.gier
LOADREADER /tmp/for8.asc
LOADPUNCH for8out_${i}.flx
EOF
../maketext.sh "_halgol" >>$D
echo "WAITINPUT" >>$D
echo "TYPE 64" >>$D
../maketext.sh "_e" >>$D
echo "WAITINPUT" >>$D
echo "TYPE 64" >>$D
echo "WAITINPUT" >>$D
# start compile
echo "TYPE 0" >>$D
echo "WAITINPUT" >>$D
# start run
echo "TYPE 0" >>$D
echo "WAITINPUT" >>$D
echo "FINISHPUNCH" >>$D
echo "QUIT" >>$D

../GIER $D >/dev/null 2>&1

flx2a <for8out_${i}.flx | tail -1 >>for8ga3out_${i}.lst
done
done

kill $xpid

/bin/echo -e "shift\\tstep" >for8ga3out_${i}_0.dat
/bin/echo -e "shift\\tlist" >for8ga3out_${i}_1.dat

sed 's/\s\+/	/g' <for8ga3out_${i}.lst | while read -a line
do
  n=${line[0]}
  type=${line[1]}
  shift=${line[2]}
  cpu=${line[3]}
  /bin/echo -e "$shift\\t$cpu" >>for8ga3out_${i}_${type}.dat
done

cat >for8plot.plt <<EOF
set size ratio 0.71
set datafile separator "\t"
set terminal pdfcairo size 29.7cm,21cm font 'Helvetica,20'
set output "for8ga3plot_${i}.pdf"

set title "list mod step n = ${i} GAIII"
stepcolor = "#ff0000"; listcolor = "#0000ff"
set auto x
set yrange [0:]
set style data histogram
set style histogram cluster gap 1
set style fill solid border -1
set boxwidth 0.9
set xtic scale 0
plot 'for8ga3out_${i}_0.dat' using 2:xticlabel(1) ti col linecolor rgb stepcolor, 'for8ga3out_${i}_1.dat' u 2 ti col linecolor rgb listcolor
EOF
gnuplot <for8plot.plt
pdftk for8plot_${i}.pdf for8ga3plot_${i}.pdf cat output for8_${i}.pdf
montage -geometry 841x595 -tile 1x2 for8plot_${i}.pdf for8ga3plot_${i}.pdf for8_${i}.png
