#!/bin/bash
# info: change system backend port
# options: PORT
#
# The function for changing the system backend port in NGINX configuration.


#----------------------------------------------------------#
#                    Variable&Function                     #
#----------------------------------------------------------#

# Argument definition
PORT=$1

# Includes
source $HESTIA/func/main.sh
source $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 [ ! -z "$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
ORIGINAL_PORT=$(cat $HESTIA/nginx/conf/nginx.conf | grep "listen" | sed 's/[^0-9]*//g')

# Check if system variable is set
if [ ! -n "$BACKEND_PORT" ]; then
    echo "BACKEND_PORT='$port'" >> $HESTIA/conf/hestia.conf
    source $HESTIA/conf/hestia.conf
fi

# Check if port is different to hestia.conf
if [ ! "$BACKEND_PORT" = "$PORT" ]; then
    sed -i s/BACKEND_PORT=\'$BACKEND_PORT\'/BACKEND_PORT=\'$PORT\'/g $HESTIA/conf/hestia.conf
fi

# Check if port is different to nginx.conf
if [ "$ORIGINAL_PORT" = "$PORT" ]; then
    # Nothing to do, exit
    exit
else
    # Replace port in config files.
    sed -i "/listen/c\        listen          $PORT ssl;" $HESTIA/nginx/conf/nginx.conf
    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
    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
    
    # Restart services
    $HESTIA/bin/v-restart-service iptables

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

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

# Logging
#log_event "$OK" "$ARGUMENTS"

exit
