#!/bin/sh
#
# VMTux.net
#
# /etc/init.d/network
#
# Busybox udhcpc dispatcher script. Copyright (C) 2009 by Axel Beckert.
# https://udhcp.busybox.net/README.udhcpc
#
# Based on the busybox example scripts and the old udhcp source
PATH=/bin:/sbin

umask 022

if [ "$(id -u)" -ne 0 ]; then echo "This command can only be used by root" >&2 ; exit 1; fi

RESOLV_CONF=/etc/resolv.conf

case $1 in
    bound|renew|restart|start)
	[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
	[ -n "$subnet" ] && NETMASK="netmask $subnet"

	/sbin/ifconfig $interface $ip $BROADCAST $NETMASK
	
	echo "VMTux.net Linux $ip" > /etc/issue
	echo >> /etc/issue
	/bin/killall getty 2>/dev/null

	if [ -n "$router" ]; then
		/bin/logger -s -t network -p 6 "$0: route del default gw 0.0.0.0 dev $interface"
		while /sbin/route del default gw 0.0.0.0 dev $interface 2>/dev/null; do :; done

		metric=0
		for i in $router; do
			/bin/logger -s -t network -p 6 "$0: route add default gw $i dev $interface metric $metric"
			/sbin/route add default gw $i dev $interface metric $((metric++))
		done
	fi

	RESOLV_CONF="/etc/resolv.conf"
	echo -n > $RESOLV_CONF
	[ -n "$domain" ] && echo "domain $domain" >> $RESOLV_CONF
	for i in $dns ; do
		/bin/logger -s -t network -p 6 "adding dns $i"
		echo "nameserver $i" >> $RESOLV_CONF
	done
	;;

    deconfig)
	/sbin/ifconfig $interface 0.0.0.0
	;;

    leasefail)
        /bin/logger -s -t network -p 6 "$0: Lease failed: $message"
	;;

    nak)
        /bin/logger -s -t network -p 6 "$0: Received a NAK: $message"
	;;

    *)
        /bin/logger -s -t network -p 6 "$0: Unknown command: $1"
	exit 1;
	;;
esac

exit 0
