#!/bin/bash
###Script by Ngo Anh Tuan-lowendviet.com.


#Change log:
#Update 2022-Oct-05: Initialize script

if [[ -f /etc/os-release ]]; then
    # freedesktop.org and systemd
    . /etc/os-release
    OS=$NAME
    VER=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
    # linuxbase.org
    OS=$(lsb_release -si)
    VER=$(lsb_release -sr)
elif [[ -f /etc/lsb-release ]]; then
    # For some versions of Debian/Ubuntu without lsb_release command
    . /etc/lsb-release
    OS=$DISTRIB_ID
    VER=$DISTRIB_RELEASE
elif [[ -f /etc/debian_version ]]; then
    # Older Debian/Ubuntu/etc.
    OS=Debian
    VER=$(cat /etc/debian_version)
elif [[ -f /etc/SuSe-release ]]; then
    # Older SuSE/etc.
    ...
elif [[ -f /etc/redhat-release ]]; then
    # Older Red Hat, CentOS, etc.
    ...
else
    # Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
    OS=$(uname -s)
    VER=$(uname -r)
fi

if [[ "$OS" = "CentOS Linux" ]]; then
  network_card="eth0"
  yum install -y subnetcalc > /dev/null
elif [ "$OS" = "Ubuntu" -a "$VER" = "18.04" ]; then
  apt-get install -y subnetcalc > /dev/null
  network_card=$(ip -o link show | awk '{print $2,$9}' | grep ens | cut -d: -f1)
elif [ "$OS" = "Ubuntu" -a "$VER" = "20.04" ]; then
  apt-get install -y subnetcalc > /dev/null
  network_card=$(ip -o link show | awk '{print $2,$9}' | grep ens | cut -d: -f1)
fi

selection="1000"

while [[ $selection -ne 4 ]] ; do
  currentIPv6=$(ip addr show dev eth0 | sed -e's/^.*inet6 \([^ ]*\).*$/\1/;t;d' | grep -v fe80)

  echo -e "======== IPV6 SETTING BY LowendViet ========"
  echo -e "======== Lua chon MENU tuong ung ========"
  echo -e "1. Hien thi IPv6 hien tai."
  echo -e "2. Doi IPv6."
  echo -e "3. Kiem tra IPv6 hoat dong, "
  echo -e "4. Thoat."
  read  selection
  if [[ $selection -eq 1 ]]; then
    echo "$currentIPv6" | while IFS= read -r line ;
    do
        prefix=$(subnetcalc $line | grep Network | cut -d "=" -f2 | cut -d "/" -f1 | awk '{$1=$1};1')
        ipv6mask=$(echo $line | cut -d "/" -f2)
        echo -e "    IPv6:" $line;
        echo "    Prefix: "$prefix
        echo "    Netmask: "$ipv6mask
    done
  elif [[ $selection -eq 2 ]]; then
    echo -e "Nhap dia chi IPv6 (Vi du: fe80::8b56:f60c:6bf1:de20/64):"
    read ipv6
    if [[ "$ipv6" == *\/* ]]; then
      ipv6mask=$(echo $ipv6 | cut -d "/" -f1)
    else
      echo -e "Nhap IPv6 netmask (Mac dinh: ${ipv6mask} neu bo trong):"
      read t
      if [[ -z "$t" ]]; then
        ipv6mask="$t"
      fi
      ipv6="${ipv6}/${ipv6mask}"
    fi
    echo -e "Nhap IPv6 gateway:"
    read ipv6gw
    if [[ "$OS" = "CentOS Linux" ]]; then
      if grep -q IPV6ADDR "/etc/sysconfig/network-scripts/ifcfg-eth0"; then
        sed -i "/IPV6ADDR=/c\IPV6ADDR=${ipv6}" /etc/sysconfig/network-scripts/ifcfg-eth0
        sed -i "/IPV6_DEFAULTGW=/c\IPV6_DEFAULTGW=${ipv6gw}" /etc/sysconfig/network-scripts/ifcfg-eth0
      else
        echo -e 'IPV6INIT="yes"' >> /etc/sysconfig/network-scripts/ifcfg-eth0
        echo -e 'IPV6_AUTOCONF="no"' >> /etc/sysconfig/network-scripts/ifcfg-eth0
        echo -e "IPV6ADDR=${ipv6}" >> /etc/sysconfig/network-scripts/ifcfg-eth0
        echo -e "IPV6_DEFAULTGW=${ipv6gw}" >> /etc/sysconfig/network-scripts/ifcfg-eth0
      fi
    fi
    systemctl restart network
  elif [[ $selection -eq 3 ]]; then
    ping6 ipv6.google.com -c4
  fi
done

