#!/bin/sh

##
# Configure network interfaces and host name
##

. /etc/rc.common

StartService ()
{
    ConsoleMessage "Initializing network"

    # Configure interfaces 
    ipconfig waitall > /dev/null 2>&1
    ifconfig lo0 127.0.0.1 netmask 255.0.0.0 up

    # See if the network is up now
    CheckForNetwork

    ##
    # Configure global network settings
    ##

    if [ "${NETWORKUP:=-NO-}" = "-YES-" ]; then
    
    #Turn on/off IPv6
	if [ "${IPV6:=-YES-}" = "-NO-" ]; then
		#Disable ipv6
		sysctl -w net.inet6.ip6.auto_on=0 > /dev/null
		ip6 -x
	fi

	# Turn on/off IP forwarding
	if [ "${IPFORWARDING:=-NO-}" = "-YES-" ]; then
	    sysctl -w net.inet.ip.forwarding=1 > /dev/null
	else
	    sysctl -w net.inet.ip.forwarding=0 > /dev/null
	fi

	# Set the host id based on the first broadcast interface's IP address
	ConsoleMessage "Setting host identifier"

	# Convert the IP address from dotted decimal to a single unsigned 32-bit
	# decimal number.  For 17.202.40.191, this would be:
	#       17 * 16777216 + 202 * 65536 + 40 * 256 + 191
	#     = 298461375
	hostid=$(($(ifconfig -a | sed -n '/inet/p' | sed -n '/broadcast/p' | head -1 | \
	    sed 's|.*inet[ 	]*\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*|16777216 * \1 + 65536 * \2 + 256 * \3 + \4|')))

	if [ -z "${hostid}" ]; then
	    echo "Warning: couldn't determine the host id"
	else
	    sysctl -w kern.hostid=${hostid}
	fi

    fi

}

StopService ()
{
    return 0
}

RestartService ()
{
    return 0
}

RunService "$1"
