#!/bin/bash
# info: update system ip
# options: [NONE]
#
# The function scans configured ip in the system and register them with vesta
# internal database. This call is intended for use on vps servers, where ip is
# set by hypervizor.


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

# Importing system variables
source /etc/profile

# Includes
source $VESTA/func/main.sh
source $VESTA/func/ip.sh
source $VESTA/conf/vesta.conf


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


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

# Listing system ip addresses
ips=$(/sbin/ip addr |grep 'inet ' |grep global |awk '{print $2}' |cut -f1 -d/)
v_ips=$(ls $VESTA/data/ips/)
ip_num=$(echo "$ips" |wc -l)
v_ip_num=$(echo "$v_ips" |wc -l)

# Checking primary IP change
if [[ "$ip_num" -eq '1' ]] && [[ "$v_ip_num" -eq 1 ]]; then
    if [ "$ips" != "$v_ips" ]; then
        new=$ips
        old=$v_ips
    fi
fi

# Updating configs
if [ ! -z "$old" ]; then
    mv $VESTA/data/ips/$old $VESTA/data/ips/$new

    # Updating PROXY
    if [ ! -z "$PROXY_SYSTEM" ]; then
        cd /etc/$PROXY_SYSTEM/conf.d
        if [ -e "$old.conf" ]; then
            mv $old.conf $new.conf
            sed -i "s/$old/$new/g" $new.conf
        fi
    fi

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

    # Updating DNS
    if [ ! -z "$DNS_SYSTEM" ]; then
        sed -i "s/$old/$new/g" $VESTA/data/users/*/dns.conf
        sed -i "s/$old/$new/g" $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
    fi

    # Updating FTP
    if [ ! -z "$FTP_SYSTEM" ] && [ "$FTP_SYSTEM" = 'vsftpd' ]; then
        conf=$(find /etc/ -maxdepth 2 -name $FTP_SYSTEM.conf)
        if [ ! -z "$conf" ]; then
            sed -i "s/$old/$new/g" $conf
            $BIN/v-restart-ftp
        fi
    fi

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

# Adding system IP
for ip in $ips; do
    check_ifconfig=$(/sbin/ifconfig |grep "$ip")
    if [ ! -e "$VESTA/data/ips/$ip" ] && [ ! -z "$check_ifconfig" ]; then
        interface=$(/sbin/ip addr |grep $ip |awk '{print $NF}' |uniq)
        interface=$(echo "$interface" |cut -f 1 -d : |head -n 1)
        netmask=$(/sbin/ip addr |grep $ip |cut -f 2 -d / |cut -f 1 -d \ )
        netmask=$(convert_cidr $netmask)
        $BIN/v-add-sys-ip $ip $netmask $interface
    fi
done

# Updating NAT
pub_ip=$(curl -s vestacp.com/what-is-my-ip/)
if [ ! -e "$VESTA/data/ips/$pub_ip" ]; then
    if [ -z "$(grep -R "$pub_ip" $VESTA/data/ips/)" ]; then
        ip=$(ls -t $VESTA/data/ips/ |head -n1)
        $BIN/v-change-sys-ip-nat $ip $pub_ip
    fi
fi


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

exit