#! /bin/sh
# CVS: $Id: plot_all,v 1.21 2006/01/03 18:04:21 rijsman Exp $
# This script should be run in the Performance directory

if [ $# -le 1  ];  then
  echo "Usage: $0 <log-file-name> <output-dir>"  
  echo "<log-file-name>, mandatory, the log file to be used"
  echo "<output-dir>, mandatory, the output log files will be placed here."
  echo "This script will run the various plot scripts on a particular log file."
  echo 
  echo "Example: "
  echo "./plot_all log PlotLogs"
  echo 
  exit
fi

current_dir=`pwd`

rm -f temp
touch temp
ls -l $1 > temp 
output_dir=$2
log_date=`awk '// { printf("%s_%s_%s", $6, $7, $8 ); }' temp`
newdir=$output_dir/$log_date

mkdir -p $newdir

build_log=$newdir/report.trace
rm -f $build_log
log_file=$1

touch $build_log
echo 'Plot directory = '$newdir > $build_log

if [ "$3" != "noplot" ];  then
  ${0%/*}/plot $log_file plot.png all nolatency >> $build_log
  ${0%/*}/plot $log_file init.png init nolatency >> $build_log
  ${0%/*}/plot_efficiency $log_file efficiency.png lines >> $build_log 
  ${0%/*}/plot_efficiency $log_file efficiencyC.png nolines >> $build_log
fi

#  log_name has to be after this statement when 
#  the directory contains one file only.  What we
#  should do is extract the last part of the string 
#  $1 up to the /
cp $log_file $newdir/
cd $newdir/
log_name=`ls *log*`
cd $current_dir

name_report=report.html
plot_file=$newdir/$name_report

rm -f $plot_file
if [ -f "plot.png" ];  then
mv plot.png $newdir/
fi

if [ -f "init.png" ];  then
init=1
mv init.png $newdir/
else
init=0
fi
if [ -f "steps.png" ];  then
mv steps.png $newdir/
fi

if [ -f "efficiency.png" ];  then
mv efficiency.png $newdir/
fi
if [ -f "efficiencyC.png" ];  then
mv efficiencyC.png $newdir/
fi

rm -f $plot_file
touch $plot_file
echo '<HTML>' > $plot_file
echo '<BODY>' >> $plot_file
echo '<HR>' >> $plot_file
echo '<H1><CENTER>Report created from IDEA log file</CENTER></H1>' >> $plot_file
echo '<H3><CENTER>'$log_file' ('$log_date')</CENTER></H3>' >> $plot_file
echo '<HR>' >> $plot_file
echo '<a href="'$log_name'"<H2><CENTER>View log file (local copy)</CENTER></H2></a>' >> $plot_file
echo '<TR><TD><HR></TD></TR>' >> $plot_file
echo '<TABLE BORDER=0 ALIGN=CENTER>' >> $plot_file
echo '<TR><TD><IMG src="plot.png"></TD></TR>' >> $plot_file
if [  $init == 1  ];  then
	echo '<TR><TD><IMG src="init.png"></TD></TR>' >> $plot_file
else
	echo '<TR><TD><HR></TD></TR>' >> $plot_file
	echo '<TR><TD><BR>Not showing detailied overview of the initialization part of reactive-planner cycles.</TD></TR>' >> $plot_file
	echo '<TR><TD><BR>Make sure to use log file with output level to high to display this graph.</TD></TR>' >> $plot_file
fi

#echo '<TR><TD><IMG src="steps.png"></TD></TR>' >> $plot_file
echo '<TR><TD><HR></TD></TR>' >> $plot_file
echo '<TR><TD><IMG src="efficiency.png"></td></TR>' >> $plot_file
echo '<TR><TD><IMG src="efficiencyC.png"></td></TR>' >> $plot_file
echo '</TABLE>' >> $plot_file
echo '' >> $plot_file

rm -f temp_report

echo '<BR>==================Warnings and Errors======================<BR>' >> temp_report
error_regexp="ERROR|WARNING|_err|execution_complete" 
egrep $error_regexp $1 >> temp_report
#errors_found=`wc $error_log | awk '{ print $1 }'`

echo '<BR>===============Communnication events=======================' >> temp_report
${0%/*}/../Test/build-summary-from-log $1 >> temp_report
cat $1.summary >> temp_report

echo '</BODY></HTML>' >> temp_report
awk '// { printf("<BR>%s",$0 ); }' temp_report >> $plot_file

#overview_file=$output_dir/plots.html
overview_file=plots.html

current_dir=`pwd`
cd $output_dir/
rm -f $overview_file
touch $overview_file
echo '<HTML><BODY>' > $overview_file
echo 'Plots: <BR><HR>' >> $overview_file
look_for=$name_report

for file in `ls */$look_for` 
do
  echo '<BR><a href="'$file'">'$file'</a>' >> $overview_file
done

echo '</BODY></HTML>' >> $overview_file
cd $current_dir

echo 'Master plot report is at: '`pwd`'/plots.html' >> $build_log
echo 'Report page is at: '`pwd`'/'$plot_file >> $build_log

rm -f ${log_file}-plot.cmds
rm -f ${log_file}-plot.data
rm -f ${log_file}-plot.data.0
rm -f ${log_file}-plot.data-plot.cmds
rm -f ${log_file}-plot.data-plot.data
rm -f ${log_file}.summary
rm -f temp_report
rm -f temp
