recettes/build.sh

211 lines
6.1 KiB
Bash
Executable file

#!/bin/sh
INDIVID_FOLDER="recettes_individ"
INDIVID_FOLDER_PDF="recettes_individ_pdf"
CHAPTERS="chapters"
CHAPTERS2="chapters2"
MERGE="chapters_merge"
REPLACEMENT_SCRIPT="./replacements.pl"
prepare_individ() {
individ_source="$1"
individ_dest="$2"
cat > "$individ_dest" <<EOF
\documentclass{article}
\usepackage[a5paper]{geometry}
\usepackage[cm]{fullpage}
\usepackage[french]{babel}
\usepackage[nostepnumber]{cuisine2}
\usepackage{graphicx}
\usepackage{makeidx}
\usepackage[utf8]{inputenc}
\usepackage{environ}
\NewEnviron{recipe}[3]{%
\begin{recipe2}{#1}{#2}{#3}
\BODY
\end{recipe2}%
}%
\begin{document}
EOF
cat "$individ_source" >> "$individ_dest"
cat >> "$individ_dest" <<EOF
\end{document}
EOF
}
syntax_result() {
if [ $1 -eq 1 ]; then
if [ $printfail -eq 1 ]; then
printf "\033[01;31m[FAIL-%s]\t%s\033[00m\n" $2 $i
fi
else
if [ $printsuccess -eq 1 ]; then
printf "\033[01;32m[PASS-%s]\t%s\033[00m\n" $2 $i
fi
fi;
}
syntax_check() {
folder="$1"
failligne=0
failtemps=0
failportions=0
failindex=0
printsuccess=0
printfail=1
for i in $(ls "$folder"/*/*.tex); do
cat $i | sed 1q | perl -ne 'if (m/^\\begin{recipe}{.*?}{.*?}{.*?}$/) { exit 0; } else { exit 1; }';
ret=$?
if [ $ret -eq 1 ]; then
failligne=$(echo ${failligne}+1 | bc);
fi;
syntax_result $ret "LIGNE"
cat $i | sed 1q | perl -ne 'if (m/{(\d+(\d+)?\s((\w|[éèà])+\s?)+(\s\+\s)?)+}$/) { exit 0; } else { exit 1; }';
ret=$?
if [ $ret -eq 1 ]; then
failtemps=$(echo ${failtemps}+1 | bc);
fi;
syntax_result $ret "TEMPS"
regexpains='(\d+\smiches?)|(\d+\spains?(\s\w+\s)*(\s?\d+g)?)'
regexportions='(\d+\sportions?)'
regexgateaux='(1\s(plaque)|(moule).*cm)'
regexmisc='(\d+(-\d+)?\s(\w+\s)*((pièces?)|(bacs?)|(pots?)))'
cat $i | sed 1q | perl -ne "if (m/{$regexpains|$regexportions|$regexgateaux|$regexmisc}/) { exit 0; } else { exit 1; }";
ret=$?
if [ $ret -eq 1 ]; then
failportions=$(echo ${failportions}+1 | bc);
fi;
syntax_result $ret "PORTIONS"
cat $i | grep "\index{" > /dev/null
ret=$?
if [ $ret -eq 1 ]; then
failindex=$(echo ${failindex}+1 | bc);
fi;
syntax_result $ret "INDEX"
done
echo "Test LIGNE (ligne 'recipe' sur une ligne avec bon nombre d'arguments) : " $failligne "erreurs"
echo "Test TEMPS (le paramètre de temps pour la recette est présent et bien formaté) :" $failtemps "erreurs"
echo "Test PORTIONS (les portions sont sous un format listé)" $failportions "erreurs"
echo "Test INDEX (une entrée dans l'index est écrite)" $failindex "erreurs"
}
operate_replacements() {
# We check that the replacement script is there, otherwise we just
# ignore this step.
if [ -f "$REPLACEMENT_SCRIPT" -a -x "$REPLACEMENT_SCRIPT" ]; then
echo "Processing replacement rules"
$REPLACEMENT_SCRIPT $(find $@ -name '*.tex')
else
echo "'$REPLACEMENT_SCRIPT' not found. No automatic word substitution will occur" >&2
fi
}
build_pdf_book() {
pdflatex -halt-on-error "$1".tex > /dev/null
if [ $? -ne 0 ]; then
echo "Error while compiling the LaTeX file for $1"
exit 5
fi
makeindex "$1".idx > /dev/null 2> /dev/null
if [ $? -ne 0 ]; then
echo "Error while generating the index file for $1"
exit 6
fi
pdflatex -halt-on-error "$1".tex >/dev/null
if [ $? -ne 0 ]; then
echo "Error while compiling the LaTeX file for $1"
exit 5
fi
}
build_pdf_page() {
pdflatex "$1" -output-directory "$2" > /dev/null
}
if [ $# -eq 0 ]; then
TARGET_BUILD="all"
else
TARGET_BUILD="$1"
fi
# We have to garantee that the destination folders will be there ...
if [ ! -d $INDIVID_FOLDER ]; then
mkdir $INDIVID_FOLDER
fi
if [ ! -d $INDIVID_FOLDER_PDF ]; then
mkdir $INDIVID_FOLDER_PDF
fi
if [ ! -d $MERGE ]; then
mkdir $MERGE
fi
case "$TARGET_BUILD" in
all)
echo "Building the whole book"
rm -f "$CHAPTERS"/*.tex "$CHAPTERS2"/*.tex
operate_replacements "$CHAPTERS" "$CHAPTERS2"
echo "Preparing build files"
for chapter in $(find "$CHAPTERS"/* "$CHAPTERS2"/* -prune -type d); do
perl tri.pl "$chapter".tex "$chapter"
done;
for chapter in $(find "$CHAPTERS"/* "$CHAPTERS2"/* -prune -type d | awk -F'/' '{print $2;}' | sort | uniq); do
perl tri.pl "$MERGE"/"$chapter".tex "$CHAPTERS"/"$chapter" "$CHAPTERS2"/"$chapter"
done;
echo "Building book I"
build_pdf_book "recettes"
echo "Building book II"
build_pdf_book "recettes2"
echo "Building complete book"
build_pdf_book "recettes_merge"
;;
clean)
echo "Cleaning the build files"
rm -f "$CHAPTERS"/*.tex "$CHAPTERS2"/*.tex "$MERGE"/*.tex;
rm -f *.aux *.log *.ind *.ilg *.idx *.toc
;;
individ)
echo "Building individual recipes";
operate_replacements "$CHAPTERS" "$CHAPTERS2"
for chapter in $(find "$CHAPTERS"/* "$CHAPTERS2"/* -prune -type d); do
for recette in $(find "$chapter"/ -name '*.tex'); do
recette_base=$(basename "$recette")
echo "Compiling $recette_base"
prepare_individ "$recette" "$INDIVID_FOLDER"/"$recette_base";
build_pdf_page "$INDIVID_FOLDER"/"$recette_base" "$INDIVID_FOLDER_PDF"
rm $(basename "$recette_base" .tex).*
done
done
;;
individ-clean)
echo "Cleaning the individual recipes";
rm -f "$INDIVID_FOLDER"/*
;;
syntax-check)
echo "Checking syntax for books"
syntax_check $CHAPTERS
syntax_check $CHAPTERS2
;;
*)
echo "Build target '$TARGET_BUILD' not recognized" >&2;
echo "Recognized options are : $0 [all | clean | individ | individ-clean | syntax-check]"
echo "If no build target is provided, 'all' is assumed"
return 10;
;;
esac