#!/bin/bash
# info: change system backend port
# options: PORT
#
# example: v-change-sys-port 5678
#
# This function for changing the system backend port in NGINX configuration.

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

# Argument definition
PORT=$1
NGINX_CONFIG="$HESTIA/nginx/conf/nginx.conf"

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

# Functions
is_port_valid() {
	# Check if PORT is numeric
	if [[ ! "$PORT" =~ ^[0-9]+$ ]]; then
		echo "Port should contains a numeric value only!"
		log_event "$E_INVALID" "$ARGUMENTS"
		exit "$E_INVALID"
	fi

	# Check if PORT is already used
	BUSY_PORT=$(lsof -i:"$PORT")
	if [ -n "$BUSY_PORT" ] && [ "$PORT" != "$BACKEND_PORT" ]; then
		echo "Port is already used by Hestia, please set anotherone!"
		log_event "$E_INUSE" "$ARGUMENTS"
		exit "$E_INUSE"
	fi
}

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

check_args '1' "$#" 'PORT'
is_port_valid

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Get original port
LISTEN_ROWS=$(cat "$NGINX_CONFIG" | grep -c "listen")
ORIGINAL_PORT=$(sed -ne "/listen/{s/.*listen[^0-9]*\([0-9][0-9]*\)[ \t]*ssl\;/\1/p;q}" "$NGINX_CONFIG")

# Check if port is different to nginx.conf
if [ "$ORIGINAL_PORT" = "$PORT" ]; then
	# Nothing to do, exit
	exit
else
	# Set new port in config via v-change-sys-config-value
	"$BIN/v-change-sys-config-value" "BACKEND_PORT" "$PORT"
	# Replace port in config files.
	sed -i "s/\(listen[ \t]*.*[: \t]\)[0-9][0-9]*\([^0-9]*ssl\;$\)/\1$PORT\2/" "$NGINX_CONFIG"
	if [ -d /etc/roundcube/ ]; then
		sed -i "/password_hestia_port/c\$rcmail_config['password_hestia_port'] = '$PORT';" /etc/roundcube/plugins/password/config.inc.php
	fi
	if [ -d /etc/snappymail/ ]; then
		sed -i "/\"hestia_port\":/c\\\"hestia_host\": $PORT" /etc/snappymail/data/_data_/_default_/configs/plugin-change-password.json
	fi

	if [ -f "$HESTIA/data/firewall/rules.conf" ]; then
		sed -i "/COMMENT='HESTIA'/c\RULE='2' ACTION='ACCEPT' PROTOCOL='TCP' PORT='$PORT' IP='0.0.0.0/0' COMMENT='HESTIA' SUSPENDED='no' TIME='07:40:16' DATE='2014-05-25'" "$HESTIA/data/firewall/rules.conf"
	fi

	if [ -f "$HESTIA/data/firewall/chains.conf" ]; then
		sed -i "/CHAIN='HESTIA'/c\CHAIN='HESTIA' PORT='$PORT' PROTOCOL='TCP'" "$HESTIA/data/firewall/chains.conf"
	fi

	# Restart services
	if [ -n "$FIREWALL_SYSTEM" ] && [ "$FIREWALL_SYSTEM" != no ]; then
		"$BIN/v-restart-service" iptables
	fi

	# Check if Hestia is running
	if [[ $(ps -eaf | grep -i hestia | sed '/^$/d' | wc -l) -gt 1 ]]; then
		"$BIN/v-restart-service" hestia
	fi
fi

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

# Logging
"$BIN/v-log-action" "system" "Warning" "System" "Hestia Control Panel backend port changed (New Value: $PORT, Old Value: $ORIGINAL_PORT)."
if [ $LISTEN_ROWS -gt 1 ]; then
	"$BIN/v-log-action" "system" "Warning" "System" "Hestia Control Panel backend port: Use first of $LISTEN_ROWS listened ports in $NGINX_CONFIG"
fi
log_event "$OK" "$ARGUMENTS"

exit
