name: install-deps

runs:
  using: 'composite'
  steps:
    - name: Harden apt against flaky mirrors
      shell: bash
      run: |
        echo 'Acquire::ForceIPv4 "true";'       | sudo tee /etc/apt/apt.conf.d/99force-ipv4 >/dev/null
        echo 'Acquire::Queue-Mode "access";'     | sudo tee /etc/apt/apt.conf.d/99queue-mode >/dev/null
        echo 'Acquire::http::Pipeline-Depth "0";' | sudo tee /etc/apt/apt.conf.d/99pipeline >/dev/null
        echo 'Acquire::Retries "3";'             | sudo tee /etc/apt/apt.conf.d/99retries >/dev/null
        echo 'Acquire::Languages "none";'        | sudo tee /etc/apt/apt.conf.d/99no-languages >/dev/null
        echo 'Dpkg::Use-Pty "0";'               | sudo tee /etc/apt/apt.conf.d/99no-pty >/dev/null
        # GitHub runners use mirror+file:///etc/apt/apt-mirrors.txt which
        # resolves to azure mirrors. Overwrite it with the main archive.
        echo 'http://archive.ubuntu.com/ubuntu/' | sudo tee /etc/apt/apt-mirrors.txt >/dev/null
        # Also fix any sources that embed azure URLs directly
        sudo sed -i -E \
          -e 's|https?://([a-z0-9-]+\.)*archive\.ubuntu\.com/ubuntu/?|http://archive.ubuntu.com/ubuntu/|g' \
          -e 's|https?://security\.ubuntu\.com/ubuntu/?|http://archive.ubuntu.com/ubuntu/|g' \
          /etc/apt/sources.list /etc/apt/sources.list.d/*.list /etc/apt/sources.list.d/*.sources 2>/dev/null || true
    - name: Install build dependencies
      run: |
        if sudo -n true 2>/dev/null; then
          read -ra pkgs <<< "$SCX_PACKAGES"
          ok=false
          for attempt in 1 2 3; do
            if timeout 300 sudo apt-get update -y && timeout 300 sudo apt-get install -y "${pkgs[@]}"; then
              ok=true
              break
            fi
            echo "::warning::apt attempt $attempt failed, retrying in 5s..."
            sleep 5
          done
          if [ "$ok" != true ]; then
            echo "::error::All apt install attempts failed"
            exit 1
          fi
        fi
        echo /usr/lib/llvm-19/bin >> $GITHUB_PATH
      shell: bash
