name: Build ESP32-C3

on:
  workflow_call:
    inputs:
      board:
        required: true
        type: string

permissions: read-all

jobs:
  build-esp32-c3:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Build base
        id: base
        uses: ./.github/actions/setup-base

      - name: Pull web ui
        uses: dsaltares/fetch-gh-release-asset@a40c8b4a0471f9ab81bdf73a010f74cc51476ad4
        with:
          repo: meshtastic/web
          file: build.tar
          target: build.tar
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Unpack web ui
        run: |
          tar -xf build.tar -C data/static
          rm build.tar
      - name: Remove debug flags for release
        if: ${{ github.event_name == 'workflow_dispatch' }}
        run: |
          sed -i '/DDEBUG_HEAP/d' ./arch/esp32/esp32.ini
          sed -i '/DDEBUG_HEAP/d' ./arch/esp32/esp32s2.ini
          sed -i '/DDEBUG_HEAP/d' ./arch/esp32/esp32s3.ini
          sed -i '/DDEBUG_HEAP/d' ./arch/esp32/esp32c3.ini
      - name: Build ESP32
        run: bin/build-esp32.sh ${{ inputs.board }}

      - name: Pull OTA Firmware
        uses: dsaltares/fetch-gh-release-asset@a40c8b4a0471f9ab81bdf73a010f74cc51476ad4
        with:
          repo: meshtastic/firmware-ota
          file: firmware-c3.bin
          target: release/bleota-c3.bin
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Get release version string
        shell: bash
        run: echo "version=$(./bin/buildinfo.py long)" >> $GITHUB_OUTPUT
        id: version

      - name: Store binaries as an artifact
        uses: actions/upload-artifact@v3
        with:
          name: firmware-${{ inputs.board }}-${{ steps.version.outputs.version }}.zip
          path: |
            release/*.bin
            release/*.elf
