#!/bin/bash
# info: change NAT IP address
# options: IP NAT_IP [RESTART]
#
# example: v-change-sys-ip-nat 10.0.0.1 203.0.113.1
#
# This function for changing NAT IP associated with IP.

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

# Argument definition
ip="$1"
nat_ip="$2"
restart="$3"

# 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
# shellcheck source=/usr/local/hestia/func/ip.sh
source $HESTIA/func/ip.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

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

check_args '2' "$#" 'IP NAT_IP [RESTART]'
is_format_valid 'ip'
is_format_valid 'nat_ip'
is_ip_valid "$ip"

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Updating IP
if [ -z "$(grep NAT= $HESTIA/data/ips/$ip)" ]; then
	sed -i "s/^TIME/NAT='$nat_ip'\nTIME/g" $HESTIA/data/ips/$ip
	old=''
	new="$nat_ip"
else
	old="$(get_ip_value '$NAT')"
	new="$nat_ip"
	sed -i "s/NAT=.*/NAT='$new'/" $HESTIA/data/ips/$ip
	if [ -z "$nat_ip" ]; then
		new="$ip"
	fi
fi

# Updating WEB configs
if [ -n "$old" ] && [ -n "$WEB_SYSTEM" ]; then
	for user in $($BIN/v-list-sys-users plain); do
		sed -i "s/$old/$new/" $HESTIA/data/users/$user/web.conf
		$BIN/v-rebuild-web-domains "$user" no
	done
	$BIN/v-restart-dns "$restart"
fi

# Updating DNS configs
if [ -n "$old" ] && [ -n "$DNS_SYSTEM" ]; then
	for user in $($BIN/v-list-sys-users plain); do
		sed -i "s/$old/$new/" "$HESTIA/data/users/$user/dns.conf"
		if ls $HESTIA/data/users/$user/dns/*.conf > /dev/null 2>&1; then
			sed -i "s/$old/$new/" $HESTIA/data/users/$user/dns/*.conf
		fi
		$BIN/v-rebuild-dns-domains "$user" no
	done
	$BIN/v-restart-dns "$restart"
fi

# Updating FTP
if [ -n "$old" ] && [ -n "$FTP_SYSTEM" ]; then
	ftp_conf="$(find /etc -maxdepth 2 -name "$FTP_SYSTEM.conf")"
	if [ -e "$ftp_conf" ]; then
		sed -i "s/$old/$new/g" "$ftp_conf"
		if [ "$FTP_SYSTEM" = 'vsftpd' ]; then
			check_pasv="$(grep pasv_address "$ftp_conf")"
			if [ -z "$check_pasv" ] && [ -n "$nat_ip" ]; then
				echo "pasv_address=$nat_ip" >> "$ftp_conf"
			fi
			if [ -n "$check_pasv" ] && [ -z "$nat_ip" ]; then
				sed -i "/pasv_address/d" "$ftp_conf"
			fi
			if [ -n "$check_pasv" ] && [ -n "$nat_ip" ]; then
				sed -i "s/pasv_address=.*/pasv_address='$nat_ip'/g" "$ftp_conf"
			fi
		fi
	fi
	if [ "$FTP_SYSTEM" = 'proftpd' ]; then
        	ext_ip_conf="/etc/$FTP_SYSTEM/conf.d/external_ip.conf"
        	content="MasqueradeAddress ${nat_ip}"
        	echo "$content" > "$ext_ip_conf"
    	fi
	$BIN/v-restart-ftp "$restart"
fi

# Updating firewall
if [ -n "$old" ] && [ -n "$FIREWALL_SYSTEM" ]; then
	sed -i "s/$old/$new/g" $HESTIA/data/firewall/*.conf
	$BIN/v-update-firewall
fi

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

# Logging
$BIN/v-log-action "system" "Info" "System" "IP NAT address changed (IP: $ip, NAT IP: $nat_ip)."
log_event "$OK" "$ARGUMENTS"

exit