#!/bin/bash
#set -x

# Available manuals
manuals='NEMO SI3 TOP'

# Initialization of the options
x_c='' ; x_p='' ; x_r=''

# Choice of the options ---
while getopts chpr: option; do
        case $option in
                ('h') cat <<EOF
Usage:
------
./manual_build.sh [-c] [-p] [-r version ] manual
Mandatory
          Chose one model manual among ${manuals} or all (if not provided compile "NEMO")
Optional
   -c     Clean up previous builds of the manual(s)
   -p     Produce manual with figures and disable draft watermark
   -r     Specify the release version number of the manual(s) instead of the branch name
EOF
                                 exit 0 ;;
            ('c') x_c=0                 ;;
            ('p') x_p=0                 ;;
            ('r') x_r=${OPTARG}         ;;
        esac
done
shift $(( $OPTIND - 1 ));

CLEAN=${x_c}
DRAFT=${x_p}
VER_NUM=${x_r}
CMP_NAM=$1

# Default case for Linux sed, just use "-i"
sedi=(-i)
case "$(uname)" in
    # For macOS, use two parameters
    Darwin*)
	isgnu=$( sed --version 2>/dev/null )
	[ $( echo $? ) -eq 0 ] && isgnu=$( echo $isgnu | grep -ic gnu ) || isgnu=0
	[ $isgnu -eq 0 ] && edi=(-i "")
	;;
esac

## Initialisation
##---------------

# Model
if [ "${CMP_NAM}" == 'all' ] ; then
    models="$manuals"
elif [ "${CMP_NAM}" ==  '' ] ; then
    models='NEMO'
elif [[ "${manuals}" == *"${CMP_NAM}"* ]] ; then
    models="${CMP_NAM}"
else
    echo "No entry for manual ${CMP_NAM}"
    exit
fi

# Version
version=`git symbolic-ref -q --short HEAD || git describe --tags --exact-match || echo "orphan"`
if [ "x${VER_NUM}" != 'x' ] ; then
    version=${VER_NUM}
fi

# Draft mode
ifdraft=1 ; draft_opt="using draft mode"
if [ "x${DRAFT}" != 'x' ] ; then
    ifdraft=0 ; draft_opt=""
fi

echo "Compiling ${models} manual(s) at version ${version} ${draft_opt} \n"

# Set version
version=`echo $version | sed -e 's;_;\\\\\\\_;g'`
prefile="latex/global/preamble.tex"
sed "${sedi[@]}" -e "s;\\\def\\\ver.*;\\\def\\\ver{$version};" ${prefile}

# Set draft mode
docfile="latex/global/document.tex"
pkgfile="latex/global/packages.tex"

draft_opt=`grep draft $docfile`
pkg_dft=`grep draftwatermark $pkgfile | sed -e "s;%;;g"`
if [ $ifdraft == 1 ] && [ "x$draft_opt" == "x" ] ; then
   sed "${sedi[@]}" -e "s;,abstract;,abstract,draft;" $docfile
   sed "${sedi[@]}" -e "s;.*draftwatermark.*;\\$pkg_dft;" $pkgfile
elif [ $ifdraft == 0 ] && [ "x$draft_opt" != "x" ] ; then
   sed "${sedi[@]}" -e "s;abstract,draft;abstract;" $docfile
   sed "${sedi[@]}" -e "s;.*draftwatermark.*;%\\$pkg_dft;" $pkgfile
fi

# Echo current font path
fontawesome=`grep defaultfontfeatures latex/global/packages.tex | sed -e 's/.*=\(.*\)}/\1/'`
if [ -d $fontawesome ] ; then
    echo "Fontawesome path is $fontawesome"
else
    echo
    echo "ERROR with fontawesome path : "
    echo "  $fontawesome does not exist"
    echo "  Define the proper path to the fonts/opentype/public/fontawesome directory in latex/global/packages.tex"
    echo
    exit 1
fi

# Source shared functions
. tools/shr_func.sh

## Check dependancies
##-------------------

## LaTeX installation, find latexmk should be enough
[ -z "$( which latexmk )" ] && { echo 'latexmk not installed => QUIT'; exit 2; }

## Pygments package for syntax highlighting of source code (namelists & snippets)
[ -n "$( ./tools/check_pkg.py pygments )" ] && { echo 'Python pygments is missing => QUIT'; exit 2; }

## Loop on the models
##-------------------

for model in $models; do
    echo "Start compiling manual for $model"
    if [ "x${CLEAN}" != 'x' ] ; then clean $model ; fi
    build $model
    printf "\t¤ End of building run\n"
    echo
done

if [ ! -f "${model}_manual.pdf" ] ; then
    echo "${model}_manual.pdf not created." 
    exit 1
else
    exit 0
fi