#!/bin/bash

BASENAME=referentiel-competences
NBPASS=3 # il faut 3 passes de compilation

usage () {
    echo "Usage: $0 [-f|--fast] help|draft|pub|clean"
    echo "  draft: generates draft version, including toc and comments"
    echo "  pub:   generates publication ready version"
    echo "  clean: cleanup all (non versioned) tex files"
    echo "--fast (or -f) option just make one compilation pass instead of $NBPASS"

    exit 1
}

if [ "$1" == "-f" ] || [ "$1" == "--fast" ]; then
    NBPASS1=1
    shift
else
    NBPASS1=$NBPASS
fi

if [ $# != 1 ] || [ "$1" == "help" ]; then
    usage    
fi

if [ "$1" = "clean" ]; then
    /bin/rm -f "$BASENAME"*.aux "$BASENAME"*.pdf "$BASENAME"*.toc "$BASENAME"*.snm "$BASENAME"*.nav "$BASENAME"*.log
    exit 0
fi

if [ "$1" != "pub" ] && [ "$1" != "draft" ]
then
    usage
fi

build () {
    for (( i=1; i<=$NBPASS1; i++ ))
    do
        echo "----------------- pass $i"
        lualatex -jobname "$1"-"$2" '\def\'$2'{}\input{comp}'
    done
}

# Some git version information for the draft doc

if command -v COMMAND &> /dev/null
then
    echo -n "commit "$(git rev-parse --short=10 HEAD) > docinfo.txt
    git diff-index --quiet HEAD --
    if [ $? != 0 ]
    then
        echo " (modifié)" >> docinfo.txt
        HAS_UNCOMMITED_CHANGES="Warning: modifications locales non commitées."
    fi
else
    echo "Warning: git is not installed"
fi

build "$BASENAME" "$1"

[ -z "$HAS_UNCOMMITED_CHANGES" ] || echo "$HAS_UNCOMMITED_CHANGES"



