#!/bin/bash

declare -i yidx

EXP=$(echo *.trc)
EXP=${EXP%%.trc}
OFFSETFILE=${EXP}.stree.root-d1.node-len-offsets
SEQFILE=${EXP}.seq
SEQGPLT=${SEQFILE}.pdf.gplt

if [ ! -f $OFFSETFILE -o ! -f $SEQFILE -o ! -f $SEQGPLT -o ! -f ${EXP}.dict.txt ]; then
    echo "ERROR: can't find $OFFSETFILE or $SEQFILE or $SEQGPLT or ${EXP}.dict.txt" > /dev/stderr
    exit -1
fi

function hl()
{
    local x1="$1"
    local x2="$2"
    local y="$3"
    local lt=${4:-"rgbcolor \"#0000FF\""} 
    local label="$5"
    local labelpos="$6"

    echo "set arrow from $x1,$y to $x2,$y nohead front lt $lt lw 2"
    if [[ -n $label ]]; then
	if [[ -n $labelpos ]]; then
	    echo "set label \"$label\" at $labelpos center font \"arial,10\""
	else
	    echo "set label \"$label\" at $(( x1 + (x2 - x1)/2 )),$y center font \"arial,10\""
	fi
    fi
}

echo "PROCESSING N-GRAM PLOT"
OUTPUT=${OFFSETFILE}.pdf

echo "  Creating ${OUTPUT}.gplt"
shade=32
plen=0; yidx=0
sort -n -k2,2 $OFFSETFILE | while read nid len d1 offsets; do 
    read -a olist <<< "$offsets"; 
    color=$(printf "#0000%x" $shade); 
    ((shade=(shade*2+1)%256));
    for idx in "${olist[@]}"; do
	if [[ "$len" -gt "$plen" ]]; then
	    (( yidx+=5 ))
	    plen=$len
	fi
	if [[ "$len" -lt "10" ]]; then
	    hl $idx $(( idx+len-1 )) $yidx
	else
	    hl $idx $(( idx+len-1 )) $yidx "rgbcolor \"$color\"" $nid
	fi
    done
done > ${OUTPUT}.gplt;
cat $SEQGPLT >> ${OUTPUT}.gplt;

TRACELEN=$(stat -c%s ${SEQFILE})
TRACELEN=$(( TRACELEN / 2 ))
MAXID=$(cat ${EXP}.dict.txt | wc -l)
if [[ "$MAXID" -lt "$yidx" ]]; then
    MAXID=$yidx
fi
WIDTH=$(echo "scale=4; ${TRACELEN}/72" | bc)
HEIGHT=$(echo "scale=4; ${MAXID}/72" | bc)
(
  echo set terminal pdfcairo size ${WIDTH},${HEIGHT}
  echo set output \"${OUTPUT}\"
  echo set title \"${OFFSETFILE}\"
  echo set xrange [1:${TRACELEN}]
  echo set yrange [1:${MAXID}]
) > ${OUTPUT}.gplt.hdr

echo "  Creating ${OUTPUT}"
cat ${OUTPUT}.gplt.hdr ${OUTPUT}.gplt | gnuplot
