name: ROS

on:
  push:
    branches:
      - '**'
  pull_request:

jobs:
  build:
    name: ${{ matrix.ros_distro }} (${{ matrix.arch }})
    runs-on: ubuntu-${{ matrix.ubuntu }}${{ matrix.runner_suffix }}
    strategy:
      fail-fast: false
      matrix:
        include:
          # Ubuntu 22.04 (Jammy) + ROS2 Humble
          - ubuntu: "22.04"
            ros_distro: humble
            arch: amd64
            runner_suffix: ""
          - ubuntu: "22.04"
            ros_distro: humble
            arch: arm64
            runner_suffix: "-arm"
          # Ubuntu 24.04 (Noble) + ROS2 Jazzy
          - ubuntu: "24.04"
            ros_distro: jazzy
            arch: amd64
            runner_suffix: ""
          - ubuntu: "24.04"
            ros_distro: jazzy
            arch: arm64
            runner_suffix: "-arm"

    steps:
      - name: Checkout
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          fetch-depth: 0

      - name: Setup ROS environment
        uses: ros-tooling/setup-ros@2b7b7e10fd30ff131dfb33248ddd71c5ba31dd30 # v0.7
        # No required-ros-distributions - we only need dev tools, not desktop

      - name: Install ROS build dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends \
            ros-${{ matrix.ros_distro }}-ament-cmake \
            ros-${{ matrix.ros_distro }}-rosidl-default-generators \
            ros-${{ matrix.ros_distro }}-rosidl-default-runtime \
            ros-${{ matrix.ros_distro }}-std-msgs \
            fakeroot \
            debhelper \
            devscripts

      - name: Extract version
        id: version
        run: |
          VERSION=$(grep '<version>' edgefirst_msgs/package.xml | sed 's/.*<version>\(.*\)<\/version>.*/\1/')
          echo "version=${VERSION}" >> $GITHUB_OUTPUT
          echo "Building version ${VERSION} for ROS2 ${{ matrix.ros_distro }} on ${{ matrix.arch }}"

      - name: Build Debian package
        working-directory: edgefirst_msgs
        env:
          ROS_DISTRO: ${{ matrix.ros_distro }}
          BUILD_NUMBER: ${{ github.run_number }}
          DEB_BUILD_OPTIONS: nocheck
        run: |
          # Source ROS setup for build environment
          source /opt/ros/${{ matrix.ros_distro }}/setup.bash
          
          # Generate control and changelog
          fakeroot debian/rules version
          
          # Show generated files for debugging
          echo "=== Generated debian/control ==="
          cat debian/control
          echo ""
          echo "=== Generated debian/changelog ==="
          cat debian/changelog
          echo ""
          
          # Build the package (DEB_BUILD_OPTIONS=nocheck skips tests)
          fakeroot debian/rules binary

      - name: Collect artifacts
        run: |
          mkdir -p artifacts
          cp edgefirst_msgs/../*.deb artifacts/ 2>/dev/null || cp *.deb artifacts/
          ls -la artifacts/

      - name: Upload Debian package
        uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
        with:
          name: ros-${{ matrix.ros_distro }}-edgefirst-msgs_${{ steps.version.outputs.version }}_${{ matrix.arch }}
          path: artifacts/*.deb
          if-no-files-found: error

      - name: Write job summary
        run: |
          echo "## ROS2 Debian Package Build" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "| Property | Value |" >> $GITHUB_STEP_SUMMARY
          echo "|----------|-------|" >> $GITHUB_STEP_SUMMARY
          echo "| ROS Distro | ${{ matrix.ros_distro }} |" >> $GITHUB_STEP_SUMMARY
          echo "| Ubuntu | ${{ matrix.ubuntu }} |" >> $GITHUB_STEP_SUMMARY
          echo "| Architecture | ${{ matrix.arch }} |" >> $GITHUB_STEP_SUMMARY
          echo "| Version | ${{ steps.version.outputs.version }} |" >> $GITHUB_STEP_SUMMARY
          echo "| Build Number | ${{ github.run_number }} |" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "### Artifacts" >> $GITHUB_STEP_SUMMARY
          echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
          ls -lh artifacts/*.deb >> $GITHUB_STEP_SUMMARY
          echo "\`\`\`" >> $GITHUB_STEP_SUMMARY

