#!/bin/bash

#DESC Set/Change IP, DNS, and Gateway.

IP=$1
IP2=$4
DNS=$2
GATEWAY=$3

[ "$IP" == "" ] && echo -n "Enter eth0 IP: " && read IP
[ "$IP2" == "" ] && echo -n "Enter eth1 IP: " && read IP2
[ "$DNS" == "" ] && echo -n "Enter DNS: " && read DNS
[ "$GATEWAY" == "" ] && echo -n "Enter Gateway: " && read GATEWAY

# use old ostools to make ifcfg-tfeth0 changes
/usr2/ostools/bin/updateos.pl --ipaddr=$IP --gateway=$GATEWAY --netmask=255.255.255.0 --ifname=eth0
[ "$IP2" != "" ] && /usr2/ostools/bin/updateos.pl --ipaddr=$IP2 --gateway=$GATEWAY --netmask=255.255.255.0 --ifname=eth1
sed -i "/NM_CONTROLLED=/d" /etc/sysconfig/network-scripts/ifcfg-eth0
[ "$IP2" != "" ] && sed -i "/NM_CONTROLLED=/d" /etc/sysconfig/network-scripts/ifcfg-eth1

nmcli dev set eth0 managed yes
nmcli dev mod eth0 ipv4.addr "$IP/24"
nmcli dev mod eth0 ipv4.dns $DNS
nmcli dev mod eth0 ipv4.gateway $GATEWAY
if [ "$IP2" != "" ]
then
nmcli dev set eth1 managed yes
nmcli dev mod eth1 ipv4.addr "$IP2/24"
nmcli dev mod eth1 ipv4.dns $DNS
nmcli dev mod eth1 ipv4.gateway $GATEWAY
fi
nmcli connection reload
systemctl restart NetworkManager

#Update hosts file
NAME="`hostname | cut -d"." -f1`"
IP="`hostname -I | cut -d" " -f1`"
sed -i "/$NAME/d" /etc/hosts
echo "$IP       $NAME    $NAME.teleflora.com" >>/etc/hosts

exit $?
