#!/bin/bash
#
# eth2em-rename.sh: Example script to find and replace ethN names 
# with emX names based on mapping in $map_file
#
# Copyright (C) 2011-2012 Dell, Inc.
# by Charles Rose <charles_rose@dell.com>
#
# http://linux.dell.com/biosdevname/scripts
#
# Usage:
#	# ./eth2em-rename.sh file.conf
#
# Output file:
#	em_file.conf
# will have the ethN names replaced eith emX|pXpY names
# 
# $map_file format is:
# <ethN> <emN|pXpY> <MAC>
#
# <ethN>: is the older name used by the kernel
# <emX|pXpY>: is the new name of the interface
# <MAC>: MAC address of the interface
#
# $map_file can be generated by eth2em-map.sh from URL above
#

# Change to point to map file
map_file=/tmp/eth.map

# Do not edit beyond this

prefix="em_"
new_file="${prefix}${1}"

if [ -s "$1" ]; then
	cp $1 $new_file
else
	echo "Usage: eth2em-rename.sh <file_with_eth_names>"
	exit 1
fi

if [ ! -s "$map_file" ]; then
	echo "mapfile not found. Please set \$map_file."
	exit 1
fi

i=0
while read line
do
	name[$i]=$(echo $line| awk '{print $1}')
	name[$i+1]=$(echo $line| awk '{print $2}')
	i=$i+2
done < $map_file

for ((a=0; a <= $i-1; a=a+2))
do 
	sed -i "s/${name[$a]}/${name[$a+1]}/g" $new_file
done
