#!/bin/bash
FICHIER_IP=[ethernet1]\nState=1\nName=Internet\nProtected=1\nMedia=0\nType=2\nGateway=\nAddress=125.246.235.245\nMask=255.0.0.0\nAddressComment=\n\n[ethernet2]\nState=2\nName=Intra\nProtected=1\nMedia=0\nType=2\nColor=1564521\n\n[ethernet3]\nState=1\nName=NET_IN\nProtected=1\nMedia=0\nType=1\nColor=FF0000\nGateway=124.128.200.100\nAddress=125.246.230.240\nMask=255.255.255.0\nAddressComment=\n\nIP_DYN=false\nLABEL_DHCP="DHCP"\nNOM_RESEAU=NET_IN\nLABEL_NOM=Name\=\n\nLABEL_ADRESSE=Address\nLABEL_MASQUE=Mask\nLABEL_PASSERELLE=Gateway\nLABEL_BLOC=ethernet\nLIGNE_NOM_RESEAU=`grep -i -n ^$LABEL_NOM$NOM_RESEAU`\nLIGNE_ENTETES=`grep -i -n $LABEL_BLOC $FICHIER_IP`;
ADRESSE_IP=1.2.3.4
MASQUE_IP=255.255.0.0
PASSERELLE_IP=9.10.11.12

IP_DYN=false
LABEL_DHCP="DHCP"

if [ -n "$ADRESSE_IP" ] 
then    
    VAR_TEMP=`echo $ADRESSE_IP | grep -i '$LABEL_DHCP'`;
	if [ -n "$VAR_TEMP" ]
	then
	    IP_DYN=true	  
	    MASQUE_IP=""
	    PASSERELLE_IP=""
	else
	    VAR_TEMP=`echo  $ADRESSE_IP | grep '^\(25[0-5]\|2[0-4][0-9]\|[01]\?[0-9][0-9]\?\).\(25[0-5]\|2[0-4][0-9]\|[01]\?[0-9][0-9]\?\).\(25[0-5]\|2[0-4][0-9]\|[01]\?[0-9][0-9]\?\).\(25[0-5]\|2[0-4][0-9]\|[01]\?[0-9][0-9]\?\)$'`;
            if [ -z "$VAR_TEMP" ]
	    then
		echo "Le format de l'adresse IP est incorrect.";exit 1;		
	    fi
	fi
fi
if [ "$IP_DYN" == false ]
then
    if [ -n "$MASQUE_IP" ]
    then
	VAR_TEMP=`echo  $MASQUE_IP | grep '^\(255\|254\|252\|248\|240\|224\|192\|128\|0[0]\?[0]\?\).\(255\|254\|252\|248\|240\|224\|192\|128\|0[0]\?[0]\?\).\(255\|254\|252\|248\|240\|224\|192\|128\|0[0]\?[0]\?\).\(255\|254\|252\|248\|240\|224\|192\|128\|0[0]\?[0]\?$\)'`;
        if [ -z "$VAR_TEMP" ]
        then
	    echo "Le format du masque IP est incorrect.";exit 1;	    
        else
		MASQUE1=`echo $MASQUE_IP | cut -d '.' -f1`
		MASQUE2=`echo $MASQUE_IP | cut -d '.' -f2`
		MASQUE3=`echo $MASQUE_IP | cut -d '.' -f3`
		MASQUE4=`echo $MASQUE_IP | cut -d '.' -f4`
		if [ $MASQUE1 -lt $MASQUE2 ] || [ $MASQUE2 -lt $MASQUE3 ] || [ $MASQUE3 -lt $MASQUE4 ]
		then
		    echo "Le format du masque IP est incorrect.";exit 1;
		fi
	fi
    fi   
    if [ -n "$PASSERELLE_IP" ]
    then
	VAR_TEMP=`echo  $PASSERELLE_IP | grep '^\(25[0-5]\|2[0-4][0-9]\|[01]\?[0-9][0-9]\?\).\(25[0-5]\|2[0-4][0-9]\|[01]\?[0-9][0-9]\?\).\(25[0-5]\|2[0-4][0-9]\|[01]\?[0-9][0-9]\?\).\(25[0-5]\|2[0-4][0-9]\|[01]\?[0-9][0-9]\?\)$'`;
        if [ -z "$VAR_TEMP" ]
        then
	    echo "Le format de la passerelle est incorrect.";exit 1;
	fi   
    fi

    if [ -z "$MASQUE_IP" ] || [ -z "$ADRESSE_IP" ] || [ -z "$PASSERELLE_IP" ]
    then
	echo "Il faut modifier a la fois l'adresse IP , le masque et la passerelle";exit 1;
    fi
fi
echo fin