#!/bin/sh
## /etc/dhcpd.conf
## /etc/dhcp/dhcpd.conf,  /var/lib/dhcpd/dhcpd.leases, /var/run/dhcpd.pid,
##       /var/lib/dhcpd/dhcpd.leases~.
##/etc/xinetd.d/tftp
##/etc/selinux/config
##/etc/sysconfig/network-scripts/ifcfg-eth0
##/etc/inittab   #起動　3　　　5

## eth0=x.x.x.1
## set_static_ip.sh 253 r eth0    ....
##DNS----------------------------
## /etc/nsswitch.conf  /etc/resolv.conf
## /etc/named.conf   /var/named 

## IFS=' '
IP_NUM_INPUT="$1"
if [[ ${IP_NUM} = "" ]]
then
    IP_NUM=154
fi
GWS="$2 $3 $4 $5 $6 $7"

IP_PROTO_TYPE_FILE="./static_ip_protp_type.prm"
if [ -a $dhcpd_conf ]
then
    :
else
    echo "${IP_PROTO_TYPE_FILE} not exist"
fi
#####################################################
###/etc/sysconfig/network-scripts/ifcfg-eth0
#####################################################
IFS=
ls  /etc/sysconfig/network-scripts/ifcfg*[0-9] | \
while read interface_file
do
    if [[ "$interface_file" != *-lo ]]
    then
        echo "${interface_file} modfied"
        rm -f ${interface_file}_tmp
        IFS='='
        cat ${IP_PROTO_TYPE_FILE} | \
        while read field1 field2
        do
            ##echo "$field1-----$interface_file"
            tmp=`grep "$field1" $interface_file`
            if [[ "${tmp}" = "" ]]
            then
                if [[ "$field1" = IPADDR ]]
                then
                    NET_PREFIX=`ifconfig ${ETHX} | grep Bcast | sed 's/^.*Bcast:\([0-9]*.[0-9]*.[0-9]*\).255.*$/\1/g'`
                    echo "IPADDR=${NET_PREFIX}.${IP_NUM}" >> ${interface_file}_tmp
                elif [[ "$field1" = NAME ]]
                then
                    echo "NAME=\"${ETHX}\"" >> ${interface_file}_tmp
                elif [[ "$field1" = GATEWAY ]]
                then
                    echo "GATEWAY=${NET_PREFIX}.1" >> ${interface_file}_tmp
                elif [[ "$field1" = DNS1 ]]
                then
                    echo "DNS1=${NET_PREFIX}.${IP_NUM}" >> ${interface_file}_tmp
                elif [[ "$field1" = UUID ]] || [[ "$field1" = UUID ]]
                then
                    :
                else
                    echo "${field1}=${field2}" >> ${interface_file}_tmp
                fi
            else
                if [[ "$field1" = DEVICE ]]
                then
                       ETHX=$(echo "${tmp}" | sed 's/DEVICE=\(.*\)/\1/g' | sed 's/\"\(.*\)\"/\1/g')
                       echo "${GWS}" |grep -q "${ETHX}"
                       if [ $? -eq 0 ]
                       then
                               IP_NUM="${IP_NUM_INPUT}"
                       else
                               IP_NUM="1"
                       fi
                    echo "${field1}=${ETHX}" >> ${interface_file}_tmp
                    ##ifconfig ${ETHX} | grep ${ETHX} 
                elif [[ "$field1" = IPADDR ]]
                then
                    if [[ "${ETHX}" = eth[0-9] ]]
                    then
                        NET_PREFIX=`ifconfig ${ETHX} | grep Bcast | sed 's/^.*Bcast:\([0-9]\+.[0-9]\+.[0-9]\+\).255.*$/\1/g'`
                        echo "IPADDR=${NET_PREFIX}.${IP_NUM}" >> ${interface_file}_tmp
                    else
                        echo "${tmp}" >> ${interface_file}_tmp
                    fi
                elif [[ "$field1" = DNS1 ]]
                then
                    if [[ "${NET_PREFIX}" = "" ]]
                    then
                        echo "${tmp}" >> ${interface_file}_tmp
                    else
                        echo "DNS1=${NET_PREFIX}.${IP_NUM}" >> ${interface_file}_tmp
                    fi
                elif [[ "$field1" = BOOTPROTO ]]
                then
                    echo "${field1}=${field2}" >> ${interface_file}_tmp
                else
                    echo "${tmp}" >> ${interface_file}_tmp
                fi
            fi
        done
    fi
    cp ${interface_file}_tmp ${interface_file}
    rm -f ${interface_file}_tmp
done

chmod 751 ./dhcp_tftp_setup.sh
if [[ "${2}" = r* ]]
then
	echo "./dhcp_tftp_setup.sh  $2"
	./dhcp_tftp_setup.sh  $2
else
	echo "./dhcp_tftp_setup.sh"
	./dhcp_tftp_setup.sh
fi
#####################################################
###
#####################################################
