#!/bin/bash
# Variables ACTION = Add or Delete secondary IP, pass 'add' or 'del'
# Variables SecIPS = Secondary IP list, pass "x.x.x.x x.x.x.x ..."
# Variables SecMask = Secondary IP netmask, pass "255.255.255.0"
set -e

ACTION=$1
SecIPS=$2
SecMask=$3

if [ "${ACTION}" == "add" ]; then
    if [ "$#" -lt 3 ]; then
        echo "Syntax error: The parameters passed are abnormal.
Please pass three position variables.
    \$1 == Add or delete secondary IP, pass 'add' or 'del'
    \$2 == Secondary IP list, pass \"192.168.1.3 192.168.1.4 ...\"
    \$3 == Secondary IP netmask, pass \"255.255.255.0\"
Excmple:
    sh $0 add \"192.168.1.3 192.168.1.4\" \"255.255.255.0\""
        exit 2
    fi
elif [ "${ACTION}" == "del" ]; then
    if [ "$#" -lt 2 ]; then
        echo "Syntax error: The parameters passed are abnormal.
Please pass three position variables.
    \$1 == Add or delete secondary IP, pass 'add' or 'del'
    \$2 == Secondary IP list, pass \"192.168.1.3 192.168.1.4 ...\"
Excmple:
    sh $0 del \"192.168.1.3 192.168.1.4\" "
        exit 2
    fi
else
    echo "Unknow Operator? Please pass 'add' or 'del'"
    exit 1
fi

function CentOS_Stream_9() {
    DEVICE_NAME=$(ip -o -4 route show to default | awk '{print $5}')
    FIRST_IP_MASK=$(ip a show ${DEVICE_NAME} | awk "/global noprefixroute ${DEVICE_NAME}/{print \$2}" | cut -d '/' -f2)

    # Add Secondary IP
    if [[ ${ACTION} == "add" ]]; then
        for IP in ${SecIPS}; do
            nmcli connection modify ${DEVICE_NAME} +ipv4.addresses ${IP}/${FIRST_IP_MASK}
            nmcli connection reload
            nmcli connection up ${DEVICE_NAME}
        done

    # Del Secondary IP
    elif [[ ${ACTION} == "del" ]]; then
        for IP in ${SecIPS}; do
            nmcli connection modify ${DEVICE_NAME} -ipv4.addresses ${IP}/${FIRST_IP_MASK}
            nmcli connection reload
            nmcli connection up ${DEVICE_NAME}
        done

    else
        echo "Invalid action. Use 'add' or 'del'"
        exit 1
    fi
}

function CentOS_Stream_8() {
    # Add Secondary IP
    :
    # Del Secondary IP
}

function CentOS_7.9() {
    DEVICE_NAME=$(ip -o -4 route show to default | awk '{print $5}')
    FIRST_IP_MASK=$(ip a show ${DEVICE_NAME} | awk "/global noprefixroute ${DEVICE_NAME}/{print \$2}" | cut -d '/' -f2)

    # Add Secondary IP
    if [[ ${ACTION} == "add" ]]; then
        for IP in ${SecIPS}; do
            IP_NUM=$(echo ${IP} | awk -F'.' '{print $4}')
            cat <<EOF >/etc/sysconfig/network-scripts/ifcfg-${DEVICE_NAME}:${IP_NUM}
TYPE=Ethernet
BOOTPROTO=static
DEVICE=${DEVICE_NAME}:${IP_NUM}
ONBOOT=yes
IPADDR=${IP}
PREFIX=${FIRST_IP_MASK}
EOF
            ifup ifcfg-${DEVICE_NAME}:${IP_NUM}
        done

    # Del Secondary IP
    elif [[ ${ACTION} == "del" ]]; then
        for IP in ${SecIPS}; do
            IP_NUM=$(echo ${IP} | awk -F'.' '{print $4}')
            ip addr del ${IP}/${FIRST_IP_MASK} dev ${DEVICE_NAME}:${IP_NUM}
            rm -f $(grep -lr "${IP}" /etc/sysconfig/network-scripts/)
        done

    else
        echo "Invalid action. Use 'add' or 'del'"
        exit 1
    fi
}

function Ubuntu_20.04_22.04() {
    DEVICE_NAME=$(ip -o -4 route show to default | awk '{print $5}')
    FIRST_IP_MASK=$(ip a show ${DEVICE_NAME} | awk "/global ${DEVICE_NAME}/{print \$2}" | cut -d '/' -f2)

    # Add Secondary IP
    if [[ ${ACTION} == "add" ]]; then
        for IP in ${SecIPS}; do
            IP_NUM=$(echo ${IP} | awk -F'.' '{print $4}')
            cat <<EOF >/etc/netplan/${IP_NUM}-secondary.yaml
# Created by aorta-assist on instance boot automatically, do not edit.
network:
  ethernets:
    ${DEVICE_NAME}:
      addresses:
        - ${IP}/${FIRST_IP_MASK}
  version: 2
EOF
            netplan apply
        done

    # Del Secondary IP
    elif [[ ${ACTION} == "del" ]]; then
        for IP in ${SecIPS}; do
            rm -rf $(grep -lr "${IP}" /etc/netplan/)
            netplan apply
        done

    else
        echo "Invalid action. Use 'add' or 'del'"
        exit 1
    fi
}

function Ubuntu_16() {
    DEVICE_NAME=$(ip -o -4 route show to default | awk '{print $5}')
    FIRST_IP_MASK=$(ip a show ${DEVICE_NAME} | awk "/global ${DEVICE_NAME}/{print \$2}" | cut -d '/' -f2)

    # Add Secondary IP
    if [[ ${ACTION} == "add" ]]; then
        for IP in ${SecIPS}; do
            IP_NUM=$(echo ${IP} | awk -F'.' '{print $4}')
            cat <<EOF >/etc/network/interfaces.d/${DEVICE_NAME}:${IP_NUM}.cfg
# Created by aorta-assist on instance boot automatically, do not edit.
auto ${DEVICE_NAME}:${IP_NUM}
iface ${DEVICE_NAME}:${IP_NUM} inet static
    address ${IP}
    netmask ${SecMask}
EOF
            ifup ${DEVICE_NAME}:${IP_NUM}
        done

    # Del Secondary IP
    elif [[ ${ACTION} == "del" ]]; then
        for IP in ${SecIPS}; do
            IP_NUM=$(echo ${IP} | awk -F'.' '{print $4}')
            ifdown ${DEVICE_NAME}:${IP_NUM}
            rm -rf $(grep -lr "${IP}" /etc/network)
        done

    else
        echo "Invalid action. Use 'add' or 'del'"
        exit 1
    fi
}

# Centos Stream 9
if grep -qi 'CentOS Stream 9' /etc/os-release; then
    CentOS_Stream_9

# Centos Stream 8
elif grep -qi 'CentOS Stream 8' /etc/os-release; then
    CentOS_Stream_8

# Centos 7.9
elif grep -qi 'CentOS Linux 7 (Core)' /etc/os-release; then
    CentOS_7.9

# Ubuntu 24.04 TLS
elif grep -qi '24.04' /etc/os-release; then
    Ubuntu_24.04

# Ubuntu 22.04 TLS
elif grep -qi '22.04' /etc/os-release; then
    Ubuntu_20.04_22.04

# Ubuntu 20.04 TLS
elif grep -qi '20.04' /etc/os-release; then
    Ubuntu_20.04_22.04

# Ubuntu 18.04 TLS
elif grep -qi '18.04' /etc/os-release; then
    Ubuntu_18

# Ubuntu 16.04 TLS
elif grep -qi '16.04' /etc/os-release; then
    # 要修改模板 interfaces
    Ubuntu_16

else
    echo "Unsupported OS."
    exit 1
fi
