#!/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 $VESTA/func/main.sh
source $VESTA/func/ip.sh
source $VESTA/conf/vesta.conf


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

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


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

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

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

# Updating DNS configs
if [ ! -z "$old" ] && [ ! -z "$DNS_SYSTEM" ]; then
    sed -i "s/$old/$new/" $VESTA/data/users/*/dns.conf
    sed -i "s/$old/$new/" $VESTA/data/users/*/dns/*.conf
    for user in $(ls $VESTA/data/users/); do
        $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" $VESTA/data/firewall/*.conf
    $BIN/v-update-firewall
fi



#----------------------------------------------------------#
#                       Vesta                              #
#----------------------------------------------------------#

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

exit