#!/bin/bash
# info: change service config
# options: CONFIG SERVICE [RESTART]
#
# example: v-change-sys-service-config /home/admin/dovecot.conf dovecot yes
#
# This function for changing service confguration.

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

# Argument definition
src=$1
service=$2
restart=$3

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

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

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

if [ ! -e "$src" ]; then
	check_result "$E_NOTEXIST" "$src config doesn't exist"
fi

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Defining dst config path
case $service in
	nginx) dst='/etc/nginx/nginx.conf' ;;
	httpd) dst='/etc/httpd/conf/httpd.conf' ;;
	apache2) dst='/etc/apache2/apache2.conf' ;;
	exim) dst='/etc/exim/exim.conf' ;;
	exim4) dst='/etc/exim4/exim4.conf.template' ;;
	vsftpd) dst=$(find /etc/vsftpd* -name 'vsftpd.conf') ;;
	proftpd) dst=$(find /etc/proftpd* -name 'proftpd.conf') ;;
	php) dst=$(find /etc/php* -name php.ini) ;;
	mysql) dst=$(find /etc/my* -name my.cnf) ;;
	mysqld) dst=$(find /etc/my* -name my.cnf) ;;
	mariadb) dst=$(find /etc/my* -name my.cnf) ;;
	postgresql) dst=$($BIN/v-list-sys-pgsql-config plain | cut -f 1) ;;
	postgresql-hba) dst=$($BIN/v-list-sys-pgsql-config plain | cut -f 2) ;;
	dovecot) dst=$(find /etc/dovecot* -name dovecot.conf) ;;
	dovecot-1) dst='/etc/dovecot/conf.d/10-auth.conf' ;;
	dovecot-2) dst='/etc/dovecot/conf.d/10-logging.conf' ;;
	dovecot-3) dst='/etc/dovecot/conf.d/10-mail.conf' ;;
	dovecot-4) dst='/etc/dovecot/conf.d/10-master.conf' ;;
	dovecot-5) dst='/etc/dovecot/conf.d/10-ssl.conf' ;;
	dovecot-6) dst='/etc/dovecot/conf.d/20-imap.conf' ;;
	dovecot-7) dst='/etc/dovecot/conf.d/20-pop3.conf' ;;
	dovecot-8) dst='/etc/dovecot/conf.d/auth-passwdfile.conf.ext' ;;
	named) dst='/etc/named.conf' ;;
	bind9) dst='/etc/bind/named.conf' ;;
	bind9-opt) dst='/etc/bind/named.conf.options' ;;
	spamd) dst=$($BIN/v-list-sys-spamd-config plain) ;;
	spamassassin) dst=$($BIN/v-list-sys-spamd-config plain) ;;
	clamd) dst=$($BIN/v-list-sys-clamd-config plain) ;;
	fail2ban) dst='/etc/fail2ban/jail.local' ;;
	ssh) dst='/etc/ssh/sshd_config' ;;
	*) check_result $E_NOTEXIST "service $service doesn't exist" ;;
esac

# Checking config path
for config in $dst; do
	if [ ! -e "$config" ]; then
		check_result "$E_NOTEXIST" "$service config doesn't exist"
	fi
done

# Checking diff between src and dst configs
for config in $dst; do
	diff -q "$src" "$config" > /dev/null

	if [ $? -ne 0 ]; then
		cp "$config" "$config.vst.back"
		cp "$src" "$config"
		update="yes"
	fi
done

# Restarting service
if [ "$update" = 'yes' ] && [ "$restart" != 'no' ]; then
	if [[ "$service" =~ - ]]; then
		service=$(echo ${service%-*})
	fi

	if [ "$service" = 'php' ]; then
		if [ "$(multiphp_count)" -gt 0 ]; then
			service="php-fpm"
		elif [ "$WEB_SYSTEM" = 'apache2' ]; then
			service="$WEB_SYSTEM"
		fi
	fi

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

	if [ $? -ne 0 ]; then
		for config in $dst; do
			cat "$config.vst.back" > "$config"
			rm -f "$config.vst.back"
		done
		check_result "$E_RESTART" "ERROR: $service failed to start with new configuration."
	fi
fi

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

# Logging
log_event "$OK" "$ARGUMENTS"

exit