#!/bin/sh
#####################################################################
#
# setdhcp
#
#	This shell script make ethx interface as DHCP client.
#
# Version:	0.1 2004/05/13
# Author:	h2y
#
# Usage:	setdhcp [eth0|eth1]
#
#####################################################################

# Check argument
if [ $# -ne 1 ]; then
	echo "Usage: setdhcp [eth0|eth1]"
	exit 1
fi

case "$1" in
	eth0|eth1)
		# make ifcfg file name
		IFCFG_FILE=/etc/sysconfig/network-scripts/ifcfg-$1
		;;
	*)
		echo "$1 interface is not supported"
		exit 1
		;;
esac

if [ ! -f "$IFCFG_FILE" ]; then
	echo "$1 interface is not existed"
	exit 1
fi

# read ifcfg file
. $IFCFG_FILE

# stop network
/etc/init.d/network stop

# chage boot protocl as dhcp
BOOTPROTO="dhcp"

# save ifcfg
echo "DEVICE=\"${DEVICE}\"" > $IFCFG_FILE
echo "IPADDR=\"${IPADDR}\"" >> $IFCFG_FILE
echo "NETMASK=\"${NETMASK}\"" >> $IFCFG_FILE
echo "NETWORK=\"${NETWORK}\"" >> $IFCFG_FILE
echo "BROADCAST=\"${BROADCAST}\"" >> $IFCFG_FILE
echo "ONBOOT=\"${ONBOOT}\"" >> $IFCFG_FILE
echo "BOOTPROTO=\"${BOOTPROTO}\"" >> $IFCFG_FILE

# if there is eth0-pppoe, disable it
IFCFG_ETH0PPPOE=/etc/sysconfig/network-scripts/ifcfg-eth0pppoe
if [ -f $IFCFG_ETH0PPPOE ]; then
	TMP_FILE=/tmp/xxx

	sed -e 's/^ONBOOT=\"[a-z]*\"$/ONBOOT=\"no\"/g' $IFCFG_ETH0PPPOE > $TMP_FILE
	mv $TMP_FILE $IFCFG_ETH0PPPOE
fi

# start network
/etc/init.d/network start

