name: Build Linux Sysroot

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Sysroot version (e.g., v2, v3)'
        required: true
        default: 'v2'
        type: string
      architecture:
        description: 'Target architecture'
        required: true
        default: 'amd64'
        type: choice
        options:
          - amd64
          - arm64
          - both

permissions:
  contents: read

jobs:
  build-sysroot:
    name: Build Linux Sysroot (${{ matrix.architecture }})
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix: ${{ fromJSON(inputs.architecture == 'both' && '{"include":[{"architecture":"amd64","build_script":"./tools/sysroot/build_sysroot.sh","artifact_name":"ubuntu-noble-sysroot-amd64","archive_name":"ubuntu_noble_amd64_sysroot.tar.xz","checksum_name":"ubuntu_noble_amd64_sysroot.sha256","module_name":"linux_sysroot"},{"architecture":"arm64","build_script":"./tools/sysroot/build_sysroot_arm64.sh","artifact_name":"ubuntu-noble-sysroot-arm64","archive_name":"ubuntu_noble_arm64_sysroot.tar.xz","checksum_name":"ubuntu_noble_arm64_sysroot.sha256","module_name":"linux_sysroot_arm64"}]}' || inputs.architecture == 'arm64' && '{"include":[{"architecture":"arm64","build_script":"./tools/sysroot/build_sysroot_arm64.sh","artifact_name":"ubuntu-noble-sysroot-arm64","archive_name":"ubuntu_noble_arm64_sysroot.tar.xz","checksum_name":"ubuntu_noble_arm64_sysroot.sha256","module_name":"linux_sysroot_arm64"}]}' || '{"include":[{"architecture":"amd64","build_script":"./tools/sysroot/build_sysroot.sh","artifact_name":"ubuntu-noble-sysroot-amd64","archive_name":"ubuntu_noble_amd64_sysroot.tar.xz","checksum_name":"ubuntu_noble_amd64_sysroot.sha256","module_name":"linux_sysroot"}]}') }}

    steps:
      - name: Checkout repository
        uses: actions/checkout@v7
        with:
          persist-credentials: false

      - name: Set up QEMU for ARM64 emulation
        if: ${{ matrix.architecture == 'arm64' }}
        uses: docker/setup-qemu-action@v4
        with:
          platforms: arm64

      - name: Set up Docker Buildx
        if: ${{ matrix.architecture == 'arm64' }}
        uses: docker/setup-buildx-action@v4

      - name: Build sysroot
        run: ${{ matrix.build_script }}

      - name: Upload sysroot artifact
        uses: actions/upload-artifact@v7
        with:
          name: ${{ matrix.artifact_name }}
          path: tools/sysroot/output/
          retention-days: 1

      - name: Install AWS CLI
        run: |
          if ! command -v aws &> /dev/null; then
            curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
            unzip -q awscliv2.zip
            sudo ./aws/install
          fi

      - name: Upload to DigitalOcean Spaces
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.SECRET_KEY }}
        run: |
          # Upload sysroot tarball to DO Spaces
          aws s3 cp "tools/sysroot/output/${{ matrix.archive_name }}" \
            "s3://eagle0-sysroot/${{ inputs.version }}/${{ matrix.archive_name }}" \
            --endpoint-url https://sfo3.digitaloceanspaces.com \
            --acl public-read

          # Upload sha256 file
          aws s3 cp "tools/sysroot/output/${{ matrix.checksum_name }}" \
            "s3://eagle0-sysroot/${{ inputs.version }}/${{ matrix.checksum_name }}" \
            --endpoint-url https://sfo3.digitaloceanspaces.com \
            --acl public-read

          echo ""
          echo "=== ${{ matrix.architecture }} Sysroot uploaded ==="
          echo "URL: https://eagle0-sysroot.sfo3.digitaloceanspaces.com/${{ inputs.version }}/${{ matrix.archive_name }}"
          echo "SHA256: $(cat "tools/sysroot/output/${{ matrix.checksum_name }}")"
          echo ""
          echo "Update MODULE.bazel with:"
          echo "sysroot("
          echo "    name = \"${{ matrix.module_name }}\","
          echo "    sha256 = \"$(cat "tools/sysroot/output/${{ matrix.checksum_name }}")\","
          echo "    urls = [\"https://eagle0-sysroot.sfo3.digitaloceanspaces.com/${{ inputs.version }}/${{ matrix.archive_name }}\"],"
          echo ")"
