#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

Cent=`cat /etc/issue | awk '{print $1}' | sed '/^$/d' | grep Kernel`
OtherOs=`cat /etc/issue | awk '{print $1}' | sed '/^$/d'`


eth0_conf6(){
cat /dev/null > /etc/sysconfig/network-scripts/ifcfg-eth0

cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
#network-config
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
DEVICE=eth0
IPADDR={$addr}
PREFIX={$cidr}
GATEWAY={$gateway}
DNS1={$dns1}
DNS2={$dns2}

EOF
service network restart
}

eth1_conf6(){
cat /dev/null > /etc/sysconfig/network-scripts/ifcfg-eth1

cat > /etc/sysconfig/network-scripts/ifcfg-eth1 <<EOF
#network-config
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
DEVICE=eth1
IPADDR={$addr}
PREFIX={$cidr}
GATEWAY={$gateway}
DNS1={$dns1}
DNS2={$dns2}

EOF
service network restart
}

NIC(){
checkNICeth=`ip addr | awk '{print $2}' | grep eth | sed 's/://g'`

if [ "$checkNICeth" = "eth0" ];then
    eth0_conf6
elif [ "$checkNICeth" = "eth1" ];then
    eth1_conf6
else
 echo "no NIC is found!!!"
fi
}


eth0_conf7(){
cat /dev/null > /etc/sysconfig/network-scripts/ifcfg-eth0

cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
#network-config
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
DEVICE=eth0
ONBOOT=yes
IPADDR={$addr}
NETMASK={$netmask}
GATEWAY={$gateway}
DNS1={$dns1}
DNS2={$dns2}

EOF
systemctl restart network

}


ens18_conf8(){
cat /dev/null > /etc/sysconfig/network-scripts/ifcfg-ens18

cat > /etc/sysconfig/network-scripts/ifcfg-ens18 <<EOF
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
DEVICE=ens18
ONBOOT=yes
IPADDR={$addr}
NETMASK={$netmask}
GATEWAY={$gateway}
DNS1={$dns1}
DNS2={$dns2}

EOF
nmcli n off
sleep 2
nmcli n on
sleep 2
nmcli n on
sleep 2
ifdown ens18
sleep 2
ifup ens18
}


debian(){
cat /dev/null > /etc/network/interfaces

cat > /etc/network/interfaces <<EOF
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
    address {$addr}
    netmask {$netmask}
    gateway {$gateway}
    dns-nameservers {$dns1} {$dns2}

EOF

    systemctl restart networking
}



ubuntu16(){
cat > /etc/network/interfaces <<EOF
auto ens18
iface ens18 inet static
    address {$addr}
    netmask {$netmask}
    gateway {$gateway}
    dns-nameservers {$dns1} {$dns2}
EOF
   rm -f /run/network/*
   sudo /etc/init.d/networking restart
}


ubuntu18_20(){
cat /dev/null > /etc/netplan/00-installer-config.yaml

cat > /etc/netplan/00-installer-config.yaml <<EOF
# This is the network config written by 'subiquity'
network:
  ethernets:
    ens18:
      dhcp4: no
      addresses: [{$addr}/{$cidr}]
      gateway4: {$gateway}
      nameservers:
        addresses: [{$dns1}, {$dns2}]
  version: 2

EOF
    netplan apply
}


if [ "$Cent" = "Kernel" ];then
    checkOsVer=`rpm --eval '%{centos_ver}'`
    case $checkOsVer in
    6)
    NIC
    exit;;

    7)
    eth0_conf7
    exit;;

    8)
    ens18_conf8
    exit;;

    esac

elif [ "$OtherOs" = "Debian" ];then
    debian

elif [ "$OtherOs" = "Ubuntu" ];then
    checkVer=`cat /etc/os-release | grep VERSION_ID | awk '{split($0,R,"=");print R[2]}' | sed 's/"//g'`
    case $checkVer in

    16.04)
    ubuntu16
    exit;;

    18.04)
    ubuntu18_20
    exit;;

    20.04)
    ubuntu18_20
    exit;;
esac


fi
