#!/bin/bash
# info: restart proxy server
# options: NONE
#
# example: v-restart-proxy [RESTART]
#
# This function reloads proxy server configuration.

#----------------------------------------------------------#
#                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"

date=$(date +"%Y-%m-%d %H:%M:%S")

send_email_report() {
	email=$(grep CONTACT $HESTIA/data/users/admin/user.conf)
	email=$(echo "$email" | cut -f 2 -d "'")
	tmpfile=$(mktemp)
	subj="$(hostname): $PROXY_SYSTEM restart failed"
	nginx -t >> $tmpfile 2>&1

	if [ "$1" == "DO_RESTART" ]; then
		service "$PROXY_SYSTEM" restart >> $tmpfile 2>&1
	fi
	cat "$tmpfile" | $SENDMAIL -s "$subj" "$email"
	if [ "$DEBUG_MODE" = "true" ]; then
		echo "[ $date | $PROXY_SYSTEM | PROXY ]" >> /var/log/hestia/debug.log 2>&1
		cat "$tmpfile" >> /var/log/hestia/debug.log 2>&1
	fi
	rm -f $tmpfile
}

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

# Exit
if [ -z "$PROXY_SYSTEM" ] || [ "$PROXY_SYSTEM" = '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

if [ "$1" = "updatessl" ]; then
	sed -i "/\/$SCRIPT ssl/d" $HESTIA/data/queue/restart.pipe
	echo "$BIN/$SCRIPT ssl" >> $HESTIA/data/queue/restart.pipe
	exit
fi

if [ -f "$HESTIA/web/inc/nginx_proxy" ]; then
	# if hestia is behind default nginx, restart in background with 15 sec delay
	# background restart
	if [ "$1" = 'background' ]; then
		# Restart system
		sleep 15
		# Preform a check if Nginx is valid as reload doesn't throw an error / exit
		if [ "$DEBUG_MODE" = "true" ]; then
			echo "[ $date | $PROXY_SYSTEM ]" >> /var/log/hestia/debug.log 2>&1
			if [ -f /etc/redhat-release ]; then
				$BIN/v-check-service-config "$PROXY_SYSTEM" /var/log/hestia/debug.log
			else
				service $PROXY_SYSTEM configtest >> /var/log/hestia/debug.log 2>&1
			fi
		else
			if [ -f /etc/redhat-release ]; then
				$BIN/v-check-service-config "$PROXY_SYSTEM" /dev/null
			else
				service $PROXY_SYSTEM configtest > /dev/null 2>&1
			fi
		fi
		if [ $? -ne 0 ]; then
			send_email_report
			check_result "$E_RESTART" "$PROXY_SYSTEM restart failed"
		fi

		$BIN/v-restart-service $PROXY_SYSTEM > /dev/null 2>&1

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

		exit
	fi
	# Send to background process
	nohup $BIN/v-restart-proxy 'background' &> /dev/null &
else
	# Default behaviour

	# Preform a check if Nginx is valid as reload doesn't throw an error / exit
	if [ "$DEBUG_MODE" = "true" ]; then
		echo "[ $date | $PROXY_SYSTEM ]" >> /var/log/hestia/debug.log 2>&1
		if [ -f /etc/redhat-release ]; then
			$BIN/v-check-service-config "$PROXY_SYSTEM" /var/log/hestia/debug.log
		else
			service $PROXY_SYSTEM configtest >> /var/log/hestia/debug.log 2>&1
		fi
	else
		if [ -f /etc/redhat-release ]; then
			$BIN/v-check-service-config "$PROXY_SYSTEM" /dev/null
		else
			service $PROXY_SYSTEM configtest > /dev/null 2>&1
		fi
	fi
	if [ $? -ne 0 ]; then
		send_email_report
		check_result "$E_RESTART" "$PROXY_SYSTEM restart failed"
	fi

	# Restart system
	if [ "$1" = "ssl" ]; then
		restart="ssl"
	fi
	$BIN/v-restart-service "$PROXY_SYSTEM" "$restart" > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		send_email_report "DO_RESTART"
		check_result "$E_RESTART" "$PROXY_SYSTEM restart failed"
	fi

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

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

exit