name: Build and Deploy docs

on:
  push:
    branches:
      - main
    paths:
      - "docs/**"
  pull_request:
    branches:
      - '**'
  workflow_dispatch: {}
  schedule:
    - cron: "0 0 * * *"

env:
  TX_TOKEN: ${{ secrets.TX_TOKEN }}

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Set up Python 3.9
      uses: actions/setup-python@v4
      with:
        python-version: 3.9

    - name: Install dependencies
      run: |
        sudo apt-get install -y transifex-client gettext
        python -m pip install --upgrade pip
        cd docs
        pip install pipenv
        pipenv install --system

    - name: Create temporary directory to store the CLI
      run: mkdir /tmp/tx
      shell: bash

    - name: Install transifex CLI
      run: curl -o- https://raw.githubusercontent.com/transifex/cli/master/install.sh | bash
      working-directory: /tmp/tx
      shell: bash

    - name: Push translations
      if: ${{ github.ref == 'refs/heads/main' }}
      run: |
        cd docs
        set -e
        make gettext
        if [[ -z "${TX_TOKEN}" ]]; then
          echo "TX_TOKEN not set, skip tx pull"
        else
          /tmp/tx/tx --token "${TX_TOKEN}" push -s
        fi
        make clean

    - name: Pull translations
      run: |
        cd docs
        set -e
        if [[ -z "${TX_TOKEN}" ]]; then
          echo "TX_TOKEN not set, skip tx pull"
        else
          /tmp/tx/tx --token "${TX_TOKEN}" pull -a
        fi

    - name: Build
      run: |
        cd docs
        make html
        touch ./build/html/.nojekyll

    - name: Deploy 🚀
      uses: JamesIves/github-pages-deploy-action@v4
      if: ${{ github.ref == 'refs/heads/main' }}
      with:
        branch: gh-pages # The branch the action should deploy to.
        folder: docs/build/html # The folder the action should deploy.
