#!/bin/bash -e

. ./xi-sys.cfg

# Was previous step completed?
if [ ! -f installed.crontabs ]; then
	echo "Crontabs were not configured - run previous script" >&2
	exit 1
fi

# Was this step already completed?
if [ -f installed.chkconfig ]; then
	echo "Daemons already configured - skipping."
	exit 0
fi

# Special case for EL 8 and 9
if [ "$dist" == "el8" ] || [ "$dist" == "el9" ]; then

	# Disable this, but we don't care if they don't exist
	set +e
	if `command -v firewall-cmd`; then
		systemctl disable firewalld
	fi
	set -e

	# Enable in systemctl
	for svc in nagios npcd $ntpd $mysqld crond httpd sshd; do
		systemctl enable "$svc"
	done

	# Enable php-fpm and postfix
	sed -i "s/$(grep 'pm.max_requests' /etc/php-fpm\.d/www\.conf)/pm.max_requests = 250/" /etc/php-fpm.d/www.conf
	systemctl enable php-fpm
	systemctl restart php-fpm
	# If you have a hostname like RHEL-9-123.123 the postfix wont start
	set +e
	systemctl enable postfix
	systemctl restart postfix
	set -e

elif [ "$distro" == "Ubuntu" ] || [ "$distro" == "Debian" ]; then

	for svc in nagios npcd $mysqld $httpd $ntpd $crond; do
		if `command -v systemctl`; then
			systemctl enable "$svc"
		else
			update-rc.d "$svc" defaults
		fi
	done

fi

echo "Daemons configured to start OK"
touch installed.chkconfig
