#!/bin/bash
# info: delete system ip
# options: IP
#
# The function for deleting a system ip. It does not allow to delete first ip
# on interface and do not allow to delete ip which is used by a web domain.


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

# Argument defenition
ip=$1

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


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

check_args '1' "$#" 'IP'
validate_format 'ip'
is_ip_valid "$ip"
is_ip_key_empty '$U_WEB_DOMAINS'
is_ip_key_empty '$U_SYS_USERS'


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

# Get ip owner
user="$(get_ip_value '$OWNER')"
ip_status="$(get_ip_value '$STATUS')"

# Deleting interface
interface=$(/sbin/ifconfig | grep -B1 "dr:$ip " | head -n1 | cut -f1 -d \ )
if [ ! -z "$interface" ] && [ -z "$(echo $interface |cut -s -f2 -d :)" ]; then
    echo "Error: can't delete main IP address"
    log_event "$E_FORBIDEN" "$EVENT"
    exit $E_FORBIDEN
fi
if [ ! -z "$interface" ]; then
    /sbin/ifconfig $interface down

    # Deleting startup conf on RHEL/CentOS/Fedora
    if [ -e "/etc/sysconfig/network-scripts/ifcfg-$interface" ]; then
        rm -f /etc/sysconfig/network-scripts/ifcfg-$interface
    fi

    # Deleting startup conf on Debian/Ubuntu
    if [ -e "/etc/network/interfaces" ]; then
        ip_str=$(grep -n $ip$ /etc/network/interfaces |cut -f1 -d:)
        if [ ! -z "$ip_str" ]; then
            first_str=$((ip_str - 3))
            last_str=$((ip_str + 1))
            sed -i "$first_str,$last_str d" /etc/network/interfaces
        fi
    fi
fi

# Deleting vesta ip
rm -f $VESTA/data/ips/$ip

# Deleting web config
if [ ! -z "$WEB_SYSTEM" ]; then
    rm -f /etc/$WEB_SYSTEM/conf.d/$ip.conf
fi

# Deleting proxy config
if [ ! -z "$PROXY_SYSTEM" ]; then
    rm -f /etc/$PROXY_SYSTEM/conf.d/$ip.conf

    # mod_extract_forwarded
    fw_conf="/etc/$WEB_SYSTEM/conf.d/mod_extract_forwarded.conf"
    if [ -e "$fw_conf" ]; then
        ips=$(grep 'MEFaccept 127.0.0.1' $fw_conf)
        new_ips=$(echo "$ips" | sed -e "s/$ip//" )
        sed -i "s/$ips/$new_ips/g" $fw_conf
    fi

    # mod_rpaf
    rpaf_conf="/etc/$WEB_SYSTEM/mods-enabled/rpaf.conf"
    if [ -e "$rpaf_conf" ]; then
        ips=$(grep RPAFproxy_ips $rpaf_conf)
        new_ips=$(echo "$rpaf_str" | sed -e "s/$ip//")
        sed -i "s/$ips/$new_ips/g" $rpaf_conf
    fi

fi


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

# Updating user conf
if [ ! -z "$user" ]; then
    decrease_user_value "$user" '$IP_OWNED'
fi

if [ "$user" = 'admin' ]; then
    if [ "$ip_status" = 'shared' ]; then
        for user in $(ls $VESTA/data/users/); do
            decrease_user_value "$user" '$IP_AVAIL'
        done
    else
        decrease_user_value 'admin' '$IP_AVAIL'
    fi
else
    decrease_user_value "$user" '$IP_AVAIL'
    decrease_user_value 'admin' '$IP_AVAIL'
fi


# Adding task to the vesta pipe
$BIN/v-restart-web
$BIN/v-restart-proxy

# Logging
log_history "deleted system ip address $ip"
log_event "$OK" "$EVENT"

exit
