#!/bin/bash
# Bonding + VLAN Configuration Script (CentOS 7/8)
# Multi-language, Safe, and Flexible

CONFIG_DIR="/etc/sysconfig/network-scripts"

# ====== 1. CHỌN NGÔN NGỮ ======
choose_language() {
    echo "Chọn ngôn ngữ (Select language):"
    echo "1. Tiếng Việt"
    echo "2. English"
    read -p "Lựa chọn của bạn (Your choice): " lang_choice
    if [[ $lang_choice == "1" ]]; then
        LANG=vi
    else
        LANG=en
    fi
}

# ====== 2. HÀM HIỂN THỊ THÔNG BÁO ======
msg() {
    local key=$1
    declare -A text
    text["vi:check_nm"]="Kiểm tra NetworkManager..."
    text["en:check_nm"]="Checking NetworkManager..."
    text["vi:nm_active"]="NetworkManager đang chạy → tắt để dùng network-scripts."
    text["en:nm_active"]="NetworkManager is active → disabling it for network-scripts."
    text["vi:nm_disabled"]="Đã chuyển sang network-scripts."
    text["en:nm_disabled"]="Switched to network-scripts."
    text["vi:using_scripts"]="Đang dùng network-scripts."
    text["en:using_scripts"]="Already using network-scripts."
    text["vi:if_up"]="Các interface đang UP:"
    text["en:if_up"]="Interfaces currently UP:"
    text["vi:confirm_overwrite"]="Một số file cấu hình đã tồn tại, bạn có muốn ghi đè không? (y/n): "
    text["en:confirm_overwrite"]="Some config files already exist, overwrite? (y/n): "
    echo "${text["$LANG:$key"]}"
}

# ====== 3. KIỂM TRA NETWORKMANAGER ======
check_network_manager() {
    msg "check_nm"
    if systemctl is-active --quiet NetworkManager; then
        msg "nm_active"
        systemctl stop NetworkManager
        systemctl disable NetworkManager
        systemctl enable network
        systemctl start network
        msg "nm_disabled"
    else
        msg "using_scripts"
    fi
}

# ====== 4. LIỆT KÊ INTERFACE ĐANG UP ======
list_interfaces_up() {
    msg "if_up"
    ip link show up | grep -E "^[0-9]+:" | awk -F': ' '{print $2}' | grep -v "lo"
}

# ====== 5. XÓA CONFIG CŨ ======
cleanup_configs() {
    rm -f ${CONFIG_DIR}/*_slave* ${CONFIG_DIR}/*_vlan* ${CONFIG_DIR}/ifcfg-bond*
}

# ====== 6. TẠO CONFIG INTERFACE VẬT LÝ ======
create_physical_config() {
    local iface=$1 bond=$2
    cat <<EOF > ${CONFIG_DIR}/ifcfg-${iface}
TYPE=Ethernet
BOOTPROTO=none
NAME=${iface}
DEVICE=${iface}
ONBOOT=yes
MASTER=${bond}
SLAVE=yes
EOF
}

# ====== 7. TẠO CONFIG CHO BOND ======
create_bond_config() {
    local bond=$1 ip=$2 prefix=$3 gw=$4 dns1=$5 dns2=$6 mode=$7 lacp=$8 hash=$9
    cat <<EOF > ${CONFIG_DIR}/ifcfg-${bond}
DEVICE=${bond}
NAME=${bond}
TYPE=Bond
BONDING_MASTER=yes
BOOTPROTO=none
ONBOOT=yes
BONDING_OPTS="mode=${mode} lacp_rate=${lacp} miimon=100 xmit_hash_policy=${hash}"
EOF
    [ -n "$ip" ] && echo "IPADDR=$ip" >> ${CONFIG_DIR}/ifcfg-${bond}
    [ -n "$prefix" ] && echo "PREFIX=$prefix" >> ${CONFIG_DIR}/ifcfg-${bond}
    [ -n "$gw" ] && echo "GATEWAY=$gw" >> ${CONFIG_DIR}/ifcfg-${bond}
    [ -n "$dns1" ] && echo "DNS1=$dns1" >> ${CONFIG_DIR}/ifcfg-${bond}
    [ -n "$dns2" ] && echo "DNS2=$dns2" >> ${CONFIG_DIR}/ifcfg-${bond}
}

# ====== 8. TẠO CONFIG VLAN ======
create_vlan_config() {
    local bond=$1 vlan=$2 ip=$3 prefix=$4 gw=$5 dns1=$6 dns2=$7
    cat <<EOF > ${CONFIG_DIR}/ifcfg-${bond}.${vlan}
DEVICE=${bond}.${vlan}
NAME=${bond}.${vlan}
BOOTPROTO=none
ONBOOT=yes
VLAN=yes
PHYSDEV=${bond}
IPADDR=${ip}
PREFIX=${prefix}
EOF
    [ -n "$gw" ] && echo "GATEWAY=$gw" >> ${CONFIG_DIR}/ifcfg-${bond}.${vlan}
    [ -n "$dns1" ] && echo "DNS1=$dns1" >> ${CONFIG_DIR}/ifcfg-${bond}.${vlan}
    [ -n "$dns2" ] && echo "DNS2=$dns2" >> ${CONFIG_DIR}/ifcfg-${bond}.${vlan}
}

# ====== 9. MAIN ======
choose_language
check_network_manager
list_interfaces_up

read -p "$( [ $LANG == vi ] && echo 'Nhập tên bond (vd: bond0): ' || echo 'Enter bond name (e.g., bond0): ' )" bond_name
read -p "$( [ $LANG == vi ] && echo 'Nhập các interface join bond (cách nhau bằng khoảng trắng): ' || echo 'Enter interfaces to join bond (space-separated): ' )" ifaces

# Confirm overwrite if config exists
existing_files=()
for f in ${CONFIG_DIR}/ifcfg-${bond_name} ${ifaces[@]/#/${CONFIG_DIR}/ifcfg-}; do
    [ -f "$f" ] && existing_files+=("$f")
done
if [ ${#existing_files[@]} -gt 0 ]; then
    read -p "$(msg confirm_overwrite)" confirm
    [[ $confirm != "y" ]] && exit 1
fi

# Cleanup configs
cleanup_configs

# Create physical ifaces config
for iface in $ifaces; do
    create_physical_config $iface $bond_name
done

# Bonding mode options
echo "Bonding modes: 0=balance-rr, 1=active-backup, 2=balance-xor, 3=broadcast, 4=802.3ad(LACP), 5=balance-tlb, 6=balance-alb"
read -p "Select bonding mode [4]: " bond_mode
bond_mode=${bond_mode:-4}

read -p "Enter lacp_rate (slow=0, fast=1) [1]: " lacp_rate
lacp_rate=${lacp_rate:-1}

read -p "Enter xmit_hash_policy [1=layer2, 2=layer3+4, etc] [1]: " hash_policy
hash_policy=${hash_policy:-1}

# VLAN or Basic Bond?
read -p "$( [ $LANG == vi ] && echo 'Bond này có dùng VLAN không? (y/n): ' || echo 'Does this bond use VLAN? (y/n): ' )" use_vlan

if [[ $use_vlan == "y" ]]; then
    create_bond_config $bond_name "" "" "" "" "" $bond_mode $lacp_rate $hash_policy
    while true; do
        read -p "VLAN ID: " vlan_id
        read -p "IP Address: " ip_addr
        read -p "Prefix: " prefix
        read -p "Gateway (optional): " gw
        read -p "DNS1 (optional): " dns1
        read -p "DNS2 (optional): " dns2
        create_vlan_config $bond_name $vlan_id $ip_addr $prefix $gw $dns1 $dns2
        read -p "Add another VLAN? (y/n): " more
        [[ $more != "y" ]] && break
    done
else
    read -p "IP Address: " ip_addr
    read -p "Prefix: " prefix
    read -p "Gateway: " gw
    read -p "DNS1 (optional): " dns1
    read -p "DNS2 (optional): " dns2
    create_bond_config $bond_name $ip_addr $prefix $gw $dns1 $dns2 $bond_mode $lacp_rate $hash_policy
fi

systemctl restart network
echo "Bonding configuration completed."
cat /proc/net/bonding/$bond_name

