#!/bin/bash
# info: restart php interpreter
# options: NONE
#
# example: v-restart-web-backend
#
# This function reloads php interpreter configuration.

restart=$1
# For backward compatibility might change in the feature
version=$2
if [ -f "/etc/redhat-release" ]; then
	version="${version//./}"
fi

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

# 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
# load config file
source_conf "$HESTIA/conf/hestia.conf"

send_email_report() {
	email=$(grep CONTACT $HESTIA/data/users/admin/user.conf)
	email=$(echo "$email" | cut -f 2 -d "'")
	subj="$(hostname): $WEB_BACKEND restart failed"
	cat $tmpfile | $SENDMAIL -s "$subj" $email
	[[ -f "$tmpfile" ]] && rm -f $tmpfile
}

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

# Exit
if [ -z "$WEB_BACKEND" ] || [ "$WEB_BACKEND" = 'remote' ]; then
	exit
fi

if [ "$1" = "no" ]; then
	exit
fi

# Schedule restart
if [ "$1" = 'scheduled' ] || [ -z "$1" ] && [ "$SCHEDULED_RESTART" = 'yes' ]; then
	sed -i "/\/$SCRIPT now/d" $HESTIA/data/queue/restart.pipe
	echo "$BIN/$SCRIPT now" >> $HESTIA/data/queue/restart.pipe
	exit
fi

tmpfile=$(mktemp)

if [ -z "$version" ]; then
	# Substitute php-fpm service name formats
	for version in $($BIN/v-list-sys-php plain); do
		if [ -f /etc/redhat-release ]; then
			v_php="php$version-php-fpm"
		else
			v_php="php$version-fpm"
		fi
		if [ -f /etc/redhat-release ]; then
			if [ ! -f "/etc/opt/remi/php${version}/php-fpm.d/dummy.conf" ]; then
				cp -f $HESTIA_INSTALL_DIR/php-fpm/dummy.conf /etc/opt/remi/php${version}/php-fpm.d/
				sed -i "s/9999/99$v_tpl/g" /etc/opt/remi/php${version}/php-fpm.d/dummy.conf
			fi
		else
			if [ ! -f "/etc/php/${version}/fpm/pool.d/dummy.conf" ]; then
				cp -f "$HESTIA_INSTALL_DIR/php-fpm/dummy.conf" "/etc/php/${version}/fpm/pool.d/"
				sed -i "s/9999/99${version//./}/g" "/etc/php/${version}/fpm/pool.d/dummy.conf"
			fi
		fi

		$BIN/v-restart-service "$v_php" "$restart"
		if [ $? -ne 0 ]; then
			send_email_report
			check_result "$E_RESTART" "$v_php restart failed"
		fi
	done
else
	if [ -f /etc/redhat-release ]; then
		v_php="php$version-php-fpm"
	else
		v_php="php$version-fpm"
	fi
	$BIN/v-restart-service "$v_php" "$restart"
	if [ $? -ne 0 ]; then
		send_email_report
		check_result "$E_RESTART" "$v_php restart failed"
	fi
fi

# Update restart queue
if [ -e "$HESTIA/data/queue/restart.pipe" ]; then
	sed -i "/\/$SCRIPT now/d" $HESTIA/data/queue/restart.pipe
fi

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

[[ -f "$tmpfile" ]] && rm -f $tmpfile
exit