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-amd64:
    if: ${{ inputs.architecture == 'amd64' || inputs.architecture == 'both' }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Build sysroot
        run: ./tools/sysroot/build_sysroot.sh

      - name: Upload sysroot artifact
        uses: actions/upload-artifact@v7
        with:
          name: ubuntu-noble-sysroot-amd64
          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/ubuntu_noble_amd64_sysroot.tar.xz \
            s3://eagle0-sysroot/${{ inputs.version }}/ubuntu_noble_amd64_sysroot.tar.xz \
            --endpoint-url https://sfo3.digitaloceanspaces.com \
            --acl public-read

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

          echo ""
          echo "=== AMD64 Sysroot uploaded ==="
          echo "URL: https://eagle0-sysroot.sfo3.digitaloceanspaces.com/${{ inputs.version }}/ubuntu_noble_amd64_sysroot.tar.xz"
          echo "SHA256: $(cat tools/sysroot/output/ubuntu_noble_amd64_sysroot.sha256)"
          echo ""
          echo "Update MODULE.bazel with:"
          echo "sysroot("
          echo "    name = \"linux_sysroot\","
          echo "    sha256 = \"$(cat tools/sysroot/output/ubuntu_noble_amd64_sysroot.sha256)\","
          echo "    urls = [\"https://eagle0-sysroot.sfo3.digitaloceanspaces.com/${{ inputs.version }}/ubuntu_noble_amd64_sysroot.tar.xz\"],"
          echo ")"

  build-sysroot-arm64:
    if: ${{ inputs.architecture == 'arm64' || inputs.architecture == 'both' }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Set up QEMU for ARM64 emulation
        uses: docker/setup-qemu-action@v4
        with:
          platforms: arm64

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v4

      - name: Build ARM64 sysroot
        run: ./tools/sysroot/build_sysroot_arm64.sh

      - name: Upload sysroot artifact
        uses: actions/upload-artifact@v7
        with:
          name: ubuntu-noble-sysroot-arm64
          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/ubuntu_noble_arm64_sysroot.tar.xz \
            s3://eagle0-sysroot/${{ inputs.version }}/ubuntu_noble_arm64_sysroot.tar.xz \
            --endpoint-url https://sfo3.digitaloceanspaces.com \
            --acl public-read

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

          echo ""
          echo "=== ARM64 Sysroot uploaded ==="
          echo "URL: https://eagle0-sysroot.sfo3.digitaloceanspaces.com/${{ inputs.version }}/ubuntu_noble_arm64_sysroot.tar.xz"
          echo "SHA256: $(cat tools/sysroot/output/ubuntu_noble_arm64_sysroot.sha256)"
          echo ""
          echo "Update MODULE.bazel with:"
          echo "sysroot("
          echo "    name = \"linux_sysroot_arm64\","
          echo "    sha256 = \"$(cat tools/sysroot/output/ubuntu_noble_arm64_sysroot.sha256)\","
          echo "    urls = [\"https://eagle0-sysroot.sfo3.digitaloceanspaces.com/${{ inputs.version }}/ubuntu_noble_arm64_sysroot.tar.xz\"],"
          echo ")"
