#!/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                             #
#----------------------------------------------------------#

if [ "$service" == "apache2" ]; then
	service="httpd"
fi

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

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

if [ "$service" = "cron" ] && [ -f /etc/redhat-release ]; then
	service="crond"
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
		if [ "$LOCAL_PHP" == "yes" ]; then
			service_list="${service_list} brepo-php-fpm${version}"
		else
			service_list="${service_list} php${version}-php-fpm"
		fi
	done
else
	service_list="$service"
fi

for service in $service_list; do
	is_fpm="no"
	if [[ "$service" =~ ^php[0-9.]+-php-fpm ]] || [[ "$service" =~ brepo-php-fpm[0-9.]+ ]]; then
		is_fpm="yes"
	fi
	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" = "httpd" -o \
		"$service" = "exim4" -o \
		"$service" = "dovecot" -o \
		"$service" = "bind9" -o \
		"$service" = "named" -o \
		"$service" = "vsftpd" -o \
		"$is_fpm" = "yes" -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
