#!/bin/sh

### BEGIN INIT INFO
# Provides:       hestia
#                 internal nginx
#                 internal php-fpm
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the hestia control panel
# Description:       starts nginx and php-fpm using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NGINX_DAEMON=/usr/local/hestia/nginx/sbin/hestia-nginx
NGINX_NAME=hestia-nginx
NGINX_DESC=hestia-nginx
NGINX_PID=/run/hestia-nginx.pid
NGINX_CONF=/usr/local/hestia/nginx/conf/nginx.conf

PHP_DAEMON=/usr/local/hestia/php/sbin/hestia-php
PHP_NAME=hestia-php
PHP_DESC=hestia-php
PHP_PID=/run/hestia-php.pid
PHP_CONF=/usr/local/hestia/php/etc/php-fpm.conf

set -e

. /lib/lsb/init-functions

. /etc/profile.d/hestia.sh

adapt_nginx_config() {
	# Detect "physical" NICs only (virtual NICs created by Docker, WireGuard etc. are excluded)
	physical_nics="$(ip -d -j link show | jq -r '.[] | if .link_type == "loopback" // .linkinfo.info_kind then empty else .ifname end')"
	if [ -z "$physical_nics" ]; then
		physical_nics="$(ip -d -j link show | jq -r '.[] | if .link_type == "loopback" then empty else .ifname end')"
	fi
	for nic in $physical_nics; do
		if [ -z "$ipv4_scope_global" ]; then
			ipv4_scope_global="$(ip -4 -d -j addr show "$nic" | jq -r '.[] | select(length > 0) | .addr_info[] | if .scope == "global" then .local else empty end')"
		fi
		if [ -z "$ipv6_scope_global" ]; then
			ipv6_scope_global="$(ip -6 -d -j addr show "$nic" | jq -r '.[] | select(length > 0) | .addr_info[] | if .scope == "global" then .local else empty end')"
		fi
	done

	if [ -n "$ipv4_scope_global" ]; then
		sed -i 's/#IPV4\([ \t]*listen[ \t]*[0-9]\{1,5\}.*\)/\1/' "$NGINX_CONF"
	else
		sed -i 's/^\([ \t]*listen[ \t]*[0-9]\{1,5\}.*\)/#IPV4\1/' "$NGINX_CONF"
	fi
	if [ -n "$ipv6_scope_global" ]; then
		sed -i 's/#IPV6\([ \t]*listen[ \t]*\[\:\:\]\:[0-9]\{1,5\}.*\)/\1/' "$NGINX_CONF"
	else
		sed -i 's/^\([ \t]*listen[ \t]*\[\:\:\]\:[0-9]\{1,5\}.*\)/#IPV6\1/' "$NGINX_CONF"
	fi
}

update_nginx_resolver() {
	if grep -qw "1.0.0.1 8.8.4.4 1.1.1.1 8.8.8.8" "$NGINX_CONF"; then
		for nameserver in $(grep -is '^nameserver' /etc/resolv.conf | cut -d' ' -f2 | tr '\r\n' ' ' | xargs); do
			if echo "$nameserver" | grep -Pq "^(\d{1,3}\.){3}\d{1,3}$"; then
				if [ -z "$resolver" ]; then
					resolver="$nameserver"
				else
					resolver="$resolver $nameserver"
				fi
			fi
		done

		if [ -n "$resolver" ]; then
			sed -i "s/1.0.0.1 8.8.4.4 1.1.1.1 8.8.8.8/$resolver/g" "$NGINX_CONF"
		fi
	fi
}

start_nginx() {
	#adapt_nginx_config
	update_nginx_resolver
	start-stop-daemon --start --quiet --pidfile $NGINX_PID \
		--retry 5 --exec $NGINX_DAEMON --oknodo
}

start_php() {
	start-stop-daemon --start --quiet --pidfile $PHP_PID \
		--retry 5 --exec $PHP_DAEMON --oknodo
}

stop_nginx() {
	start-stop-daemon --stop --quiet --pidfile $NGINX_PID \
		--retry 5 --oknodo --exec $NGINX_DAEMON
}

stop_php() {
	start-stop-daemon --stop --quiet --pidfile $PHP_PID \
		--retry 5 --oknodo --exec $PHP_DAEMON
}

case "$1" in
	start)
		log_daemon_msg "Starting $NGINX_DESC" "$NGINX_NAME"
		start_nginx
		log_end_msg $?
		log_daemon_msg "Starting $PHP_DESC" "$PHP_NAME"
		start_php
		log_end_msg $?
		;;

	stop)
		log_daemon_msg "Stopping $NGINX_DESC" "$NGINX_NAME"
		stop_nginx
		log_end_msg $?
		log_daemon_msg "Stopping $PHP_DESC" "$PHP_NAME"
		stop_php
		log_end_msg $?
		;;

	restart | force-reload | reload | configtest | testconfig)
		log_daemon_msg "Restarting $NGINX_DESC" "$NGINX_NAME"
		stop_nginx
		stop_php
		sleep 1
		start_nginx
		log_end_msg $?
		log_daemon_msg "Restarting $PHP_DESC" "$PHP_NAME"
		start_php
		log_end_msg $?
		;;

	status)
		status_of_proc -p $NGINX_PID "$NGINX_DAEMON" hestia-nginx
		status_of_proc -p $PHP_PID "$PHP_DAEMON" hestia-php
		;;

	*)
		echo "Usage: hestia {start|stop|restart|status}" >&2
		exit 1
		;;
esac

exit 0
