name: OGX-Mini
on:
  push:
  schedule:
    - cron: "0 6 * * *"

jobs:
  ogx_mini:
    name: OGX-Mini
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target: [PI_PICO, PI_PICOW]
        project_name: [OGX-Mini]

    steps:

      - name: Compare Versions
        run: |
          GITHUB_REPO="wiredopposite/OGX-Mini" >> $GITEA_ENV
          GITHUB_VERSION=$(curl -s "https://api.github.com/repos/$GITHUB_REPO/releases/latest" | jq -r .tag_name)
          echo "GitHub Latest Version: $GITHUB_VERSION"
          echo "GITHUB_VERSION=$GITHUB_VERSION" >> $GITEA_ENV

          GITEA_URL="https://git.sthope.dev"
          GITEA_REPO="sthope/OGX-Mini"
          GITEA_VERSION=$(curl -s "$GITEA_URL/api/v1/repos/$GITEA_REPO/releases/latest" -H 'accept: application/json' | jq -r '.tag_name')
          echo "Gitea Latest Version: $GITEA_VERSION"
          echo "GITEA_VERSION=$GITEA_VERSION" >> $GITEA_ENV

          if [ "$(printf '%s\n' "$GITEA_VERSION" "$GITHUB_VERSION" | sort -V | head -n1)" = "$GITHUB_VERSION" ]; then
            echo "❌❌GitHub release is older or the same. Stopping workflow.❌❌"
            #exit 1
            PB_TOKEN="${{ secrets.PB_TOKEN }}"
            PB_TITLE="${{ matrix.project_name }}"
            curl -u ${PB_TOKEN}: -X POST https://api.pushbullet.com/v2/pushes --header 'Content-Type: application/json' --data-binary '{"type": "note", "title": "${PB_TITLE}", "body": "${PB_TITLE} new: $GITHUB_VERSION" }'
          else
            echo "✅✅GitHub release is newer. Continuing workflow.✅✅"
            PB_TOKEN="${{ secrets.PB_TOKEN }}"
            curl -u ${PB_TOKEN}: -X POST https://api.pushbullet.com/v2/pushes \
              --header 'Content-Type: application/json' \
              --data-binary '{"type": "note", \
              "title": "${{ matrix.project_name }}", \
              "body": "${{ matrix.project_name }} new: $GITHUB_VERSION" }'
          fi

      - name: Installing dependencies & Cloning repository
        run: |
          apt-get update
          apt-get install -y git python3 python3-pip cmake ninja-build gcc-arm-none-eabi
          git clone --recursive https://github.com/$GITHUB_REPO.git ${{ gitea.workspace }}
          cd ${{ gitea.workspace }}
          git fetch origin --tags --force
          git submodule update --init --recursive

      - name: Build the firmware
        run: |
          cd ${{ gitea.workspace }}/Firmware/RP2040
          cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DOGXM_BOARD=STRING=${{ matrix.target }} -DMAX_GAMEPADS=1        
          cmake --build build

      - name: Move Files
        run: |
          mkdir -p ${{ gitea.workspace }}/output
          mv ${{ gitea.workspace }}/Firmware/RP2040/build/*.uf2 ${{ gitea.workspace }}/output

      - name: Upload artifacts
        uses: actions/upload-artifact@v3
        with:
          name: ${{ matrix.target }}.uf2
          path: |
            ${{ gitea.workspace }}/output/${{ matrix.target }}.uf2

      - name: Upload Release
        uses: akkuman/gitea-release-action@v1
        with:
          name: "${{ env.GITHUB_VERSION }}"
          tag_name: "${{ env.GITHUB_VERSION }}"
          prerelease: false
          files: |-
            ${{ gitea.workspace }}/output/${{ matrix.target }}.uf2