#!/bin/bash
# Copyright (C) 2008 - 2015 .org
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

echo ""
echo "----------------------------------------"
echo " Network fix"
echo " dhcp - reset eth0 to dhcp"
echo " rebuild - rebuild a bad network config"
echo "----------------------------------------"
echo ""

function echolog {
echo "`date +'%m/%d/%Y %H:%M:%S'` - $1"
}

case $1 in
  dhcp)
	echolog "Setting eth0 to DHCP..."
	sed -i -e 's/^BOOTPROTO=none/BOOTPROTO=dhcp/' /etc/sysconfig/network-scripts/ifcfg-eth0
	echolog "Done..."
      ;;
	  
  rebuild)
	echolog "Rebuilding eth0 network config..."
	MACRAW=`cat /sys/class/net/eth0/address`

	MAC=`echo $MACRAW | tr  '[a-z]' '[A-Z]'`

cat > /var/tmp/ifcfg-eth0.tmp <<EOF
IPV6INIT="yes"
NM_CONTROLLED="yes"
HWADDR="$MAC"
BOOTPROTO="dhcp"
DEVICE="eth0"
ONBOOT="yes"
EOF

	mv /var/tmp/ifcfg-eth0.tmp /etc/sysconfig/network-scripts/ifcfg-eth0
	/bin/systemctl restart network.service
	echolog "Done..."
      ;;
  *)
	echolog "Unknown option."
      ;;	  
esac
