name: Solus eopkg build

on:
  workflow_dispatch:
    inputs:
      package_yml:
        description: "Path to Solus package.yml for ypkg build"
        required: false
        default: "packaging/solus/package.yml"
      solus_image:
        description: "Solus container image"
        required: false
        default: "docker.io/silkeh/solus:latest"
  push:
    branches:
      - main
      - master

permissions:
  contents: read
  packages: read

jobs:
  build-eopkg:
    runs-on: ubuntu-latest
    env:
      SOLUS_IMAGE: ${{ inputs.solus_image || 'docker.io/silkeh/solus:latest' }}
      PACKAGE_YML: ${{ inputs.package_yml || 'packaging/solus/package.yml' }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Log in to GHCR (for ghcr.io images)
        if: startsWith(env.SOLUS_IMAGE, 'ghcr.io/')
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ github.token }}

      - name: Build eopkg package in Solus container
        run: |
          if [ ! -f "$PACKAGE_YML" ]; then
            echo "Expected Solus packaging file at $PACKAGE_YML" >&2
            echo "Available package.yml files:" >&2
            find . -name "package.yml" -print >&2 || true
            echo "Add a package.yml (ypkg) file or update the workflow input PACKAGE_YML." >&2
            exit 1
          fi

          if ! docker pull "$SOLUS_IMAGE"; then
            echo "Failed to pull SOLUS_IMAGE=$SOLUS_IMAGE" >&2
            echo "Set workflow input 'solus_image' to a valid image that provides eopkg/ypkg." >&2
            exit 1
          fi

          docker run --rm \
            -v "$PWD":/workspace \
            -w /workspace \
            -e PACKAGE_YML="$PACKAGE_YML" \
            "$SOLUS_IMAGE" \
            sh -lc '
              set -euo pipefail
              eopkg update-repo -y
              # Keep file-conflict handling on: some base images still carry
              # openssl-11 leftovers, and Meson tooling is not always preinstalled.
              eopkg install -y --ignore-file-conflicts \
                ypkg git meson ninja pkgconf gcc gettext
              ypkg build "$PACKAGE_YML"
              mkdir -p /workspace/artifacts
              find . -maxdepth 3 -name "*.eopkg" -type f -exec cp -v {} /workspace/artifacts/ \;
            '

      - name: Upload eopkg artifacts
        uses: actions/upload-artifact@v4
        with:
          name: solus-eopkg
          path: artifacts/*.eopkg
          if-no-files-found: error
