#!/bin/bash




stockVar() {
iface=`echo $tempVar | cut -d '/' -f 1 | cut -b 1,2,3`
if [[ "$iface" = "eth" ]]
then
	mode=`echo $tempVar | cut -d '/' -f 2`
	if [[ $mode = "dhcp" ]] || [[ $mode = "static" ]]
	then
		if [[ $mode = "static" ]]
		then
			tempIp=`echo $tempVar | cut -d '/' -f 3`
			tempMask=`echo $tempVar | cut -d '/' -f 4`
			tempGate=`echo $tempVar | cut -d '/' -f 5`
			if [[ -z $tempIp ]] || [[ -z $tempMask ]] || [[ -z $tempGate ]]
			then
				echo "Error : if one network parameter is specified, others must be specified too (Ip / Mask / Gateway)"  
				exit 1
			else
				infoTab[$indiceTab]=$tempVar
				indiceTab=`expr $indiceTab + 1`
			fi
		elif [[ $mode = "dhcp" ]]
		then
			infoTab[$indiceTab]=$tempVar
			indiceTab=`expr $indiceTab + 1`
		fi
	else
		echo "Error : Mode must static or dhcp"
		exit 1
	fi
else
	echo "Error : First parameter must be eth-something"
	exit 1
fi
}

doManip() {
if [[ "$searchingForETCflag" = "0" ]] && [[ -d /tmp/$$-dir/etc ]]
then
	searchingForETCflag=1
        #Redhat#
	if [ -f /tmp/$$-dir/etc/sysconfig/network ]
        then
		echo "Red Hat Style distribution spotted ! Editing Configuration"

                #Edit Hostname
                if [ -n $newHostname  ]
                then
                         OldHostname=`cat /tmp/$$-dir/etc/sysconfig/network | grep HOSTNAME | cut -d '=' -f 2`
                         sed -i 's/HOSTNAME='$OldHostname'/HOSTNAME='$newHostname'/g' /tmp/$$-dir/etc/sysconfig/network
		fi
				
		#a bit dirty... only a bit#
		#OldMac=`cat /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-eth0 | grep HWADDR | cut -d '=' -f 2`
		#sed -i 's/'$OldMac'/'$macAddressTab'/g' /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-eth0
		rm -rf /tmp/$$-dir/etc/rc.d/rc5.d/S05kudzu
			
				
                 eval t=\($infoTab\)
		for elt in "${t[@]}"
        	do
										
			interface=`echo $elt | cut -d '/' -f 1`
			mode=`echo $elt | cut -d '/' -f 2`
			ip=`echo $elt | cut -d '/' -f 3`
			netmask=`echo $elt | cut -d '/' -f 4`
			gateway=`echo $elt | cut -d '/' -f 5`

			bootproto=`cat /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-$interface | grep BOOTPROTO | cut -d "=" -f '2'`
													
					
			if [[ "$mode" = "dhcp" ]] && [[ "$bootproto" = "none" ]]
			then
				sed -i 's/BOOTPROTO=none/BOOTPROTO=dhcp/g' /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-$interface
				ligne=`cat /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-$interface | grep -n IPADDR | cut -d ':' -f 1`
				sed -i ''$ligne'd' /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-$interface
				ligne=`cat /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-$interface | grep -n NETMASK | cut -d ':' -f 1`
				sed -i ''$ligne'd' /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-$interface
				ligne=`cat /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-$interface | grep -n GATEWAY | cut -d ':' -f 1`
				sed -i ''$ligne'd' /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-$interface
			else
				if [[ "$bootproto" = "none" ]] &&  [[ "$mode" = "static" ]]
				then
					OldIP=`cat /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-$interface | grep IPADDR | cut -d "=" -f '2'`				
					sed -i 's/'$OldIP'/'$ip'/g' /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-$interface
					OldMask=`cat /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-$interface | grep NETMASK | cut -d "=" -f '2'`
					sed -i 's/'$OldMask'/'$netmask'/g' /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-$interface
					OldGateway=`cat /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-$interface | grep GATEWAY | cut -d "=" -f '2'`
					sed -i 's/'$OldGateway'/'$gateway'/g' /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-$interface
				elif [[ "$bootproto" = "dhcp" ]] &&  [[ "$mode" = "static" ]]
				then
					sed -i 's/BOOTPROTO=dhcp/BOOTPROTO=none/g' /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-$interface
					echo IPADDR=$ip >> /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-$interface
					echo NETMASK=$netmask >> /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-$interface
					echo GATEWAY=$gateway >> /tmp/$$-dir/etc/sysconfig/network-scripts/ifcfg-$interface
				fi
			fi
		done
		#Debian
	elif [ -f /tmp/$$-dir/etc/network/interfaces ]
	then
		echo "Debian Style distribution spotted ! Editing Configuration"
		rm -rf /tmp/$$-dir/etc/udev/rules.d/70-persistent-net.rules
                                
		#Edit Hostname
                if [ -n $newHostname  ]
		then
			rm -rf /tmp/$$-dir/etc/hostname
			echo $newHostname >> /tmp/$$-dir/etc/hostname
		fi
				
                #Network Parameters
                eval t=\($infoTab\)
		for elt in "${t[@]}"
        	do			
			interface=`echo $elt | cut -d '/' -f 1`
			mode=`echo $elt | cut -d '/' -f 2`
			ip=`echo $elt | cut -d '/' -f 3`
			netmask=`echo $elt | cut -d '/' -f 4`
			gateway=`echo $elt | cut -d '/' -f 5`
					
		        bootproto=`cat /tmp/$$-dir/etc/network/interfaces | grep "iface $interface" | cut -d ' ' -f '4'`
					
			if [ "$mode" = "dhcp" ]
			then		
				if [ "$bootproto" = "static" ]
				then
					ligne=`cat /tmp/$$-dir/etc/network/interfaces | grep -n "iface $interface inet" | cut -d ':' -f 1`
					sed -i 's/iface '$interface' inet static/iface '$interface' inet dhcp/' /tmp/$$-dir/etc/network/interfaces
					ligne=`expr $ligne + 1`
					sed -i ''$ligne'd' /tmp/$$-dir/etc/network/interfaces
					sed -i ''$ligne'd' /tmp/$$-dir/etc/network/interfaces
					sed -i ''$ligne'd' /tmp/$$-dir/etc/network/interfaces
				fi
	           	elif [ "$mode" = "static" ]
			then
				if [ "$bootproto" = "dhcp" ]
                 	        then
			 		sed -i 's/iface '$interface' inet dhcp/iface '$interface' inet static/g' /tmp/$$-dir/etc/network/interfaces
					ligne=`cat /tmp/$$-dir/etc/network/interfaces | grep -n "iface $interface inet" /tmp/$$-dir/etc/network/interfaces | cut -d: -f1`
					sed -i ''$ligne'a\gateway '$gateway'' /tmp/$$-dir/etc/network/interfaces
					sed -i ''$ligne'a\netmask '$netmask'' /tmp/$$-dir/etc/network/interfaces	
					sed -i ''$ligne'a\address '$ip'' /tmp/$$-dir/etc/network/interfaces
						
				elif [ "$bootproto" = "static" ]
				then
					ligne=`cat /tmp/$$-dir/etc/network/interfaces | grep -n "iface $interface" | cut -d ":" -f 1`
					ligne=`expr $ligne + 1`
					OldIP=`cat /tmp/$$-dir/etc/network/interfaces | awk 'NR == '$ligne'' | cut -d " " -f 2`
					ligne=`expr $ligne + 1`
					OldNetMask=`cat /tmp/$$-dir/etc/network/interfaces | awk 'NR == '$ligne'' | cut -d " " -f 2` 
					ligne=`expr $ligne + 1`
					OldGateway=`cat /tmp/$$-dir/etc/network/interfaces | awk 'NR == '$ligne'' | cut -d " " -f 2`
							
					sed -i 's/'$OldIP'/'$ip'/' /tmp/$$-dir/etc/network/interfaces
					sed -i 's/'$OldNetMask'/'$netmask'/' /tmp/$$-dir/etc/network/interfaces
					sed -i 's/'$OldGateway'/'$gateway'/' /tmp/$$-dir/etc/network/interfaces
				fi
			fi
		done
        else
                echo "Distrib Style not recognized, i prefer to don't touch anything"
	fi
fi
}

disk=
infoTab=
indiceTab=0
name=
uuid=
newMac=
newIP=
newMask=
newGateway=
macAdress=
flagForce=0
flagNetwork=0
autostart=0
searchingForETCflag=

while [[ $1 = -* ]]; do
  case "$1" in
     --disk)
      disk="$2"
      shift 2
      ;;
     --hostname)
      newHostname="$2"
      flagNetwork=1
      shift 2
      ;;
     -h|--help)
      usage
      exit
      ;;
      --net)
      tempVar="$2"
      stockVar
      flagNetwork=1
      shift 2
      ;;
      *)
      echo "Error: Unknown option: $1" >&2
      exit 1
      ;;
  esac
done

if [[ -z $disk ]] 
then
     usage
     exit 1
fi


# Testing path to determine if VM is "Debian Style" or "Red Hat Style" #
if [ $flagNetwork = "1" ]
then
	mkdir /tmp/$$-dir> /dev/null 2>&1
	searchingForETCflag=0

	loopPart=`kpartx -l $disk | awk {'print $1'}`
	if [[ -z $loopPart ]]
	then
		mount -o loop $disk /tmp/$$-dir
		doManip
		umount /tmp/$$-dir
	else
		kpartx -a $disk
		eval t=\($loopPart\)
		for elt in "${t[@]}"
		do
			mount /dev/mapper/$elt /tmp/$$-dir> /dev/null 2>&1
			doManip
			umount /tmp/$$-dir> /dev/null 2>&1
		done
		kpartx -d $disk > /dev/null 2>&1
	fi
fi

##End : Cleaning
rm -f /tmp/$$-vol.xml /tmp/$$-vm.xml
rmdir /tmp/$$-dir> /dev/null 2>&1
echo "Job's done"

