name: PlatformIO CI

permissions:
    pages: write
    id-token: write

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        with:
          path: |
            ~/.cache/pip
            ~/.platformio/.cache
          key: ${{ runner.os }}-pio

      - uses: actions/setup-python@v4
        with:
          python-version: '3.9'

      - name: Install PlatformIO Core
        run: pip install --upgrade platformio esptool

      - name: Build PlatformIO Project
        run: |
          cd CYD-Klipper
          pio run

      - name: Make output dir
        run: |
          mkdir -p output
      
      - name: Build Binary
        run: |
          cp ./CYD-Klipper/.pio/build/esp32dev/bootloader.bin output
          cp ./CYD-Klipper/.pio/build/esp32dev/partitions.bin output
          cp ./CYD-Klipper/.pio/build/esp32dev/firmware.bin output
          cp ~/.platformio/packages/framework-arduinoespressif32/tools/partitions/boot_app0.bin output
          python3 -m esptool --chip esp32 merge_bin -o ./output/merged-firmware.bin --flash_mode dio --flash_freq 40m --flash_size 4MB 0x1000 ./output/bootloader.bin 0x8000 ./output/partitions.bin 0xe000 ./output/boot_app0.bin 0x10000 ./output/firmware.bin
          cp -r ./output ./_site

      - name: Upload artifact 
        uses: actions/upload-artifact@v3
        with:
          name: firmware
          path: ./output 

      - name: Upload GitHub Page Artifact
        uses: actions/upload-pages-artifact@v2

  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build
    if: github.event_name == 'push' && github.ref == 'refs/heads/master'
    steps:
      - name: Print GitHub event name
        run: |
          echo "${{ github.event_name }}"

      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v2
  