#!/bin/sh
# Shell script to convert INE vol1 configs to Dynamips format
# Changes interface names
# Created by Daniel Dib @ http://reaper81.wordpress.com

newdir="/home/reaper/INE/configs/Dynamips"


#Check if configs exist
for config in r1 r2 r3 r4 r5 r6 sw1 sw2 sw3 sw4 bb1 bb2 bb3
do
  if [ ! -r "$config.txt" ]; then
    doerror "Configuration file $config.txt does not exist"
  fi
done

echo "Files exist, continuing script..."

echo "Creating $newdir..."

mkdir -p $newdir 2>/dev/null

echo "Correcting configurations..."

echo "Starting with r1-r6..."

sed "
/FastEthernet0\/0/a \ speed 100\n duplex full
" r1.txt >$newdir/r1.txt

sed "
/FastEthernet0\/0/a \ speed 100\n duplex full
" r2.txt >$newdir/r2.txt

sed "
/FastEthernet0\/0/a \ speed 100\n duplex full
/FastEthernet0\/1/a \ speed 100\n duplex full
" r3.txt >$newdir/r3.txt

sed "
s/Serial0\/0\/0/Serial0\/0/g
s/Serial0\/1\/0/Serial0\/1/g
/FastEthernet0\/0/a \ speed 100\n duplex full
/FastEthernet0\/1/a \ speed 100\n duplex full
" r4.txt >$newdir/r4.txt

sed "
s/Serial0\/0\/0/Serial0\/0/g
s/Serial0\/1\/0/Serial0\/1/g
/FastEthernet0\/0/a \ speed 100\n duplex full
/FastEthernet0\/1/a \ speed 100\n duplex full
" r5.txt >$newdir/r5.txt

sed "
s/Serial0\/0\/0/Serial0\/0/g
/FastEthernet0\/0/a \ speed 100\n duplex full
/FastEthernet0\/1/a \ speed 100\n duplex full
" r6.txt >$newdir/r6.txt

echo "Routers done, starting with switches..."

sed "
s/FastEthernet0\/1$/FastEthernet1\/1/g
s/FastEthernet0\/3$/FastEthernet1\/3/g
s/FastEthernet0\/5$/FastEthernet1\/5/g
s/FastEthernet0\/13/FastEthernet1\/7/g
s/FastEthernet0\/14/FastEthernet1\/8/g
s/FastEthernet0\/15/FastEthernet1\/9/g
s/FastEthernet0\/16/FastEthernet1\/10/g
s/FastEthernet0\/17/FastEthernet1\/11/g
s/FastEthernet0\/18/FastEthernet1\/12/g
s/FastEthernet0\/19/FastEthernet1\/13/g
s/FastEthernet0\/20/FastEthernet1\/14/g
s/FastEthernet0\/21/FastEthernet1\/15/g
s/switchport mode dynamic desirable/switchport mode trunk/g
s/switchport host/switchport mode access/g
/FastEthernet1\/1$/a \ speed 100\n duplex full
/FastEthernet1\/3$/a \ speed 100\n duplex full
/FastEthernet1\/5$/a \ speed 100\n duplex full
/FastEthernet1\/7$/a \ speed 100\n duplex full
/FastEthernet1\/8$/a \ speed 100\n duplex full
/FastEthernet1\/9$/a \ speed 100\n duplex full
/FastEthernet1\/10/a \ speed 100\n duplex full
/FastEthernet1\/11/a \ speed 100\n duplex full
/FastEthernet1\/12/a \ speed 100\n duplex full
/FastEthernet1\/13/a \ speed 100\n duplex full
/FastEthernet1\/14/a \ speed 100\n duplex full
/FastEthernet1\/15/a \ speed 100\n duplex full
" sw1.txt >$newdir/sw1.txt

sed "
s/FastEthernet0\/24/FastEthernet1\/0/g
s/FastEthernet0\/2$/FastEthernet1\/2/g
s/FastEthernet0\/4$/FastEthernet1\/4/g
s/FastEthernet0\/6$/FastEthernet1\/6/g
s/FastEthernet0\/13/FastEthernet1\/7/g
s/FastEthernet0\/14/FastEthernet1\/8/g
s/FastEthernet0\/15/FastEthernet1\/9/g
s/FastEthernet0\/16/FastEthernet1\/10/g
s/FastEthernet0\/17/FastEthernet1\/11/g
s/FastEthernet0\/18/FastEthernet1\/12/g
s/FastEthernet0\/19/FastEthernet1\/13/g
s/FastEthernet0\/20/FastEthernet1\/14/g
s/FastEthernet0\/21/FastEthernet1\/15/g
s/switchport mode dynamic desirable/switchport mode trunk/g
s/switchport host/switchport mode access/g
/FastEthernet1\/0$/a \ speed 100\n duplex full
/FastEthernet1\/2$/a \ speed 100\n duplex full
/FastEthernet1\/4$/a \ speed 100\n duplex full
/FastEthernet1\/7$/a \ speed 100\n duplex full
/FastEthernet1\/8$/a \ speed 100\n duplex full
/FastEthernet1\/9$/a \ speed 100\n duplex full
/FastEthernet1\/10/a \ speed 100\n duplex full
/FastEthernet1\/11/a \ speed 100\n duplex full
/FastEthernet1\/12/a \ speed 100\n duplex full
/FastEthernet1\/13/a \ speed 100\n duplex full
/FastEthernet1\/14/a \ speed 100\n duplex full
/FastEthernet1\/15/a \ speed 100\n duplex full
" sw2.txt >$newdir/sw2.txt

sed "
s/FastEthernet0\/24/FastEthernet1\/0/g
s/FastEthernet0\/3$/FastEthernet1\/3/g
s/FastEthernet0\/5$/FastEthernet1\/5/g
s/FastEthernet0\/13/FastEthernet1\/7/g
s/FastEthernet0\/14/FastEthernet1\/8/g
s/FastEthernet0\/15/FastEthernet1\/9/g
s/FastEthernet0\/16/FastEthernet1\/10/g
s/FastEthernet0\/17/FastEthernet1\/11/g
s/FastEthernet0\/18/FastEthernet1\/12/g
s/FastEthernet0\/19/FastEthernet1\/13/g
s/FastEthernet0\/20/FastEthernet1\/14/g
s/FastEthernet0\/21/FastEthernet1\/15/g
s/switchport mode dynamic desirable/switchport mode trunk/g
s/switchport host/switchport mode access/g
/FastEthernet1\/0$/a \ speed 100\n duplex full
/FastEthernet1\/3$/a \ speed 100\n duplex full
/FastEthernet1\/5$/a \ speed 100\n duplex full
/FastEthernet1\/7$/a \ speed 100\n duplex full
/FastEthernet1\/8$/a \ speed 100\n duplex full
/FastEthernet1\/9$/a \ speed 100\n duplex full
/FastEthernet1\/10/a \ speed 100\n duplex full
/FastEthernet1\/11/a \ speed 100\n duplex full
/FastEthernet1\/12/a \ speed 100\n duplex full
/FastEthernet1\/13/a \ speed 100\n duplex full
/FastEthernet1\/14/a \ speed 100\n duplex full
/FastEthernet1\/15/a \ speed 100\n duplex full
" sw3.txt >$newdir/sw3.txt

sed "
s/FastEthernet0\/4$/FastEthernet1\/4/g
s/FastEthernet0\/6$/FastEthernet1\/6/g
s/FastEthernet0\/13/FastEthernet1\/7/g
s/FastEthernet0\/14/FastEthernet1\/8/g
s/FastEthernet0\/15/FastEthernet1\/9/g
s/FastEthernet0\/16/FastEthernet1\/10/g
s/FastEthernet0\/17/FastEthernet1\/11/g
s/FastEthernet0\/18/FastEthernet1\/12/g
s/FastEthernet0\/19/FastEthernet1\/13/g
s/FastEthernet0\/20/FastEthernet1\/14/g
s/FastEthernet0\/21/FastEthernet1\/15/g
s/switchport mode dynamic desirable/switchport mode trunk/g
s/switchport host/switchport mode access/g
/FastEthernet1\/4$/a \ speed 100\n duplex full
/FastEthernet1\/6$/a \ speed 100\n duplex full
/FastEthernet1\/7$/a \ speed 100\n duplex full
/FastEthernet1\/8$/a \ speed 100\n duplex full
/FastEthernet1\/9$/a \ speed 100\n duplex full
/FastEthernet1\/10/a \ speed 100\n duplex full
/FastEthernet1\/11/a \ speed 100\n duplex full
/FastEthernet1\/12/a \ speed 100\n duplex full
/FastEthernet1\/13/a \ speed 100\n duplex full
/FastEthernet1\/14/a \ speed 100\n duplex full
/FastEthernet1\/15/a \ speed 100\n duplex full
" sw4.txt >$newdir/sw4.txt

echo "Switches done, starting with backbone..."

sed "
s/Serial6/Serial1\/1/g
s/Serial9/Serial1\/0/g
/Serial0$/,/^\!$/d
/Serial1$/,/^\!$/d
/Serial2$/,/^\!$/d
/Serial3$/,/^\!$/d
/Serial4$/,/^\!$/d
/Serial5$/,/^\!$/d
/Serial7$/,/^\!$/d
/Serial8$/,/^\!$/d
" bb1.txt >$newdir/bb1.txt

sed "
s/Ethernet0/FastEthernet0\/0/g
/FastEthernet0\/0/a \ speed 100\n duplex full
" bb2.txt >$newdir/bb2.txt

sed "
s/Serial0/Serial1\/0/g
s/Ethernet0/FastEthernet0\/0/g
/FastEthernet0\/0/a \ speed 100\n duplex full
" bb3.txt >$newdir/bb3.txt
