#!/bin/bash
# info: restart service
# options: SERVICE [RESTART]
#
# example: v-restart-service apache2
#
# This function restarts system service.

#----------------------------------------------------------#
#                Variables & Functions                     #
#----------------------------------------------------------#

# Argument definition
service=$1
restart=$2

# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
source_conf "$HESTIA/conf/hestia.conf"

#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#

check_args '1' "$#" 'SERVICE [RESTART]'
is_format_valid 'service' 'restart'

#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

log="/dev/null"
if [ "$DEBUG_MODE" = "true" ]; then

	log="/var/log/hestia/debug.log"
fi

# Multi-instance service restart request handling
if [ "$service" = "php-fpm" ]; then
	service_list=''
	# Get installed php versions
	php_versions=$($BIN/v-list-sys-php plain)
	# Substitute php-fpm service name formats
	for version in $php_versions; do
		service_list="${service_list} php${version}-fpm"
	done
else
	service_list="$service"
fi

for service in $service_list; do
	if [ "$service" = "iptables" ]; then
		# Run the restart rules for iptables firewall
		$BIN/v-stop-firewall
		$BIN/v-update-firewall
	elif [ "$restart" = "ssl" ] && [ "$service" = "nginx" ]; then
		service $service upgrade >> $log 2>&1
	elif [ -z "$restart" -o "$restart" = "no" ] && [ \
		"$service" = "nginx" -o \
		"$service" = "apache2" -o \
		"$service" = "exim4" -o \
		"$service" = "dovecot" -o \
		"$service" = "bind9" -o \
		"$service" = "named" -o \
		"$service" = "vsftpd" -o \
		"$service" = "php5.6-fpm" -o \
		"$service" = "php7.0-fpm" -o \
		"$service" = "php7.1-fpm" -o \
		"$service" = "php7.2-fpm" -o \
		"$service" = "php7.3-fpm" -o \
		"$service" = "php7.4-fpm" -o \
		"$service" = "php8.0-fpm" -o \
		"$service" = "php8.1-fpm" -o \
		"$service" = "php8.2-fpm" -o \
		"$service" = "proftpd" -o \
		"$service" = "ssh" -o \
		"$service" = "fail2ban" ]; then
		systemctl reload-or-restart "$service" >> $log 2>&1
	else
		systemctl reset-failed "$service" >> $log 2>&1
		systemctl restart "$service" >> $log 2>&1
	fi

	# Check the result of the service restart and report whether it failed.
	if [ $? -ne 0 ]; then
		check_result "$E_RESTART" "ERROR: Restart of $service failed."
		$BIN/v-log-action "system" "Error" "System" "Service failed to restart (Name: $service)."
	else
		$BIN/v-log-action "system" "Info" "System" "Service restarted (Name: $service)."
	fi
done

#----------------------------------------------------------#
#                       Hestia                             #
#----------------------------------------------------------#

# Logging
log_event "$OK" "$ARGUMENTS"

exit