#!/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="$TULIO/nginx/conf/nginx.conf"

# Includes
# shellcheck source=/etc/tuliocp/tulio.conf
source /etc/tuliocp/tulio.conf
# shellcheck source=/usr/local/tulio/func/main.sh
source "$TULIO/func/main.sh"
# shellcheck source=/usr/local/tulio/func/webmail.sh
source "$TULIO/func/webmail.sh"
# load config file
source_conf "$TULIO/conf/tulio.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 Tulio, 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_tulio_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"
	RC_PASSWORD_CONFIG='/etc/roundcube/plugins/password/config.inc.php'
	if [ -f "$RC_PASSWORD_CONFIG" ]; then
		if ! set_roundcube_option "$RC_PASSWORD_CONFIG" 'password_tulio_port' "$PORT"; then
			echo "Error: unable to update $RC_PASSWORD_CONFIG"
			log_event "$E_UPDATE" "$ARGUMENTS"
			exit "$E_UPDATE"
		fi
	fi
	if [ -d /var/lib/snappymail/ ]; then
		sed -i "/\"tulio_port\":/c\\        \"tulio_port\": $PORT" /var/lib/snappymail/data/_data_/_default_/configs/plugin-change-password.json
	fi

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

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

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

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

#----------------------------------------------------------#
#                       Tulio                             #
#----------------------------------------------------------#

# Logging
"$BIN/v-log-action" "system" "Warning" "System" "Tulio 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" "Tulio Control Panel backend port: Use first of $LISTEN_ROWS listened ports in $NGINX_CONFIG"
fi
log_event "$OK" "$ARGUMENTS"

exit
