#!/bin/bash
set -e

echo "Starting service restart..."

# Restart Nginx
if systemctl list-unit-files | grep -q nginx.service; then
    systemctl restart nginx
    echo "Nginx restarted."
fi

# Restart Apache Ubuntu
if systemctl list-unit-files | grep -q apache2.service; then
    systemctl restart apache2
    echo "Apache2 restarted."
fi

# Restart Apache Amazon Linux
if systemctl list-unit-files | grep -q httpd.service; then
    systemctl restart httpd
    echo "Httpd restarted."
fi

# Restart PHP-FPM
if systemctl list-unit-files | grep -q php8.1-fpm.service; then
    systemctl restart php8.1-fpm

elif systemctl list-unit-files | grep -q php8.0-fpm.service; then
    systemctl restart php8.0-fpm

elif systemctl list-unit-files | grep -q php7.4-fpm.service; then
    systemctl restart php7.4-fpm

elif systemctl list-unit-files | grep -q php-fpm.service; then
    systemctl restart php-fpm
fi

echo "Restart completed successfully."