#!/bin/bash
#set -x
EXP=$(echo *.trc)
EXP=${EXP%%.trc}
SEQFILE=${EXP}.seq

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

echo "PROCESSING SEQ PLOT"

TRACELEN=$(stat -c%s ${SEQFILE})
TRACELEN=$(( TRACELEN / 2 ))
MAXID=$(cat ${EXP}.dict.txt | wc -l)
OUTPUT=${SEQFILE}.pdf

if [ ! -f "${OUTPUT}.dat" ]; then
    echo "  Creating ${OUTPUT}.dat"
    od -An -w2 -v -d ${SEQFILE} | cat -n > ${OUTPUT}.dat
fi

echo "  Creating ${OUTPUT}.gplt"
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 \"${SEQFILE}\"
  echo set xrange [1:${TRACELEN}]
  echo set yrange [1:${MAXID}]
) > ${OUTPUT}.gplt.hdr
(
  echo set xlabel \"Time\"
  echo set ylabel \"Action ID ordered by frequency\"
  echo plot \"${OUTPUT}.dat\" with points pt 7 ps 0.125 notitle
  echo quit
) > ${OUTPUT}.gplt

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