#!/bin/sh
#---------------------------------------------------------------------------
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
# Author: Anne Pacalet (mailto:anne.pacalet@tele2.fr)
#---------------------------------------------------------------------------
# gprof_html : génération d'un fichiers html à partir d'un fichier gprof.
#---------------------------------------------------------------------------
Usage () {
  echo "gprof_html : génération d'un fichier html";
  echo "             à partir des résultats de gprof."
  echo "    Permet aussi de lancer un test et la génération du fichier gprof,"
  echo "    mais suppose que le programme a déjà été compilé avec l'option -pg"
  echo " -h       : cette aide"
  echo " -p <nom> : nom du fichier de mesure (défaut : gmon.out)"
  echo " -g <nom> : nom du fichier gprof (défaut : gprof.txt)"
  echo " -o <nom> : nom du fichier html (défaut : gprof.html)"
  echo " -e <exec> : nom du progamme à executer"
  echo "             Si le programme prend des arguments,"
  echo "             $0 -e prog -- arg1 arg2"
  echo "             (ce qui suit -- est sera passé au programme)"
  echo "             défaut : on utilise le fichier gprof existant."
}
Verbose () {
  if [ ! -z "$verb" ] ; then
    echo $*
  fi
}

gprof_html_fic=`which $0`
gawk_fic=`dirname $gprof_html_fic`/gprof.awk

if ! [ -r $gawk_fic ] ; then
  echo "ERREUR : ce programme nécessite le filtre gprof.awk";
  echo "         qui aurait du accompagner...";
fi

verb=0;
gmon_fic=gmon.out;
gprof_fic=gprof.txt;
html_fic=gprof.html;

while getopts p:g:o:e:hv opt
do
  case "$opt" in
  p) gmon_fic=$OPTARG;;
  g) gprof_fic=$OPTARG;;
  o) html_fic=$OPTARG;;
  e) prog=$OPTARG;;
  h) Usage; exit 0;;
  v) verb=1;;
  *)  Usage; exit 1;;
  esac
done
shift $(($OPTIND - 1))

if [ "$prog" != "" ] ; then
  cmd="$prog $*"
  Verbose "Lancement de : $cmd"
  $cmd ;
  if [ -f gmon.out ] ; then
    if [ "$gmon_fic" != "gmon.out" ] ; then
      Verbose "Déplacement de gmon.out -> $gmon_fic";
      mv gmon.out $gmon_fic;
    fi ;
  else
    echo "ERREUR : le fichier gmon.out n'a pas été généré"
    echo "         Vérifier que la compilation et l'édition de lien"
    echo "         ont bien été faites avec l'option -pg"
    exit 1;
  fi
  Verbose "Génération de $gprof_fic"
  gprof $prog $gmon_fic > $gprof_fic
fi 

Verbose "Génération de $html_fic"
gawk -f $gawk_fic $gprof_fic > $html_fic

