name: Web Installer build and deploy

on:
  workflow_call:
    inputs:
      release-name:
        description: 'Name of the Web Installer release'
        required: true
        type: string

jobs:
  determine-projects:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.11'

    - name: Identify projects and version
      id: projects
      run: |
        echo "envs=$(tools/installer_envs.py)" >> $GITHUB_OUTPUT
        echo "version=$(tools/installer_version.py)" >> $GITHUB_OUTPUT

    outputs:
      envs: ${{ steps.projects.outputs.envs }}
      version: ${{ steps.projects.outputs.version }}

  build-project:
    runs-on: ubuntu-latest

    needs: [determine-projects]

    strategy:
      matrix:
        envname: ${{ fromJson(needs.determine-projects.outputs.envs) }}

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Install PlatformIO
        run: |
          curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py -o get-platformio.py
          python get-platformio.py
          echo "/home/runner/.platformio/penv/bin" >> $GITHUB_PATH

      - name: Copy secrets and clear SSID
        run: |
          grep -v "^#define cszSSID" include/secrets.example.h > include/secrets.h
          echo '#define cszSSID ""' >> include/secrets.h

      - name: "Build web installer project: ${{ matrix.envname }}"
        run: python tools/installer_buildenv.py ${{ matrix.envname }} ${{ needs.determine-projects.outputs.version }}

      - name: Store build output
        uses: actions/upload-artifact@v4
        with:
          overwrite: true
          path: build
          name: webinstaller-project-${{ matrix.envname }}

  compose-and-publish-installer:
    runs-on: ubuntu-latest

    needs: [build-project]

    steps:
    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.11'

    - uses: actions/checkout@v4

    - run: mkdir -p build

    - name: Retrieve composer input
      uses: actions/download-artifact@v4
      with:
        path: build
        merge-multiple: true
        pattern: webinstaller-project-*

    - name: Compose web installer
      run: |
        python tools/installer_compose.py '${{ inputs.release-name }}'
        touch WebInstaller/.nojekyll

    - name: Push to GitHub Pages
      uses: JamesIves/github-pages-deploy-action@v4
      with:
        branch: pages
        folder: WebInstaller
