name: Build branch

on:
  release:
    types: [created]
  push:
    branches:
      - master
    paths-ignore:
      - "**.md"
      - "**.yml"
      - "**.yaml"
  workflow_dispatch:

jobs:
  build_esp32:
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        environment:
          - out: adafruit
            env: "huzzah32-featherwing-24 -e huzzah32-featherwing-35 -e huzzah32-featherwing-24-v2 -e huzzah32-featherwing-35-v2 -e huzzah32-v2-featherwing-24-v2 -e huzzah32-v2-featherwing-35-v2"
          - out: az-touch
            env: "az-touch-mod-esp32_ili9341_4MB -e az-touch-mod-esp32_ili9341_8MB"
          - env: d1-mini-esp32_ili9341
            out: d1-mini-esp32
          - out: d1-r32-espduino32
            env: "d1-r32-waveshare_ili9486 -e d1-r32-unoshield_ili9341_adc -e d1-r32-unoshield_ili9486_adc"
          - out: dustinwatts
            env: "freetouchdeck_4MB -e freetouchdeck_8MB -e esp32-touchdown"
          - out: elecrow
            env: "esp32-terminal-rgb_16MB -e esp32-terminal-spi_16MB"
          - out: globalsecurity
            env: gs-t3e_16MB
          - out: lanbon
            env: lanbon_l8
          - out: lilygo-ttgo
            env: "lilygo-lily-pi_st7796 -e lilygo-lily-pi_ili9481 -e ttgo-t7-v1_5_ili9341_4MB -e ttgo-t7-v1_5_ili9341_16MB"
          - out: lolin
            env: lolin-d32-pro_ili9341
          - out: m5stack
            env: m5stack-core2
          - out: makerfabs
            env: "makerfabs-tft35-cap_4MB -e makerfabs-tft35-cap_16MB -e makerfabs-s3-tft35-spi -e makerfabs-s3-tft40-rgb -e makerfabs-s3-tft43-rgb"
          - out: panlee
            env: "panlee-zw3d95ce01s-ar-4848_16MB -e panlee-zw3d95ce01s-ur-4848_16MB -e panlee-zw3d95ce01s-tr-4848_16MB"
          - out: seeed-studios
            env: "sensecap-indicator-d1_8MB"
          - out: sunton
            env: "esp32-2432s028r_4MB -e esp32-3248s035c_4MB -e esp32-3248s035r_4MB -e sunton-4827s043c_16MB -e sunton-8048s043c_16MB -e sunton-8048s050c_16MB -e sunton-8048s070c_16MB"
          - out: waveshare
            env: "esp32-one_ili9486 -e esp32-one_st7796"
          - out: wireless-tag
            env: "wt32-sc01_4MB -e wt32-sc01_16MB -e wt-86-32-3zw1 -e wt32-sc01-plus_8MB -e wt32-sc01-plus_16MB"
          - out: yeacreate
            env: yeacreate-nscreen32
          - out: guition
            env: esp32-s3-4848S040

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: "true"
      - name: Cache pip
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-
      - name: Cache PlatformIO
        uses: actions/cache@v4
        with:
          path: ~/.platformio
          key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.x"
      - name: Install PlatformIO
        run: |
          python -m pip install --upgrade pip
          pip install --upgrade platformio
      - name: Install Setuptools
        run: |
          pip install --upgrade setuptools
      - name: Enable ESP32 platforms from platformio_override-template.ini
        run: |
          sed 's/; user_setups\/esp32/user_setups\/esp32/g' platformio_override-template.ini > platformio_override.ini
      - name: List all files in current folder
        run: |
          ls -la
      - name: Cat platformio_override.ini
        run: |
          cat platformio_override.ini
      - name: Run PlatformIO
        run: pio run -e ${{ matrix.environment.env }}
      - name: Upload output file
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.environment.out }}
          path: build_output/firmware/*.bin
      - name: Create release and upload firmware
        if: github.ref != 'refs/heads/master'
        run: |
          set -x
          assets=()
          for asset in build_output/firmware/*.bin; do
            assets+=("-a" "$asset")
          done
          tag_name="${GITHUB_REF##*/}"
          hub release edit "${assets[@]}" -m "$tag_name" "$tag_name"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    #- name: Create release and upload firmware
    #  run: |
    #    set -x
    #    assets=()
    #    for asset in build_output/firmware/*.bin; do
    #      assets+=("-a" "$asset")
    #    done
    #    tag_name="${GITHUB_REF##*/}"
    #    hub release create "${assets[@]}" -m "$tag_name" "$tag_name"
    #  env:
    #    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  linux_build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: "true"
      - name: Cache pip
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-
      - name: Cache PlatformIO
        uses: actions/cache@v4
        with:
          path: ~/.platformio
          key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.x"
      - name: Install PlatformIO
        run: |
          python -m pip install --upgrade pip
          pip install --upgrade platformio
      - name: Enable Linux platform from platformio_override.ini
        run: |
          sed 's/; user_setups\/linux/user_setups\/linux/g' platformio_override-template.ini > platformio_override.ini
          mkdir -p .pio/libdeps/linux_sdl/paho/src
      - name: Install SDL2 library
        run: |
          sudo apt-get update
          sudo apt-get install libsdl2-dev
      - name: List all files in current folder
        run: |
          ls -la
      - name: Cat platformio_override.ini
        run: |
          cat platformio_override.ini
      - name: Enable Linux platform from platformio_override.ini
        run: |
          sed -i 's/; user_setups\/linux/user_setups\/linux/g' platformio_override.ini
          mkdir -p .pio/libdeps/linux_sdl/paho/src
      - name: Install SDL2 library
        run: |
          sudo apt-get update
          sudo apt-get install libsdl2-dev
      - name: Run PlatformIO
        run: pio run -e linux_sdl
