name: "FujiNet CI"

on:
  push:
    # Do not build tags. Do not build release branch
    branches:
      - "**"
      - "!release"
    tags:
      - "!**"
  pull_request:
    # Do not build release branch
    branches:
      - "**"
      - "!release"

jobs:
  tagged-release:
    name: "PlatformIO CI"
    runs-on: "ubuntu-latest"
    strategy:
      matrix:
        target-platform: [ATARI, ATARI-esp32-s3-wroom-1-n16r8, ADAM, APPLE, IEC-LOLIN-D32, IEC-NUGGET, COCO]

    steps:
    - name: Checkout Source
      uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - 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.12.1'

    - name: Install PlatformIO
      run: |
        python -m pip install --upgrade pip
        pip install setuptools
        pip install --upgrade platformio
        pip install Jinja2
        pip install pyyaml

    - name: Show python version
      run: python --version

    - name: Show pio system info
      run: pio system info

    - name: Show pio location
      run: pip show platformio

    - name: Create PlatformIO INI for Build
      run: cd "$GITHUB_WORKSPACE" && /usr/bin/bash ./build.sh -l "$GITHUB_WORKSPACE"/.github/workflows/platformio.release-${{ matrix.target-platform }}.ini -i "$GITHUB_WORKSPACE"/platformio-generated.ini

    - name: Show platformio.ini
      run: cat "$GITHUB_WORKSPACE"/platformio-generated.ini

    - name: Build release
      run: cd "$GITHUB_WORKSPACE" && /usr/bin/bash ./build.sh -z -l "$GITHUB_WORKSPACE"/.github/workflows/platformio.release-${{ matrix.target-platform }}.ini -i "$GITHUB_WORKSPACE"/platformio-generated.ini

    - name: Upload firmware zip for ${{ matrix.target-platform }}
      uses: actions/upload-artifact@v4
      with:
        name: ${{ matrix.target-platform }}
        path: firmware/**
