#!/bin/bash
# info: update system ip
# options: [NONE]
#
# The function scans configured ip in the system and register them with hestia
# 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 $HESTIA/func/main.sh
source $HESTIA/func/ip.sh
source $HESTIA/conf/hestia.conf


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


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

# Listing system ip addresses
ips=$(/sbin/ip addr |grep 'inet ' |grep global |awk '{print $2}' |cut -f1 -d/)
v_ips=$(ls $HESTIA/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 "$new" ]; then
    mv $HESTIA/data/ips/$old $HESTIA/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
        for user in $($HESTIA/bin/v-list-sys-users plain); do
            sed -i "s/$old/$new/g" $HESTIA/data/users/$user/web.conf
            $BIN/v-rebuild-web-domains $user no
        done
        $BIN/v-restart-proxy
        $BIN/v-restart-web
    fi

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

# Adding system IP
for ip in $ips; do
    check_ip=$(/sbin/ip addr list |grep "$ip")
    if [ ! -e "$HESTIA/data/ips/$ip" ] && [ ! -z "$check_ip" ]; 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 --ipv4 -s https://ip.hestiacp.com/)
if [ ! -e "$HESTIA/data/ips/$pub_ip" ]; then
    if [ -z "$(grep -R "$pub_ip" $HESTIA/data/ips/)" ]; then
        ip=$(ls -t $HESTIA/data/ips/ |head -n1)
        $BIN/v-change-sys-ip-nat $ip $pub_ip
    fi
fi


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

exit
