name: Update translations

on:
  schedule:
    - cron:  '0 */6 * * *'  # https://crontab.guru/#0_*/12_*_*_*
  workflow_dispatch: # allow manual trigger
  push:
    branches:
      - master
    paths-ignore:
      - '**.ts'

jobs:
  lock-weblate:
    runs-on: ubuntu-latest
    container:
      image: weblate/wlc
      options: "-u root:root"
    steps:
      - name: Lock Weblate
        run: |
          mkdir -p ~/.config
          echo "${{ secrets.WEBLATE_CONFIG }}" > ~/.config/weblate
          wlc commit "polymc/polymc"
          wlc lock "polymc/polymc"

  update:
    needs: lock-weblate
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository.

      - name: Prepare
        run: |
          sudo apt-get update
          sudo apt-get install -y qttools5-dev-tools gettext
          git config --local user.email "gitbot@scrumplex.net"
          git config --local user.name "Translations Bot"

      - name: Ensure updated translations
        run: |
          # pull latest translations manually to avoid conflicts
          git pull  # maybe Weblate already pushed their changes
          git remote add weblate https://hosted.weblate.org/git/polymc/polymc
          git remote update
          git merge weblate/master

      - name: Update translations
        run: |
          git clone https://github.com/PolyMC/PolyMC.git src

          ./update.sh

      - name: Commit files
        run: |
          git commit -m "Update source strings" -a || true

      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}

  unlock-weblate:
    needs: update
    if: ${{ always() }}  # ensure that this runs, even if something failed
    runs-on: ubuntu-latest
    container:
      image: weblate/wlc
      options: "-u root:root"
    steps:
      - name: Unlock Weblate
        run: |
          mkdir -p ~/.config
          echo "${{ secrets.WEBLATE_CONFIG }}" > ~/.config/weblate
          wlc pull "polymc/polymc"
          wlc unlock "polymc/polymc"

