name: Generate screenshots
on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-18.04
    steps:
      # Get OPENRNDR
      - name: Get OPENRNDR HEAD ref
        run: echo "OPENRNDR_HEAD=$(git ls-remote https://github.com/openrndr/openrndr HEAD | awk '{print $1}')" >> $GITHUB_ENV
      - uses: actions/cache@v2
        id: cache_openrndr
        with:
          path: |
            ./openrndr
          key: ${{ runner.os }}--openrndr-${{ env.OPENRNDR_HEAD }}
      - uses: actions/checkout@v2.3.4
        if: steps.cache_openrndr.outputs.cache-hit != 'true'
        with:
          repository: openrndr/openrndr
          path: ./openrndr
          ref: master
      - name: Publish OPENRNDR snapshot
        run: |
          cd ./openrndr
          ./gradlew publishToMavenLocal -Prelease.version=0.5.1-SNAPSHOT
          cd ..

      - uses: actions/checkout@v2
      - uses: actions/setup-java@v1
        with:
          java-version: 17
      - uses: openrndr/setup-opengl@v1.1
      - name: Test glxinfo
        run: |
          echo $LD_LIBRARY_PATH
          export GALLIUM_DRIVER=swr
          xvfb-run glxinfo

#      - name: Collect screenshots
#        run: xvfb-run ./gradlew collectScreenshots
      - name: Build main readme
        run: xvfb-run ./gradlew buildMainReadme
      - name: Prepare media branch
        run: |
          git config --global user.email "actions@openrndr.org"
          git config --global user.name "OPENRNDR Actions"
          git reset HEAD -- .
          (git add README.md && git commit -m "add auto-generated README" && git push origin master) || true
          (git add [a-z-]*/README.md && git commit -m "add demos to README.md" && git push origin master) || true
          git checkout --orphan media
          git reset HEAD -- .
          git add [a-z-]*/images/*.png
          git commit -m "add auto-generated media"
          git push -f origin media
