#!/bin/bash
# info: change ip nat address
# options: IP NAT_IP [RESTART]
#
# The function for changing nat ip associated with ip.


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

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

# Includes
source $HESTIA/func/main.sh
source $HESTIA/func/ip.sh
source $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 [ ! -z "$old" ] && [ ! -z "$WEB_SYSTEM" ]; then
    for user in $($HESTIA/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 [ ! -z "$old" ] && [ ! -z "$DNS_SYSTEM" ]; then
    for user in $($HESTIA/bin/v-list-sys-users plain); do
        sed -i "s/$old/$new/" $HESTIA/data/users/$user/dns.conf
        sed -i "s/$old/$new/" $HESTIA/data/users/$user/dns/*.conf
        $BIN/v-rebuild-dns-domains $user no
    done
    $BIN/v-restart-dns $restart
fi

# Updating FTP
if [ ! -z "$old" ] && [ ! -z "$FTP_SYSTEM" ]; then
    conf=$(find /etc -name $FTP_SYSTEM.conf)
    if [ -e "$conf" ]; then
        sed -i "s/$old/$new/g" $conf
        if [ "$FTP_SYSTEM" = 'vsftpd' ]; then
            check_pasv=$(grep pasv_address $conf)
            if [ -z "$check_pasv" ] && [ ! -z "$nat_ip" ]; then
                echo "pasv_address=$nat_ip" >> $conf
            fi
            if [ ! -z "$check_pasv" ] && [ -z "$nat_ip" ]; then
                sed -i "/pasv_address/d" $conf
            fi
            if [ ! -z "$check_pasv" ] && [ ! -z "$nat_ip" ]; then
                sed -i "s/pasv_address=.*/pasv_address='$nat_ip'/g" $conf
            fi
        fi
    fi
    $BIN/v-restart-ftp $restart
fi

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



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

# Logging
log_history "changed associated nat address on $ip to $nat_ip" '' 'admin'
log_event "$OK" "$ARGUMENTS"

exit
