#!/bin/bash

#######################################################################
# LaTeX statistics generator - version 0.61 (enough for my purposes)
# Copyright (C) 2004  Andrea Trentini (www.atrent.it)
#
#    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.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#    or browse http://www.gnu.org/licenses/gpl.txt
#######################################################################

#######################################################################
# scopo: generare un po' di statistiche sui file latex
# tipo: quali immagini sono incluse, quali file, quali riferimenti, ...
#######################################################################

# TODO: have a look at .aux and .log files!

#######################################################################
# FUNCTIONS
#######################################################################
function ifecho
{
 if
  test $ONE != "--oneline"
 then
  echo -e $*
 fi
}

function stripComments()
{
 grep -v '^%' $1
}

function extractFeature
{
 if
  test $ONE != "--oneline"
 then
  stripComments $1 | awk '/\\'$2'(\[.*\])*{.*}/ { match($0,/\{.*\}/)
   printf "\t\t%s @ %d\n",substr($0,RSTART,RLENGTH),NR}'
 else
  stripComments $1 | awk '/\\'$2'(\[.*\])*{.*}/ { match($0,/\{.*\}/)
   printf "%s:%s:%s:%s\n",'\"$2\"','\"$1\"',substr($0,RSTART,RLENGTH),NR}'
 fi

 # old version
 # print '\"$1\:\"' substr($0,RSTART,RLENGTH)}'
}


#######################################################################
# MAIN
#######################################################################

echo REMARK: line numbers are wrong because of \'stripComments\'

# just generate a short list of available refs
cat *.bib | grep '@' | sort > availableRefs.txt

ONE="NO"
if
 test $# -ge 1
then
 ONE=$1
fi

# trova tutti i .tex
#LIST=$(find . -type f -name '*.tex' | sort)
LIST=$(ls *.tex | sort)

if
 test $ONE = NO
then
 echo ----------------
 echo File sizes \(LOC\)
wc -l $(echo $LIST)
fi

########################

ifecho ----------
ifecho Inclusions
for source in $LIST
do
 ifecho '\t' $source
 extractFeature $source input
 extractFeature $source include
 extractFeature $source includeonly
done

ifecho ----------
ifecho References
for source in $LIST
do
 ifecho '\t' $source
 extractFeature $source cite

 # .aux files are simpler to parse
 # something like 'grep "\citation" *.aux'
 # (there can be more than one aux)
 # but there is one aux file for the main latex file...
 # and not for every latex source, so you lose
 # info about 'which cites what'
done

ifecho ------
ifecho Images
for source in $LIST
do
 ifecho '\t' $source
 extractFeature $source includegraphics
done

ifecho ------
ifecho Labels
for source in $LIST
do
 ifecho '\t' $source
 extractFeature $source label
done

ifecho ----
ifecho Refs
for source in $LIST
do
 ifecho '\t' $source
 extractFeature $source ref
done