#!/usr/bin/awk -f
#
# Generates a jgraph file to plot the shootout results.  There is one
# bar for each language being compared, normalized against gcc as the
# baseline.  The average is printed as the last bar.

BEGIN 	   { nidx = 0; tidx = 0; }
	   { name=$1;
	     median=$4;
	     idx = match(name,"/");
	     prefix=substr(name,1,idx-1);
	     name=substr(name,idx+1);
	     for (i=0; i<nidx; i++)
	       if (names[i] == name) break;
	     if (i == nidx)
	       names[nidx++] = name;
	     for (i=0; i<tidx; i++)
	       if (types[i] == prefix) break;
	     if (i == tidx)
	       types[tidx++] = prefix;
	     # printf("%s,%s = %g\n",name,prefix,median);
	     times[name,prefix] = median;
	   }
END 	   { printf ("newgraph clip\n\n");
	     filld = 1.0 / tidx;
	     fill = 0.0;
	     group_delta = 8;
	     bar_delta = 5;
	     incr = tidx * bar_delta + group_delta;
	     for (j=0;j<tidx;j++) {
	       x = group_delta;
	       type = types[j];
	       printf ("newcurve\n  label : %s\n  marktype xbar\n",type);
	       printf ("fill %2.2f\n  pts ",fill);
	       fill = fill + filld;
	       avg = 0.0;
	       for (i=0;i<nidx;i++) {
  	         name = names[i];
	         base = times[name,"gcc"];
	         if (base == 0) {
		   continue;
	         }
		 normal = times[name,type] / base;
		 printf ("%d %g ",x + j * bar_delta,normal);
		 x = x + incr;
		 avg = avg + normal;
	       }
	       avg = avg / nidx;
	       printf ("%d %g ",x + incr + j * bar_delta,avg);
	       printf ("\n\n");
	     }
	     printf ("yaxis min 0 max 20 size 2 label : normalized elapsed time\n");
	     printf ("xaxis min 0 max %d size 5.5\n  no_auto_hash_labels no_auto_hash_marks\n",group_delta + tidx / 2 + (2+nidx) * incr + 10);
	     printf ("  hash_labels rotate -45 vjt hjl\n");
	     x = group_delta + (tidx / 2);
	     for (i=0;i<nidx;i++) {
	       if (times[names[i],"gcc"] == 0) continue;
	       printf ("hash_at %d hash_label at %d : %s\n",
		       x,x,names[i]);
	       x = x + incr;
	     }
	     printf ("hash_at %d hash_label at %d : average\n",
		     x + incr,x + incr);
	     printf ("\nlegend x 50 y 18\n");
           } 
  
