#!/bin/bash
# ============================================================
# IPv6 Setup Script for KVM VPS
# Supports: Ubuntu, Debian, AlmaLinux, CentOS, RHEL
# Usage:    bash add-ipv6.sh <YOUR_IPv6_ADDRESS>
# ============================================================

GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'

IPV6="$1"

if [ -z "$IPV6" ]; then
  echo "Usage:   bash add-ipv6.sh <IPv6_ADDRESS>"
  echo "Example: bash add-ipv6.sh 2001:1af8:5000:a013:9:0:bdeb:9428"
  exit 1
fi

# Detect interface
if command -v netplan &>/dev/null && ls /etc/netplan/*.yaml &>/dev/null; then
  IFACE=$(grep -h "^\s*[a-z]" /etc/netplan/*.yaml | grep -v "network\|version\|ethernets\|dhcp\|address\|route\|gateway\|nameserver\|match\|optional" | awk '{print $1}' | tr -d ':' | head -1)
fi
if [ -z "$IFACE" ]; then
  IFACE=$(ip -4 route show default 2>/dev/null | awk '{print $5}' | head -1)
fi
if [ -z "$IFACE" ]; then
  IFACE=$(ip link show | grep -v lo | grep "state UP" | awk -F': ' '{print $2}' | head -1)
fi
if [ -z "$IFACE" ]; then
  echo -e "${RED}ERROR: Could not detect network interface. Contact support.${NC}"
  exit 1
fi

echo "Interface : $IFACE"
echo "IPv6      : $IPV6/96"
echo ""

# Ubuntu / Debian (netplan)
if command -v netplan &>/dev/null; then
  echo "Detected: Ubuntu/Debian (netplan)"
  mkdir -p /etc/cloud/cloud.cfg.d
  echo "network: {config: disabled}" > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
  cat > /etc/netplan/99-ipv6.yaml << EOF
network:
  version: 2
  ethernets:
    ${IFACE}:
      dhcp4: true
      addresses:
        - ${IPV6}/96
EOF
  chmod 600 /etc/netplan/99-ipv6.yaml
  netplan apply 2>&1

# AlmaLinux / CentOS / RHEL (NetworkManager)
elif command -v nmcli &>/dev/null; then
  echo "Detected: AlmaLinux/CentOS/RHEL (NetworkManager)"
  CON=$(nmcli -t -f NAME,DEVICE con show --active | grep ":${IFACE}$" | cut -d: -f1 | head -1)
  if [ -z "$CON" ]; then CON=$(nmcli -t -f NAME con show --active | grep -v lo | head -1); fi
  if [ -z "$CON" ]; then echo -e "${RED}ERROR: No active connection found.${NC}"; exit 1; fi
  echo "Connection: $CON"
  nmcli con mod "$CON" +ipv6.addresses "${IPV6}/96"
  nmcli con up "$CON"

# Debian (ifupdown)
elif [ -f /etc/network/interfaces ]; then
  echo "Detected: Debian (ifupdown)"
  if ! grep -q "$IPV6" /etc/network/interfaces; then
    cat >> /etc/network/interfaces << EOF

iface ${IFACE} inet6 static
    address ${IPV6}
    netmask 96
EOF
  fi
  ip -6 addr add "${IPV6}/96" dev "$IFACE" 2>/dev/null || true

else
  echo -e "${RED}ERROR: Unsupported OS. Contact support.${NC}"
  exit 1
fi

echo ""
echo "Verifying..."
sleep 3

V6_ADDR=$(ip -6 addr show | grep "scope global" | awk '{print $2}' | head -1)
if [ -n "$V6_ADDR" ]; then
  echo -e "${GREEN}IPv6 assigned: $V6_ADDR${NC}"
else
  echo -e "${RED}IPv6 not found. Contact support.${NC}"; exit 1
fi

echo ""
echo "Testing connectivity..."
if ping6 -c 2 -W 3 2606:4700:4700::1111 &>/dev/null; then
  echo -e "${GREEN}SUCCESS: IPv6 is working!${NC}"
  echo "Your public IPv6: $(curl -6 -s --max-time 5 https://ipv6.icanhazip.com)"
else
  echo -e "${RED}FAILED: Contact support.${NC}"; exit 1