#!/bin/bash -x
#we can use !/bin/bash -x to parse it step by step
# Script to parse csv to create spa$MA address config files for provisionig

#Here are the some sample.cfg that i want to create

#<flat-profile>
#<GPP_A> 12345678 
#</GPP_A>
#<Profile_Rule ua="na"> /spa$MA.cfg
#</Profile_Rule>
#<Display_Name_1_ ua="na"> Dokimis Dokimidis</Display_Name_1_>
#<User_ID_1_ ua="na"> 123</User_ID_1_>
#<Password_1_ ua="na"> 123</Password_1_>
#</flat-profile>

#In order to do it a little easier or more difficult it depends on point of view; we will declare a variable for every setting we need to change 
#We will use the variables for opening and closing tags for configuration changes
#General opens and closes flat profile!
#Variable declaration

first=flat-profile
second=GPP_A
third=Profile_Rule
forth=Display_Name_1_
fifth=User_ID_1_
sixth=Password_1_
seventh=Proxy_1_
general='ua="na"'

#Sample File to parse

#IpAddress	Mac_Address		Sip Server	Last Name - First Name	Username	Password
#192.168.4.1	00:06:5B:8C:1E:2C	192.168.4.1	Tsakalos Stratis	314		314
#192.168.4.5	00:06:5B:8C:1E:2D	192.168.4.1	Test User		255		255

#To delete the first line
sed -i '1d' dokimi.csv

#script to read dokimi.csv file and parse it . Cut uses the ";" character as delimiter

while read line 

do

#we will use it in GPP_A in order to use it for search and replace scripts
ip_address=`echo $line|cut -d';' -f1` 
mac_address=`echo $line|cut -d';' -f2 | tr "[:upper:]" "[:lower:]"`
proxy=`echo $line|cut -d';' -f3`  
display=`echo $line|cut -d';' -f4`
number=`echo $line|cut -d';' -f5`
password=`echo $line|cut -d';' -f6`

touch $mac_address.cfg
echo "<$first>" >> $mac_address.cfg 
echo "<$second>$ip_address</$second>" >> $mac_address.cfg
echo "<$third $general>tftp://$proxy/spa$mac_address.cfg</$forth>" >> $mac_address.cfg
echo "<$forth $general>$display</$forth>" >> $mac_address.cfg
echo "<$sixth $general>$password</$sixth>" >> $mac_address.cfg
echo "<$seventh $general>$proxy</$seventh>" >> $mac_address.cfg

#proxy and registration you can skip them if you like!

#echo "<Proxy_1_ group="Ext_1/Proxy_and_Registration">$proxy</Proxy_1_>" >> $mac_address.cfg
#echo "<Use_Outbound_Proxy_1_ group="Ext_1/Proxy_and_Registration">No</Use_Outbound_Proxy_1_>" >> $mac_address.cfg
#echo "<Outbound_Proxy_1_ group="Ext_1/Proxy_and_Registration"/>" >> $mac_address.cfg
#echo "<Use_OB_Proxy_In_Dialog_1_ group="Ext_1/Proxy_and_Registration">Yes</Use_OB_Proxy_In_Dialog_1_>" >> $mac_address.cfg
#echo "<Register_1_ group="Ext_1/Proxy_and_Registration">Yes</Register_1_>" >> $mac_address.cfg
#echo "<Make_Call_Without_Reg_1_ group="Ext_1/Proxy_and_Registration">No</Make_Call_Without_Reg_1_>" >> $mac_address.cfg

#closing config 

echo "<$fifth $general>$number</$fifth>" >> $mac_address.cfg
echo "</$first>" >> $mac_address.cfg
	
done < dokimi.csv


