1#! /bin/sh
2
3FILE=$1
4TMP=$(mktemp -d /tmp/tmp.XXXXXX)
5function error
6{
7    cat 1>&2
8    exit 1
9    rm -rf $TMP
10}
11
12if ! cols=$(grep "#[ ]*COLUMNS" $FILE)
13then
14    error <<EOF
15E: $FILE: Cannot locate the COLUMNS descriptor
16EOF
17fi
18cols=$(echo $cols | sed 's/#//')
19columns=$(echo $cols | awk '{print $2;}')
20count=1
21while [ $count -le $columns ]
22do
23  column[$count]=$(echo $cols | awk -vcount=$count '{print $(2 + count);}')
24  if [ -z "${column[$count]}" ]
25      then
26      column[$count]=$count;
27  fi
28  count=$(($count + 1))
29done
30
31# Set up the plot area
32count=2
33with="with dots"
34cat > $TMP/gnuplot.script <<EOF
35set xlabel "${column[1]} (s)"
36set ylabel "Progress"
37EOF
38
39# Plot the events
40height=15
41grep "#[ ]*EVENT" $FILE | sed 's/#//' > $TMP/events
42cat $TMP/events | while read event x text
43do
44  if ! [ $event = "EVENT" ]
45      then
46      cat 1>&2 <<EOF
47E: Unknown event type "$event", ignoring
48EOF
49      continue;
50  fi
51  height_text=$(($height + 2))
52  echo "set arrow from $x, graph 0 to $x, graph 0.$height" >> $TMP/gnuplot.script
53  echo "set label \"$text\" at $x, graph 0.$height_text center" >> $TMP/gnuplot.script
54  height=$(($height + 8))
55done
56
57# Set Key
58echo "set key left top box lt 0" >> $TMP/gnuplot.script
59# Plot the data
60echo "plot '$FILE' using 1:2 title \"${column[$count]}\" $with" >> $TMP/gnuplot.script
61count=3
62while [ $count -le $columns ]
63do
64  echo "replot '$FILE' using 1:$count title \"${column[$count]}\" $with" \
65      >> $TMP/gnuplot.script
66  count=$(($count + 1))
67done
68
69
70( cat $TMP/gnuplot.script; cat ) | gnuplot
71rm -rf $TMP
72