#!/bin/bash

function to_latex() {
echo $1 | sed 's/_/\\_/g'
}

function svn_info() {
    cd $1
    svn info | grep URL
    echo '\\'
    svn info | grep Revision
    echo '\\'
}

function abstract()
{
    cd $ROOT
    hostname ; echo '\\'
    pwd ; echo '\\'
    svn_info $ROOT/XIOS
    svn_info $ROOT/DYNAMICO
    svn_info $ROOT
}

function all_results()
{
    cd $ROOT/RUNS
    for TEST in * ; do
	NAME=$(to_latex $TEST)
	echo "\subsection{$NAME}"
	for GRAPH in $(ls $TEST/*.png) ; do
	    cat <<EOF
\begin{figure}
\centering{}
\includegraphics[scale=0.5]{$ROOT/RUNS/$GRAPH}
\caption{$NAME}
\end{figure}
EOF
	done
	echo '\newpage{}'
    done
#    cd $ROOT/tmp
}

function all_params()
{
    cd $ROOT/RUNS
    for TEST in * ; do
	NAME=$(to_latex $TEST)
	cat <<EOF
\subsection{$NAME}
\begin{lstlisting}
$(cat $TEST/run.def)
\end{lstlisting}
EOF
    done
#    cd $ROOT/tmp
}

ROOT=$PWD

for dir in $ROOT/RUNS/* ; do
    cd $dir
    echo
    pwd
    ls -l post.py netcdf
    python post.py & 
done
wait

cd $ROOT/tmp
rm -f *.aux *.log *.toc *.tex

cat <<EOF >report.tex
\documentclass[10pt,a4paper,english]{article}
\usepackage{graphicx}
\usepackage{listings}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{color}
\usepackage{babel}
\usepackage[unicode=true,pdfusetitle,
 bookmarks=true,bookmarksnumbered=false,bookmarksopen=false,
 breaklinks=false,pdfborder={0 0 1},backref=false,colorlinks=true]
 {hyperref}
\usepackage{geometry}
\geometry{verbose,tmargin=2cm,bmargin=2cm,lmargin=2cm,rmargin=2cm}

\begin{document}

\title{DYNAMICO correctness report ($arch)}
\date{}
\author{Thomas Dubos}
\maketitle

\begin{abstract}
$(abstract)
\end{abstract}

\tableofcontents{}
\section{Results}
$(all_results)

\twocolumn

\section{Parameters}
$(all_params)
\end{document}
EOF

pdflatex -interaction=batchmode report.tex
pdflatex -interaction=batchmode report.tex
mailx -s "DYNAMICO correctness report " -a report.pdf dubos@lmd.polytechnique.fr </dev/null
