---
name: rssguard

on:
  push:
    branches: ["*"]
    tags: ["*"]

jobs:
  check-build-script:
    name: check-build-script
    runs-on: ubuntu-latest
    steps:
      - name: Checkout source code
        uses: actions/checkout@v3

      - name: Check script syntax
        run: bash -n ./resources/scripts/github-actions/build-linux-mac.sh

      - name: Show shellcheck version
        run: shellcheck --version

      - name: Run shellcheck
        run: shellcheck --color=always ./resources/scripts/github-actions/build-linux-mac.sh

  build-rssguard:
    needs: check-build-script
    name: "${{ matrix.os }}; webengine = ${{ matrix.use_webengine }}; qt5 = ${{ matrix.use_qt5 }}"
    runs-on: "${{ matrix.os }}"
    strategy:
      matrix:
        os: [windows-2019, ubuntu-18.04, macos-10.15]
        use_webengine: ["ON", "OFF"]
        use_qt5: ["ON", "OFF"]
        include:
          - os: windows-2019
            script_name: .\resources\scripts\github-actions\build-windows.ps1
          - os: ubuntu-18.04
            script_name: ./resources/scripts/github-actions/build-linux-mac.sh
          - os: macos-10.15
            script_name: ./resources/scripts/github-actions/build-linux-mac.sh
        exclude:
          - os: ubuntu-18.04
            use_qt5: "OFF"
          - os: macos-10.15
            use_qt5: "ON"
    steps:         
      - name: Checkout source code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
          submodules: true
         
      - name: Delete old development binaries
        uses: mknejp/delete-release-assets@v1
        if: strategy.job-index == 0
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          tag: devbuild
          fail-if-no-assets: false
          fail-if-no-release: false
          assets: | 
            *.AppImage
            *.dmg
            *.exe
            *.7z
            
      - name: Update "devbuild" tag
        uses: richardsimko/update-tag@v1.0.5
        if: strategy.job-index == 0
        with:
          tag_name: devbuild
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
         
      - name: Prepare environment and compile application
        run: ${{ matrix.script_name }} "${{ matrix.os }}" "${{ matrix.use_webengine }}" "${{ matrix.use_qt5 }}"
        env:
          GMAIL_CLIENT_ID: ${{ secrets.GMAIL_CLIENT_ID }}
          GMAIL_CLIENT_SECRET: ${{ secrets.GMAIL_CLIENT_SECRET }}
          FEEDLY_CLIENT_ID: ${{ secrets.FEEDLY_CLIENT_ID }}
          FEEDLY_CLIENT_SECRET: ${{ secrets.FEEDLY_CLIENT_SECRET }}

      - name: Release development binaries
        uses: softprops/action-gh-release@v1
        with:
          prerelease: true
          name: "Development builds"
          tag_name: "devbuild"
          files: |
            ./rssguard-build/rssguard-*win*.exe
            ./rssguard-build/rssguard-*win*.7z
            ./rssguard-build/rssguard-*mac64.dmg
            ./rssguard-build/rssguard-*linux64.AppImage
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          
      - name: Release stable binaries
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/')
        with:
          prerelease: false
          files: |
            ./rssguard-build/rssguard-*win*.exe
            ./rssguard-build/rssguard-*win*.7z
            ./rssguard-build/rssguard-*mac64.dmg
            ./rssguard-build/rssguard-*linux64.AppImage
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
