#!/bin/bash
#
#  NAME
#       trn-break-net - break the network configuration
#
#  USAGE
#       trn-break-net 1|2|3
#
#  DESCRIPTION
#       This script breaks the network configuration in three specific
#       ways. The choice is based on the argument passed.
#       This script is used in the criterion test that teaches learners
#       how to troubleshoot the network configuration.
#
#  CHANGELOG
#       * Tue Aug 31 2010 Robert Locke <rlocke@redhat.com>
#       - Original code

ARG=$1

# Get some current information
IFACE=eth0
IPADDR=$(ip -4 addr ls dev $IFACE | grep inet | awk '{print $2}' | cut -d/ -f1)
LOOIA=${IPADDR##*.}
IFCFG=/etc/sysconfig/network-scripts/ifcfg-${IFACE}
NAME=$(hostname -s)
VALID=server

######################################################################
#
# create a statically configure ifcfg template from which breaks
# can be derived.  on first run, stash the exisiting ifcfg file.
# the original file can be restored with "lab-break-net restore".
#
######################################################################

# stash the originally discoverd file
RESTORE=/var/tmp/.ifcfg_${IFACE}.orig
if ! [ -e $RESTORE ]; then  
	echo "storing original configuration."
	cp $IFCFG $RESTORE
fi

# generate a "static template" from which breaks can be derived.
TMPL=/var/tmp/.ifcfg_${IFACE}.tmpl
cat > $TMPL << EOF
DEVICE=$IFACE
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE=Ethernet
IPADDR=$IPADDR
PREFIX=24
GATEWAY=192.168.0.254
DNS1=192.168.0.254
DOMAIN=example.com
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System $IFACE"
EOF

# Exit out if no argument selecting form of breakage
if [ "X${ARG}" != "X1" ] && [ "X${ARG}" != "X2" ] && [ "X${ARG}" != "X3" ] && [ "X$ARG" != "Xrestore" ]; then
    cat <<EOF
$0 is a script to break your network configuration. 
It requires a single argument of 1, 2, or 3 that will break your 
system in one of three specific ways.

Usage: $0 1|2|3|restore

If multiple arguments are specified, then only the first one will 
be used.
EOF
    exit 1
fi

# Must only be run on server virtual machine
length=${#VALID}
if [ "${NAME:0:$length}" != ${VALID} ] ; then
    cat <<EOF
$0 is designed to be run on your virtual server.

Please connect to that system to run this script/exercise.
EOF
    exit 2
fi

# Must be run as root
if [ "$EUID" -ne 0 ]; then
    echo You must run this script as root
    exit 3
fi

# no longer needed with staic template
#
# # Change away from "dynamic" settings
# if grep "^BOOTPROTO.*dhcp" ${IFCFG}
# then
#   sed -i -e 's/^BOOTPROTO.*dhcp.*/# &/' ${IFCFG}
# fi

# Split to particular breakage
case $ARG in
    1)
        sed -e 's/DNS1=.*/DNS1=192.168.10.254/' $TMPL > ${IFCFG}
	restorecon ${IFCFG}
        ;;
    2)
        sed -e 's/GATEWAY=.*/GATEWAY=192.168.10.254/' $TMPL > ${IFCFG}
	restorecon ${IFCFG}
        ;;
    3)
        sed -e "s/IPADDR=.*/IPADDR=192.168.10.${LOOIA}/" $TMPL > ${IFCFG}
	restorecon ${IFCFG}
        ;;
    restore) 
        if [ -e $RESTORE ]; then
		mv $RESTORE $IFCFG
		restorecon ${IFCFG}
		echo original configuration restored.
	else
		echo could not find original configuration.
	fi
	;;
esac

# Technically the following is not needed since NM "detects" file change
service NetworkManager restart

