#!/bin/bash
# Remove duplicate ionCube entry from php.ini
# Run with: sudo bash remove-duplicate-ioncube.sh

echo "=== Removing Duplicate ionCube Entry ==="
echo ""

PHP_INI="/etc/php/8.3/fpm/php.ini"

echo "1. Checking current configuration..."
echo "   Entries in php.ini:"
grep -n "zend_extension.*ioncube" "$PHP_INI" 2>/dev/null || echo "   (none found)"

echo ""
echo "   Entry in conf.d:"
cat /etc/php/8.3/fpm/conf.d/00-ioncube.ini

echo ""
echo "2. Removing duplicate from php.ini..."
sudo sed -i '/zend_extension.*ioncube/d' "$PHP_INI"

echo "✓ Removed"
echo ""

echo "3. Verifying..."
REMAINING=$(grep -c "zend_extension.*ioncube" "$PHP_INI" 2>/dev/null || echo "0")
if [ "$REMAINING" -eq 0 ]; then
    echo "✓ No ionCube entries in php.ini (correct - should only be in conf.d)"
else
    echo "⚠️  Still found $REMAINING entries in php.ini"
fi

echo ""
echo "4. Restarting PHP-FPM 8.3..."
sudo systemctl restart php8.3-fpm
sleep 2

if sudo systemctl is-active --quiet php8.3-fpm; then
    echo "✓ PHP-FPM 8.3 restarted"
else
    echo "❌ PHP-FPM 8.3 failed to restart"
    exit 1
fi

echo ""
echo "5. Testing billing site..."
sleep 2
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" https://billing.friendlytv.site/ 2>&1)
echo "   HTTP Status: $HTTP_CODE"

if [ "$HTTP_CODE" = "200" ]; then
    echo ""
    echo "✅ SUCCESS! Billing site is working!"
else
    echo ""
    echo "⚠️  Still getting $HTTP_CODE"
    echo "   Check: sudo tail -10 /var/log/apache2/error.log"
fi

