#!/bin/bash

# This (ugly) script draws a graph from the QtDMM and devicedown.sh logs.
# gnuplot is required!
# For an detailed instruction please visit: http://powersave.sf.net/pm-report/
#
# The format of measure log have to be:
# 
#
#      COL1      COL2      COL3
#      ========================
#       (DATE)   TIME     VALUE
#      XXXXXX   HH;MM:SS XX.XXX
#
#       %s\t%s\t%s\n

# Internal File separator - don't change this. Read man page of bash for further details.
# IFS is only set with \n so we can read line by line of lspci output
IFS="
"

LOGFILE=$1
MACHINE=$2
RESOLUTION=$3

if ! [ $3 ]; then
	echo -e "usage: $0 <logfile> <machine name> <graph width (px)>\n";

	echo -e "<logfile> - use devicedown.sh log file for this and name QtDMM with append \033[1m.log\033[0m\n";
	echo -e "\tdevicedown.sh log: consumption_report.112912790"; 
	echo -e "\tQtDMM log should be named: consumption_report.112912790\033[1m.log\033[0m";
	echo -e "\tin this case -> $0 consumption_report.112912790 ASUS_M2400N 6000\n";

	echo -e "<machine name> - as a single string. for example:\n";
	echo -e "\tIBM T40P -> $0 consumption_report.112912790 \033[1mIBM_T40P\033[0m 6000"; 
	echo -e "\tCompaq Armada e500 -> $0 consumption_report.11291790 \033[1mCompaq_Armada_e500\033[0m 6000\n"; 
	echo -e "thanks :) - have a nice day!";
	exit 0;
fi;

# check for gnuplot
if ! [ -x "`which gnuplot`" ]; then
	echo -n "gnuplot - was _NOT_ found! -> ";
	echo "http://www.gnuplot.info/";
	exit 0;
fi;

echo -e "Preparing GNUplot script.";
echo -e "Found devices:";
echo -e "[every _tested_ device appears _here_ two times - \033[1mthat's ok! (:\033[0m ]";

# read test-machine-logfile (from devicedown.sh) line-by-line
for n in `cat $LOGFILE`; do
	PCIID=$( echo $n | awk '{print $2}');
	DATE=$( echo $n | awk '{print $1}');
	# WARNING!
	# i'm extremely green behind my ears....
	# also i'm not quite good in regular expressions...
	# if you think regular expressions is the holy grail .. skip next 6 lines.
	# otherwise you get eye cancer... :(

	# ugly #1
	if [ "$( echo $n | grep "changed to D3" )" -o "$( echo $n | grep "back to D0")" ]; then
		i=1;
		while [ "$( echo $n | cut -c 18- | cut -c $i )" != ":"  ]; do
			i=$(expr $i + 1);
		done;
		# ugly #2
		# TYPE is the hardware group - for example "Graphic Device"/"Ethernet Controller"/ .....
		TYPE=$(echo $n | cut -c 18-$(expr $i + 16));
		echo -e "\n\033[1m$TYPE\033[0m (D3||D0)";
	fi;
	# mark position of device while entering D3 state
	if [ "$( echo $n | grep "changed to D3" )" ]; then
		VERTICAL_LINES="$VERTICAL_LINES\nset arrow from \"$DATE\",graph 0 to \"$DATE\", graph 1 nohead;"; 
		IDLABELS="$IDLABELS\nset label \" $PCIID@D3\" at \"$DATE\",graph 0.85";
		LABELS="$LABELS\nset label \" $TYPE\" at \"$DATE\", graph 0.82";		
	# mark position of device while entering D0 state
	elif [ "$( echo $n | grep "back to D0" )" ]; then
		VERTICAL_LINES="$VERTICAL_LINES\nset arrow from \"$DATE\",graph 0 to  \"$DATE\", graph 1 nohead;";
		IDLABELS="$IDLABELS\nset label \" $PCIID@D0\" at \"$DATE\",graph 0.75";
		LABELS="$LABELS\nset label \" $TYPE\" at \"$DATE\", graph 0.72";	
	fi;
	echo -n ".";
done;

echo -e "\n\nGNUplot script finished.";
echo -n "Running GNUplot - plot, plot, plot";

# creating gnuplot script and run gnuplot with sdtin
echo -e '
set grid
set xlabel "[Time in s]"
set ylabel "[A]"
set xdata time
set timefmt "%H:%M:%S"
set format x "%H:%M:%S"
set format y "%.3f"
set ytics 0.005
' $VERTICAL_LINES '
' $LABELS '
' $IDLABELS '
set title " Machine: ' $MACHINE '"
set terminal png size '$3' 
set output "'$LOGFILE'.png"
plot "'$LOGFILE'.log" using 2:3 with lines
' | gnuplot - &> /dev/null;

# finished
echo ", plot, plot, plllll ... ready!";
echo -e "$0 says: \033[1m$LOGFILE.png\033[0m is successfully created.";
