#!/bin/bash

# Color codes
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
BLUE="\e[34m"
RESET="\e[0m"

echo -e "${BLUE}--- Bonding + VLAN Configuration Script for CentOS 7 ---${RESET}"

read -p "$(echo -e ${YELLOW}Enter physical interfaces (e.g., eth0 eth1):${RESET} ) " phys_ifaces

echo -e "${BLUE}Select configuration mode:${RESET}"
echo -e "  ${GREEN}1) Basic Bond${RESET}"
echo -e "  ${GREEN}2) VLAN tagging on Bond${RESET}"
read -p "$(echo -e ${YELLOW}Choose option (1 or 2):${RESET} ) " bond_mode

bond_cfg="/etc/sysconfig/network-scripts/ifcfg-bond0"

echo -e "${BLUE}Cleaning up old bonding and VLAN configuration files...${RESET}"
find /etc/sysconfig/network-scripts/ -type f \( \
    -name 'ifcfg-Bond_*' -o \
    -name 'ifcfg-VLAN_*' -o \
    -name 'ifcfg-bond0_slave*' \
\) -exec rm -f {} \;

# Create bond0 base configuration
cat > $bond_cfg <<EOF
DEVICE=bond0
NAME=bond0
TYPE=Bond
BONDING_MASTER=yes
BOOTPROTO=none
ONBOOT=yes
BONDING_OPTS="mode=802.3ad lacp_rate=fast miimon=100 xmit_hash_policy=1 fail_over_mac=0"
EOF

# Configure each physical interface
for iface in $phys_ifaces; do
  iface_cfg="/etc/sysconfig/network-scripts/ifcfg-$iface"
  echo -e "${GREEN}Configuring $iface_cfg...${RESET}"
  cat > $iface_cfg <<EOF
DEVICE=$iface
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
EOF
done

if [ "$bond_mode" == "1" ]; then
  echo -e "${YELLOW}You selected Basic Bond configuration.${RESET}"

  read -p "Enter IP Address for bond0 (e.g., 192.168.1.100): " ip_addr
  read -p "Enter Subnet prefix (e.g., 24): " subnet
  read -p "Enter Gateway (e.g., 192.168.1.1): " gateway
  read -p "Enter Nameserver1 (e.g., 8.8.8.8): " ns1
  read -p "Enter Nameserver2 (optional): " ns2

  echo "IPADDR=$ip_addr" >> $bond_cfg
  echo "PREFIX=$subnet" >> $bond_cfg
  echo "GATEWAY=$gateway" >> $bond_cfg
  echo "DNS1=$ns1" >> $bond_cfg
  [ -n "$ns2" ] && echo "DNS2=$ns2" >> $bond_cfg

elif [ "$bond_mode" == "2" ]; then
  echo -e "${YELLOW}You selected VLAN Tagged Bond configuration.${RESET}"

  read -p "Enter VLAN IDs to create (e.g., 28 29 30): " vlan_list

  # Remove IP-related lines if any exist in bond0
  sed -i '/IPADDR/d;/PREFIX/d;/GATEWAY/d;/DNS1/d;/DNS2/d' $bond_cfg

  for vlan in $vlan_list; do
    vlan_iface="bond0.$vlan"
    vlan_cfg="/etc/sysconfig/network-scripts/ifcfg-$vlan_iface"
    
    echo -e "${GREEN}Configuring $vlan_iface...${RESET}"
    read -p "Enter IP Address for $vlan_iface (e.g., 192.168.$vlan.100): " ip_addr
    read -p "Enter Subnet prefix (e.g., 24): " subnet
    read -p "Enter Gateway (optional): " gateway
    read -p "Enter Nameserver1 (optional): " ns1
    read -p "Enter Nameserver2 (optional): " ns2

    cat > $vlan_cfg <<EOF
DEVICE=$vlan_iface
BOOTPROTO=none
ONBOOT=yes
IPADDR=$ip_addr
PREFIX=$subnet
VLAN=yes
PHYSDEV=bond0
EOF

    [ -n "$gateway" ] && echo "GATEWAY=$gateway" >> $vlan_cfg
    [ -n "$ns1" ] && echo "DNS1=$ns1" >> $vlan_cfg
    [ -n "$ns2" ] && echo "DNS2=$ns2" >> $vlan_cfg
  done

else
  echo -e "${RED}Invalid selection. Exiting.${RESET}"
  exit 1
fi

echo -e "${YELLOW}Restarting network service to apply changes...${RESET}"
systemctl restart network

echo -e "${GREEN}Bonding configuration completed (Mode: $bond_mode)${RESET}"
if [ "$bond_mode" == "2" ]; then
  echo -e "${BLUE}Created VLAN interfaces:${RESET}"
  for vlan in $vlan_list; do
    echo -e "  - ${GREEN}bond0.$vlan${RESET}"
  done
fi

echo -e "${BLUE}Check bonding status with:${RESET} cat /proc/net/bonding/bond0"

