#!/bin/bash
# Copyright (c) 2021 Graphcore Ltd. All rights reserved.
set -e

# Parameters
DOC_SOURCE_DIR="$1"
DOC_TARGET_HTML="$2"
DOC_TARGET_PDF="$3"
DOC_TARGET_LATEX_SRC="$3_latex_src"
export DOC_TITLE="$4" # exported as it's read by sphinx_resources
export VERSION="$5"   # exported as it's read by sphinx_resources

# Run sphinx build to get the HTML output.
echo "Generating HTML: ${DOC_TARGET_HTML}..."
sphinx-build \
    -b html \
    -c "@GRAPHCORE_SPHINX_RESOURCES_DIR@" \
    -D "breathe_projects.project=@PROJECT_BINARY_DIR@/docs/doxygen/xml" \
    ${DOC_SOURCE_DIR} \
    ${DOC_TARGET_HTML}

# Generate LaTeX sources from which we can generate the PDF output.
echo "Generating LaTeX sources: ${DOC_TARGET_LATEX_SRC}..."
sphinx-build \
    -b latex \
    -c "@GRAPHCORE_SPHINX_RESOURCES_DIR@" \
    -D "breathe_projects.project=@PROJECT_BINARY_DIR@/docs/doxygen/xml" \
    ${DOC_SOURCE_DIR} \
    ${DOC_TARGET_LATEX_SRC}

# Make the PDF from LaTeX sources.
echo "Generating PDF: ${DOC_TARGET_PDF}..."
cd ${DOC_TARGET_LATEX_SRC} && make LATEXMKOPTS='-silent -f'
mv "${DOC_TARGET_LATEX_SRC}/doc.pdf" ${DOC_TARGET_PDF}
