name: Build thesis document
on:
  push:
    branches: [ main ]
    paths:
      - ".github/workflows/build-sphinx-thesis.yml"
      - "thesis/**"
      - "backend/**"
jobs:
  html:
    name: "Build HTML document"
    runs-on: ubuntu-latest
    steps:
      - name: "Checkout repository"
        uses: actions/checkout@v2
        with:
          lfs: true
      - name: "Checkout LFS objects"
        run: git lfs checkout
      - name: "Setup Python"
        uses: actions/setup-python@v2
        with:
          python-version: 3.9
      - name: "Setup Poetry"
        uses: abatilo/actions-poetry@v2.0.0
        with:
          poetry-version: 1.1.11
      - name: "Install backend dependencies"
        working-directory: backend/
        run: poetry install --no-interaction
      - name: "Find Poetry Python environment"
        working-directory: backend/
        run: echo "pythonLocation=$(poetry env list --full-path | cut -f1 -d' ')/bin" >> $GITHUB_ENV
      - name: "Build HTML document with Sphinx"
        working-directory: thesis/
        run: |
          source $pythonLocation/activate
          make html
      - name: "Create a gzipped tarball of the build"
        working-directory: ${{ github.workspace }}
        run: tar -cvzf thesis/build/thesis.html.tar.gz thesis/build/html
      - name: "Upload build artifact"
        uses: actions/upload-artifact@v2
        with:
          name: "thesis.html.tar.gz"
          path: "thesis/build/thesis.html.tar.gz"
      - name: "Publish build to GitHub Pages"
        uses: peaceiris/actions-gh-pages@v3.7.3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: thesis/build/html/
          enable_jekyll: false
  latexpdf:
    name: "Build PDF document"
    runs-on: ubuntu-latest
    steps:
      - name: "Update apt repositories"
        run: sudo apt-get update -y
      - name: "Checkout repository"
        uses: actions/checkout@v2
        with:
          lfs: true
      - name: "Checkout LFS objects"
        run: git lfs checkout
      - name: "Setup Python"
        uses: actions/setup-python@v2
        with:
          python-version: 3.9
      - name: "Setup Poetry"
        uses: abatilo/actions-poetry@v2.0.0
        with:
          poetry-version: 1.1.11
      - name: "Install LaTeX packages"
        run: sudo apt-get install -y latexmk texlive-latex-recommended texlive-latex-extra texlive-fonts-recommended texlive-luatex fonts-ebgaramond fonts-ebgaramond-extra fonts-firacode xindy
      - name: "Install backend dependencies"
        working-directory: backend/
        run: poetry install --no-interaction
      - name: "Find Poetry Python environment"
        working-directory: backend/
        run: echo "pythonLocation=$(poetry env list --full-path | cut -f1 -d' ')/bin" >> $GITHUB_ENV
      - name: "Build LaTeX document with Sphinx"
        working-directory: thesis/
        run: |
          source $pythonLocation/activate
          make latex
      - name: "Remove numbering from subsections"
        working-directory: thesis/build/latex/
        run: sed --regexp-extended --in-place 's/[\]subsection[{]/\\subsection*{/g' progettazioneesviluppodisophonapplicativocloudasupportodellaricerca.tex
      - name: "Remove numbering from subsubsections"
        working-directory: thesis/build/latex/
        run: sed --regexp-extended --in-place 's/[\]subsubsection[{]/\\subsubsection*{/g' progettazioneesviluppodisophonapplicativocloudasupportodellaricerca.tex
      - name: "Remove numbering from paragraphs"
        working-directory: thesis/build/latex/
        run: sed --regexp-extended --in-place 's/[\]paragraph[{]/\\paragraph*{/g' progettazioneesviluppodisophonapplicativocloudasupportodellaricerca.tex
      - name: "Remove numbering from subparagraphs"
        working-directory: thesis/build/latex/
        run: sed --regexp-extended --in-place 's/[\]subparagraph[{]/\\subparagraph*{/g' progettazioneesviluppodisophonapplicativocloudasupportodellaricerca.tex
      - name: "Add appendix macro"
        working-directory: thesis/build/latex/
        run: sed --regexp-extended --in-place 's/[\]chapter[{]Installazione di Sophon[}]/\\appendix\\chapter{Installazione di Sophon}/g' progettazioneesviluppodisophonapplicativocloudasupportodellaricerca.tex
      - name: "Remove autogenerated indexes"
        working-directory: thesis/
        run: python latex_index_remover.py
      - name: "Build PDF document with Latexmk"
        working-directory: thesis/build/latex/
        run: make
      - name: "Upload build artifact"
        uses: actions/upload-artifact@v2
        with:
          name: "thesis.pdf"
          path: "thesis/build/latex/progettazioneesviluppodisophonapplicativocloudasupportodellaricerca.pdf"