#!/bin/sh
# http://lists.debian.org/debian-boot/2002/11/msg00500.html
# udhcpc script edited by Tim Riker <Tim@Rikers.org>
#
# Copyright (C) 2011  mariodebian at gmail
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#


[ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1

RESOLV_CONF="/etc/resolv.conf"
[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
[ -n "$subnet" ] && NETMASK="netmask $subnet"

case "$1" in
	deconfig)
		/sbin/ifconfig $interface 0.0.0.0
		;;

	renew|bound)
		( echo "# created by /bin/udhcpc-script"
			[ -n "$boot_file" ] && echo "boot_file=$boot_file"
			[ -n "$bootfile" ] && echo "bootfile=$bootfile"
			[ -n "$bootsize" ] && echo "bootsize=$bootsize"
			[ -n "$broadcast" ] && echo "broadcast=$broadcast"
			[ -n "$clientid" ] && echo "clientid=$clientid"
			[ -n "$cookiesrv" ] && echo "cookiesrv=$cookiesrv"
			[ -n "$dhcptype" ] && echo "dhcptype=$dhcptype"
			[ -n "$dns" ] && echo "dns=$dns"
			[ -n "$domain" ] && echo "domain=$domain"
			[ -n "$hostname" ] && echo "hostname=$hostname"
			[ -n "$interface" ] && echo "interface=$interface"
			[ -n "$ip" ] && echo "ip=$ip"
			[ -n "$ipttl" ] && echo "ipttl=$ipttl"
			[ -n "$lease" ] && echo "lease=$lease"
			[ -n "$logsrv" ] && echo "logsrv=$logsrv"
			[ -n "$lprsrv" ] && echo "lprsrv=$lprsrv"
			[ -n "$message" ] && echo "message=$message"
			[ -n "$mtu" ] && echo "mtu=$mtu"
			[ -n "$namesrv" ] && echo "namesrv=$namesrv"
			[ -n "$nisdomain" ] && echo "nisdomain=$nisdomain"
			[ -n "$nissrv" ] && echo "nissrv=$nissrv"
			[ -n "$ntpsrv" ] && echo "ntpsrv=$ntpsrv"
			[ -n "$requestip" ] && echo "requestip=$requestip"
			[ -n "$rootpath" ] && echo "rootpath=$rootpath"
			[ -n "$router" ] && echo "router=$router"
			[ -n "$search" ] && echo "search=$search"
			[ -n "$serverid" ] && echo "serverid=$serverid"
			[ -n "$siaddr" ] && echo "siaddr=$siaddr"
			[ -n "$sname" ] && echo "sname=$sname"
			[ -n "$staticroutes" ] && echo "staticroutes=$staticroutes"
			[ -n "$subnet" ] && echo "subnet=$subnet"
			[ -n "$swapsrv" ] && echo "swapsrv=$swapsrv"
			[ -n "$tftp" ] && echo "tftp=$tftp"
			[ -n "$timesrv" ] && echo "timesrv=$timesrv"
			[ -n "$timezone" ] && echo "timezone=$timezone"
			[ -n "$userclass" ] && echo "userclass=$userclass"
			[ -n "$vendorclass" ] && echo "vendorclass=$vendorclass"
			[ -n "$wins" ] && echo "wins=$wins"
			[ -n "$wpad" ] && echo "wpad=$wpad"
			[ -n "$sipsrv" ] && echo "sipsrv=$sipsrv"
		) > /tmp/net.data
		
		ifconfig $interface $ip $BROADCAST $NETMASK

		if [ -n "$router" ] ; then
			while route del default gw 0.0.0.0 dev $interface ; do
				:
			done

			for i in $router ; do
				route add default gw $i dev $interface
			done
		fi

		[ -n "$domain" ] && echo search $domain > $RESOLV_CONF
		for i in $dns ; do
			echo nameserver $i >> $RESOLV_CONF
		done
		;;
esac

exit 0
