#!/bin/bash
i=$1
ip=$2
mask=$3
code=$4
centos_add () {
code=$1
ip=$2
mask=$3
network_name=$(ip -o -4 route show to default | awk '{print $5}')
nic_dir="/etc/sysconfig/network-scripts/"
cat <<EOF > "${nic_dir}/ifcfg-${network_name}:${code}"
DEVICE=${network_name}:${code}
Type=Ethernet
BOOTPROTO=none
NM_CONTROLLED=no
ONBOOT=yes
IPADDR=${ip}
NETMASK=${mask}
EOF
ifup ${network_name}:${code}
}
centos_del () {
ip=$1
find /etc/sysconfig/network-scripts/ -type f -exec grep -q "${ip}" {} \; -exec sh -c 'ifdown "$(basename "$1")" && rm -f "$1"' sh {} +
}
opensuse_add () {
code=$1
ip=$2
mask=$3
network_name=$(ip -o -4 route show to default | awk '{print $5}')
nic_dir="/etc/sysconfig/network"
echo "IPADDR_${code}=$ip" >> $nic_dir/ifcfg-${network_name}
echo "NETMASK_${code}=$mask" >> $nic_dir/ifcfg-${network_name}
ifdown ${network_name} && ifup ${network_name}
}
opensuse_del () {
ip=$1
network_name=$(ip -o -4 route show to default | awk '{print $5}')
sed -i "/$ip/,/NETMASK/d" /etc/sysconfig/network/ifcfg-${network_name}
ifdown ${network_name} && ifup ${network_name}
}
debian_add () {
code=$1
ip=$2
mask=$3
network_name=$(ip -o -4 route show to default | awk '{print $5}')
nic_dir="/etc/network/interfaces.d"
file_count=$(ls "$nic_dir"/"$network_name"* 2>/dev/null | wc -l)
if [ $file_count -eq 0 ]; then
cp /etc/network/interfaces /etc/network/interfaces.bak && \
cat <<AOF> /etc/network/interfaces.d/${network_name}.cfg
auto ${network_name}
iface ${network_name} inet dhcp
AOF
sed -i "/^auto ${network_name}/d; /^iface ${network_name}/d" /etc/network/interfaces
ifdown ${network_name} && ifup ${network_name}
cat <<EOF > "${nic_dir}/${network_name}:${code}.cfg"
auto ${network_name}:${code}
iface ${network_name}:${code} inet static
    address ${ip}
    netmask ${mask}
EOF
ifup "${network_name}:${code}"
else
cat <<EOF > "${nic_dir}/${network_name}:${code}.cfg"
auto ${network_name}:${code}
iface ${network_name}:${code} inet static
    address ${ip}
    netmask ${mask}
EOF
ifup "${network_name}:${code}"
fi
}
debian_del () {
ip=$1
find /etc/network/interfaces.d/ -type f -exec grep -l "${ip}" {} \; \
| sed 's/\.cfg$//' | xargs -I {} sh -c 'interface_name=$(basename {} | cut -d- -f2-);ifdown $interface_name;rm -f {}.cfg'
}
ubuntu_add () {
code=$1
ip=$2
mask=$3
gateway=$(ip route list | awk '/default/{print $3}')
stat_ip=$(ip a | grep `ip route list | awk '/default/{print $5}'` | awk '/global/{print $2}')
cfg=$(grep -l `ip route list | awk '/default/{print $5}'` /etc/netplan/*.yaml)
name=$(ip route list | awk '/default/{print $5}')
subnet_mask=${mask}
prefix=0
subnet_mask_num=$(echo $subnet_mask | awk -F'.' '{print ($1 * 256^3) + ($2 * 256^2) + ($3 * 256) + $4}')
while [ $subnet_mask_num -gt 0 ]; do
    prefix=$((prefix + (subnet_mask_num & 1)))
    subnet_mask_num=$((subnet_mask_num >> 1))
done
if grep -q "dhcp4: true" $cfg ; then
cat <<EOF > $cfg
network:
  ethernets:
    ${name}:
      dhcp4: false
      addresses:
        - ${stat_ip}
      gateway4: ${gateway}
      nameservers:
        addresses: [8.8.8.8,114.114.114.114]
  version: 2
EOF
fi
cat <<EOF > /etc/netplan/$code-secondary.yaml
network:
  ethernets:
    ${name}:
      dhcp4: false
      addresses:
        - ${ip}/${prefix}
  version: 2
EOF
netplan apply
}
ubuntu_del () {
ip=$1
rm -f `grep -li "$ip" /etc/netplan/*.yaml` && netplan apply
}
if grep -qi "centos" /etc/os-release; then
    if [ "${i}" == "add" ]; then
        centos_add ${code} ${ip} ${mask}
    elif [ "${i}" == "del" ]; then
        centos_del ${ip}
    else
        echo "invalid operation parameter"
    fi
fi
if grep -qi "ubuntu" /etc/os-release; then
    if grep -qi "16.04" /etc/os-release; then
        if [ "${i}" == "add" ]; then
            debian_add ${code} ${ip} ${mask}
        elif [ "${i}" == "del" ]; then
            debian_del ${ip}
        else
            echo "invalid operation parameter"
        fi
    else
        if [ "${i}" == "add" ]; then
            ubuntu_add ${code} ${ip} ${mask}
        elif [ "${i}" == "del" ]; then
            ubuntu_del ${ip}
        else
            echo "invalid operation parameter"
        fi
    fi
fi
if grep -qi "ID=debian" /etc/os-release; then
    if [ "${i}" == "add" ]; then
        debian_add ${code} ${ip} ${mask}
    elif [ "${i}" == "del" ]; then
        debian_del ${ip}
    else
        echo "invalid operation parameter"
    fi
fi
if grep -qi "opensuse" /etc/os-release; then
    if [ "${i}" == "add" ]; then
        opensuse_add ${code} ${ip} ${mask}
    elif [ "${i}" == "del" ]; then
        opensuse_del ${ip}
    else
        echo "invalid operation parameter"
    fi
fi

rm "$0"
