#!/bin/bash
set -e

echo "=== PHP 8.5 CLI Installation Script ==="
echo ""

# Check if running as root
if [ "$EUID" -ne 0 ]; then
    echo "Please run as root or with sudo: sudo bash install-php85.sh"
    exit 1
fi

# Step 1: Install prerequisites
echo "Step 1: Installing prerequisites..."
apt-get update
apt-get install -y lsb-release ca-certificates curl

# Step 2: Add Ondrej Surý PHP PPA
echo "Step 2: Adding PHP PPA repository..."
curl -sSL -o /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list
apt-get update

# Step 3: Install PHP 8.5 CLI with extensions
echo "Step 3: Installing PHP 8.5 CLI and extensions..."
apt-get install -y \
    php8.5-cli \
    php8.5-common \
    php8.5-opcache \
    php8.5-curl \
    php8.5-mbstring \
    php8.5-xml \
    php8.5-zip \
    php8.5-bcmath \
    php8.5-gd \
    php8.5-mysql \
    php8.5-pgsql \
    php8.5-sqlite3 \
    php8.5-redis \
    php8.5-apcu \
    php8.5-intl \
    php8.5-soap \
    php8.5-xdebug

# Step 4: Verify installation
echo ""
echo "=== Installation Complete ==="
echo ""
echo "PHP 8.5 Version:"
php8.5 -v
echo ""
echo "Installed Extensions:"
php8.5 -m | head -20
echo "... (more extensions available)"
echo ""
echo "Usage:"
echo "  Run PHP 8.5 explicitly: php8.5 script.php"
echo "  Set as default: sudo update-alternatives --set php /usr/bin/php8.5"
echo "  Check version: php8.5 -v"
echo ""
