name: "Version check for NEW release"
run-name: " Version check for NEW release ${{ inputs.DEPLOY_TARGET }} by @${{ github.ACTOR }}"

on:
  workflow_dispatch:
  push:
    branches:
      - master
  schedule:
    - cron: "*/30 * * * *"

permissions:
  contents: write

jobs:
  pull-request:
    concurrency:
      group: check-for-new-versions
      cancel-in-progress: false
    runs-on: ubuntu-latest
    env:
      REPO_UNLEASHED: ${{ vars.REPO_UNLEASHED }}
      CURRENT_TAG: ${{ vars.FIRMWARE_VERSION }}
      REMOTE_TAG_INFO: ''
      RELEASE_VERSION: ${{ vars.RELEASE_VERSION }}
      FIRMWARE_VERSION: ${{ vars.FIRMWARE_VERSION }}
      REPO_SELF: ${{ vars.REPO_SELF }}
      CHECKOUT_DIR: "firmware"
      RELEASE_TYPE: 0
    steps:
      - name: Copy Repo Files
        uses: actions/checkout@v4
        with:
          repository: "${{ env.REPO_SELF }}"
          clean: "true"
          submodules: "true"
          token: ${{ secrets.FLIPPER_TOKEN }}
      - name: Check firmware release
        shell: pwsh
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          ./.github/check-version.ps1 '${{ env.FIRMWARE_VERSION }}' '${{ env.RELEASE_VERSION }}' '${{ env.REPO_SELF }}' '${{ env.REPO_UNLEASHED }}' | %{ Write-Output($_) >> $Env:GITHUB_ENV }
      - name: Print env values
        run: env

      - name: Create release if necessary
        if: ${{ success() && (env.RELEASE_TYPE == 2  || env.RELEASE_TYPE == 1 )}}
        uses: ./.github/actions/make-release
        with:
          gh_token: ${{ secrets.FLIPPER_TOKEN }}
          release_type: ${{ env.RELEASE_TYPE }}
          release_version: ${{ env.RELEASE_VERSION }}
          firmware_version: ${{ env.REMOTE_TAG_INFO }}
          repo_self: ${{ env.REPO_SELF }}
          owner: ${{ github.repository_owner }}
          current_tag: ${{ env.CURRENT_TAG }}
          remote_tag_info: ${{ env.REMOTE_TAG_INFO }}

      - name: Current settings in repo invalid. Changing
        if: ${{ success() && env.RELEASE_TYPE > 2 }}
        env:
          GITHUB_TOKEN: ${{ secrets.FLIPPER_TOKEN }}
          OWNER: ${{ github.repository_owner }}
        run: |
          gh variable set RELEASE_VERSION -b ${{ env.RELEASE_VERSION }} -R ${{ env.REPO_SELF }}

# EOF
