name: Launchpad PPA

permissions:
  contents: read

on:
  push:
    branches:
      - main
    tags:
      - '*'
  pull_request:
    branches:
      - main
  workflow_dispatch:

jobs:
  prepare-matrix:
    name: Prepare matrix
    runs-on: ubuntu-latest
    outputs:
      package_matrix: ${{ steps.set-matrix.outputs.package_matrix }}
    steps:
      - name: Define shared matrix values
        id: set-matrix
        run: |
          {
            echo 'package_matrix<<EOF'
            cat <<'JSON'
          {
            "include": [
              {
                "release": "24.04",
                "distro": "ubuntu",
                "codename": "noble",
                "ppa": "ppa:lemonade-team/bleeding-edge"
              },
              {
                "release": "25.10",
                "distro": "ubuntu",
                "codename": "questing",
                "ppa": "ppa:lemonade-team/bleeding-edge"
              },
              {
                "release": "26.04",
                "distro": "ubuntu",
                "codename": "resolute",
                "ppa": ""
              }
            ]
          }
          JSON
            echo 'EOF'
          } >> "$GITHUB_OUTPUT"

  package:
    name: Build
    needs: prepare-matrix
    runs-on: ubuntu-latest
    permissions:
      contents: read
    strategy:
      matrix: ${{ fromJSON(needs.prepare-matrix.outputs.package_matrix) }}
    container:
      image: ghcr.io/lemonade-sdk/lemonade/build-environment:${{ matrix.distro }}${{ matrix.release }}
      credentials:
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v5
        with:
          submodules: recursive
          fetch-depth: 0
          fetch-tags: true

      - name: Configure git safe directory
        run: git config --global --add safe.directory $(pwd)

      - name: Prepare Debian build
        id: get_version
        uses: ./.github/actions/prepare-debian-build
        with:
          release: ${{ matrix.release }}
          codename: ${{ matrix.codename }}

      - name: Get short SHA
        id: short_sha
        run: echo "sha=$(echo ${{ github.sha }} | cut -c1-7)" >> $GITHUB_OUTPUT

      - name: Catch missing build-deps
        run: |
          apt-get update && apt-get build-dep . -y

      - name: Build binary package
        id: build_binary
        if: github.event_name == 'pull_request'
        uses: ./.github/actions/build-debian-package
        with:
          package-type: binary
          deb-version: ${{ steps.get_version.outputs.version }}

      - name: Build source package
        id: build_source
        if: github.event_name != 'pull_request'
        uses: ./.github/actions/build-debian-package
        with:
          package-type: source
          deb-version: ${{ steps.get_version.outputs.version }}

      - name: Upload Debian package
        if: github.event_name == 'pull_request'
        uses: actions/upload-artifact@v7
        with:
          name: lemonade-${{ matrix.codename }}-deb
          path: ${{ steps.build_binary.outputs.output-dir }}/*.deb
          retention-days: 30

      - name: Upload source package
        if: github.event_name != 'pull_request'
        uses: actions/upload-artifact@v7
        with:
          name: lemonade-${{ matrix.codename }}-source-package
          path: ${{ steps.build_source.outputs.output-dir }}/*
          retention-days: 30

  package-arm64:
    name: Build (ARM64)
    # Uses official ubuntu:${{ matrix.release }} images from Docker Hub instead of
    # the custom build-environment images, because the latter do not yet carry
    # linux/arm64 manifests. build-container.yml will publish multi-arch images
    # post-merge; once that happens this can revert to the custom build-environment.
    needs: prepare-matrix
    runs-on: ubuntu-24.04-arm
    permissions:
      contents: read
    if: github.event_name == 'pull_request'
    strategy:
      matrix: ${{ fromJSON(needs.prepare-matrix.outputs.package_matrix) }}
    container:
      image: ubuntu:${{ matrix.release }}
    steps:
      - name: Install checkout prerequisites
        run: |
          apt-get update
          apt-get install -y git ca-certificates software-properties-common

      - name: Add PPA for build dependencies
        if: matrix.ppa != ''
        run: add-apt-repository -y ${{ matrix.ppa }}

      - name: Checkout code
        uses: actions/checkout@v5
        with:
          submodules: recursive
          fetch-depth: 0
          fetch-tags: true

      - name: Configure git safe directory
        run: git config --global --add safe.directory $(pwd)

      - name: Install build dependencies
        run: |
          apt-get update
          apt-get install -y \
            build-essential \
            curl \
            debhelper \
            devscripts \
            dpkg-dev \
            git \
            python3-pip \
            python3-venv \
            unzip

      - name: Prepare Debian build
        id: get_version
        uses: ./.github/actions/prepare-debian-build
        with:
          release: ${{ matrix.release }}
          codename: ${{ matrix.codename }}

      - name: Build binary package
        id: build_binary
        uses: ./.github/actions/build-debian-package
        with:
          package-type: binary
          deb-version: ${{ steps.get_version.outputs.version }}
          arch-only: 'true'

      - name: Upload Debian package
        uses: actions/upload-artifact@v7
        with:
          name: lemonade-${{ matrix.codename }}-arm64-deb
          path: ${{ steps.build_binary.outputs.output-dir }}/*.deb
          retention-days: 30

  upload-stable:
    name: Upload (Stable)
    strategy:
      matrix: ${{ fromJSON(needs.prepare-matrix.outputs.package_matrix) }}
    needs: [prepare-matrix, package]
    permissions:
      contents: read
    if: ${{ startsWith(github.ref, 'refs/tags/v') && github.event_name != 'pull_request' }}
    uses: ./.github/workflows/upload.yml
    with:
      target: ppa:lemonade-team/stable
      artifact: lemonade-${{ matrix.codename }}-source-package
    secrets: inherit

  upload-bleeding-edge:
    name: Upload (Bleeding Edge)
    strategy:
      matrix: ${{ fromJSON(needs.prepare-matrix.outputs.package_matrix) }}
    needs: [prepare-matrix, package]
    permissions:
      contents: read
    if: ${{ !startsWith(github.ref, 'refs/tags/v') && github.event_name != 'pull_request' }}
    uses: ./.github/workflows/upload.yml
    with:
      target: ppa:lemonade-team/bleeding-edge
      artifact: lemonade-${{ matrix.codename }}-source-package
    secrets: inherit
