#!/bin/bash

# Copyright (C) 2005-2022 René Rebe

. ./functions

alt=1
outext=png

while [ "$1" ]; do
	case $1 in
		-alt) alt=0 ;;
		-eps) outext=eps ;;
		*) echo "Unknown option \"$1\"" ; exit ;;
	esac
	shift
done

ccs="`cd results; ls | sed 's,.*_\(.*\)-.*,\1,' | sort -u`"

for t in bt rt size; do
  benchs="`cd results; ls *-$t | cut -d _ -f 1 | sort -u`"
  echo $benchs

  (
  # generate data files
  echo -n ""
  for cc in $ccs ; do
	echo -n "$cc"
	ref=""
	for b in $benchs ; do
		v="`tail -n 1 results/${b}_${cc}-$t 2> /dev/null`"
		if [ "$v" ] ; then
		  [ "$ref" ] || ref=$v
		else
		  v="0"
		fi
		# perc="`echo "scale=5; ( $v * 100 ) / $ref" | bc`"
		echo -ne "\t$v"
	done
	echo ""
  done
  ) > stat2-$t.csv # | sort -n -k +6

  # some manual sort "optimization"
  if [ $alt -eq 1 ]  ; then
  	sed "s,^\(.*\)-\(O.[^[:blank:]]*\),\2-\1, ; s/Os/O1sSs/" stat2-$t.csv | sort -k +1 |
	sed "s,\(.*\)-\([^[:blank:]]*\),\2-\1, ; s/O1sSs/Os/" >  stat2-$t.csv.new
	mv stat2-$t.csv{.new,}
  else
	sed "s/Os/O1sSs/; s/native/znative/" stat2-$t.csv | sort -k +1 |
	sed "s/O1sSs/Os/; s/znative/native/" >  stat2-$t.csv.new
	mv stat2-$t.csv{.new,}
  
  fi

  # generate ploticus files
  (
  case $t in
	rt) title="Runtime" ;;
	bt) title="Buildtime" ;;
	size) title="Size" ;;
  esac

  n=0
  for cc in $ccs ; do
	: $(( n++ ))
  done

  nbenchs=0
  for b in $benchs ; do
	: $(( nbenchs++ ))
  done

  rowh=.25

  xmax="$( echo "1.8 + $nbenchs" | bc )"
  ymax="$( scale=1; echo ".08 + $rowh * $n " | bc )"

  cat <<-EOT
#proc page
  pagesize: $(echo "0.5 + $xmax" | bc) $(echo "0.5 + $ymax" | bc)

#proc getdata
  file: stat2-$t.csv
  delimit: tab

#proc areadef
  rectangle: 1.75 1.06 $(echo $xmax + 0.25 | bc) $ymax
  //xautorange: datafield=2 nearest=5
  xautorange: datafield=2 lowfix=0
  yscaletype: categories
  ycategories: datafield=1
  title: $title
  titledetails: size=13 style=B adjust=2,0.15

#proc yaxis:
  stubs: usecategories
  stubhide: yes
  tics: none
  axisline: none
  gridblocks: gray(.875) gray(0.75)
  gridlineextent: min-1.4 max+.1

EOT

  i=0
  for b in $benchs ; do
        : $(( i++ ))

	cat <<-EOT
#proc areadef
  rectangle: $(( 1+i )) 1 $( echo "1.7 + $i" | bc ) $( scale=1; echo "$rowh * $n" | bc )
  xautorange: datafield=$(( 1+i )) lowfix=0
// nearest=5
  yscaletype: categories
  ycategories: datafield=1
//  yaxis.gridblocks: gray(0.6) white

EOT
    if [ $i = 1 ] ; then
	cat <<-EOT
#proc yaxis:
  stubs: usecategories
  tics: none
  axisline: none
  stubdetails: align=L adjust=-1.2,0

#proc annotate
  location: max min-0.08
  textdetails: size=8
  text: (in seconds - smaller is better)
  
EOT
    fi
    cat <<-EOT
#proc bars
  horizontalbars: yes
  lenfield: $(( 1+i ))
  outline: no
  barwidth: 0.052
  color: blue
  showvalues: yes
  numbersformat: %.2f
  hidezerobars: yes
  labeldetails: align=L size=7 adjust=-0.005,-0.015
//  labeldetails: align=L size=6 adjust=0,-0.13
//  labelpos: min
//  colorlist: lightblue

#proc annotate
  location: max-0.5 max-0.005
  textdetails: align=C
  text: ${b/-*/}

EOT
  done
  ) > stat2-$t.ploticus
done

# we now always render via eps output, so even other format previews are
# more preceise when done for the usual hard copy printing later on
# (otherwise png fonts are off by some pixels :-()

echo "Rendering -bt graph ..."
ploticus -eps -o stat2-bt.eps stat2-bt.ploticus
echo "Rendering -rt graph ..."
ploticus -eps -o stat2-rt.eps stat2-rt.ploticus
echo "Rendering -size graph ..."
ploticus -eps -o stat2-size.eps stat2-size.ploticus

case $outext in
	eps)	;;
	*)	dpi=192
		echo "Rendering bitmap images ..."
		convert -density ${dpi}x${dpi} stat2-bt.eps -background "#ffffff" -flatten -type Palette stat2-bt.$outext
		convert -density ${dpi}x${dpi} stat2-rt.eps -background "#ffffff" -flatten -type Palette stat2-rt.$outext
		convert -density ${dpi}x${dpi} stat2-size.eps -background "#ffffff" -flatten -type Palette stat2-size.$outext
		#rm -f stat2-{bt,rt}.eps
		;;
esac	

