#!/bin/bash
# info: update system ip
# options: [USER] [IP_STATUS]
#
# 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                     #
#----------------------------------------------------------#

# Argument defenition
user=${1-admin}
ip_status=${2-shared}

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


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

check_args '0' "$#" '[USER] [IP_STATUS]'
validate_format 'user' 'ip_status'
is_object_valid 'user' 'USER' "$user" "$user"


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

ip_list=$(/sbin/ifconfig | grep 'inet addr:' | cut -f 2 -d : | \
    cut -f 1 -d ' '| grep -v 127.0.0.1)

for ip in $ip_list; do
    if [ ! -e "$VESTA/data/ips/$ip" ]; then
        iface=$(/sbin/ifconfig |grep -B1 -w $ip |head -n1 |cut -f1 -d ' ')
        interface=$(echo "$iface" | cut -f 1 -d :)
        mask=$(/sbin/ifconfig |grep -w $ip |awk -F "Mask:" '{print $2}')
        $BIN/v-add-sys-ip $ip $mask $interface
    fi

    # TBD: revers comparation
done


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

exit
