#/bin/sh -
#
# Copyright (c) 2004, 2005 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#

#
# HACK: run dhclient on all interfaces, and do nothing else
# (well, now also bring up a widearea node control net statically if asked)
#
# Note that this file's name (eth99) cannot overlap with an actual
# existing interface or dhclient will loop.  dhclient-script invokes
# the source-config function which sources the ifcfg-ethN file.  Thus
# if this script were called "ifcfg-eth0" and eth0 was the DHCPable
# interface, we would wind up recursively invoking dhclient (trust me,
# I tried it :-)
#

. /etc/emulab/paths.sh

DEVICE="eth99"
IFACETYPES="3c59x e100 e1000 tulip e1000e tg3"

#
# First, probe for all our supported ethernet types
#
modprobe -qa $IFACETYPES

interfaces=`ifconfig -a | sed -n 's/^\([^ ]*\).*encap:Ethernet.*$/\1/p'`

#
# Check: are we a remote node?  If so, possibly try static config in pref
# to dhcp.
#
if [ -e /etc/emulab/isrem -a -e /etc/emulab/waconfig ]; then
    . /etc/emulab/waconfig
    echo "Found Emulab widearea config info..."

    if [ "$WA_BOOTMETHOD" = "static" -a "$WA_MAC" != "" ]; then 
	# try to find an iface matching WA_MAC
	cnetif=""
	for _if in $interfaces; do
	    ifconfig $_if | grep HWaddr | sed -e 's/.*HWaddr\s*\(.*\)/\1/' \
		| grep -q -i "$WA_MAC"
	    if [ "$?" = "0" ]; then
		cnetif="$_if"
		break
	    fi
	done

	if [ "$cnetif" = "" ]; then
	    echo "Could not find iface with MAC $WA_MAC; trying DHCP!"
	    WA_BOOTMETHOD="dhcp"
	elif [ "$WA_HOSTNAME" = "" \
	       -o "$WA_DOMAIN" = "" \
	       -o "$WA_IP_ADDR" = "" \
	       -o "$WA_IP_NETMASK" = "" \
	       -o "$WA_IP_GATEWAY" = "" \
	       -o "$WA_IP_DNS1" = ""  ]; then
	    echo "Missing static IP config vars; trying DHCP!"
	    WA_BOOTMETHOD="dhcp"
	fi
    fi
fi

#
# Try static config if all its constraints above are met...
#
if [ "$WA_BOOTMETHOD" = "static" ]; then
    echo "Statically configuring control net on $cnetif ..."
    ifconfig "$cnetif" inet "$WA_IP_ADDR" netmask "$WA_IP_NETMASK" up
    route add default gateway "$WA_IP_GATEWAY"

    # bring up lo too just to make sure it's up
    ifconfig lo inet 127.0.0.1 up
    route add -net 127.0.0.0/8 dev lo

    # setup resolv.conf
    echo "search $WA_DOMAIN" > /etc/resolv.conf
    echo "nameserver $WA_IP_DNS1" >> /etc/resolv.conf
    if [ x"$WA_IP_DNS2" != x ]; then
	echo "nameserver $WA_IP_DNS2" >> /etc/resolv.conf
    fi

    # set hostname
    hosts_str="$WA_HOSTNAME"
    echo "$WA_HOSTNAME" | grep -q \\.
    if [ $? = 0 ]; then
	hostname "$WA_HOSTNAME"
    else
	hostname "${WA_HOSTNAME}.${WA_DOMAIN}"
	hosts_str="${WA_HOSTNAME}.${WA_DOMAIN} ${hosts_str}"
    fi

    # setup hosts file
    echo "$WA_IP_ADDR ${hosts_str}" >> /etc/hosts

    # setup a few necessary emulab files...
    echo "$cnetif" > $BOOTDIR/controlif
    if [ -e "/etc/emulab/bossnode" ]; then
        bossnode=`cat /etc/emulab/bossnode`
	bossip=`host -t A "$bossnode"`
	retval="$?"
	i=0
	while [ "$retval" != "0" -a $i -lt 6 ]; do
	    bossip=`host -t A "$bossnode"`
	    retval="$?"
	    i=`expr $i + 1`
	    sleep 5
	done
	echo `echo "$bossip" | sed -n -e 's/.*has address\s*\(.*\)/\1/p'` \
	    > $BOOTDIR/bossip
    fi
    echo "$WA_HOSTNAME" > $BOOTDIR/realname
    echo "$WA_IP_GATEWAY" > $BOOTDIR/routerip
    echo "$WA_IP_ADDR" > $BOOTDIR/myip
    echo "$WA_IP_NETMASK" > $BOOTDIR/mynetmask

    exit 0
fi

#
# else default to dhclient...
#

#
# If dhclient returns success, then it has configured the first interface
# and gone into background mode.  At that point we don't care about it any
# more and just kill it.  We also shutdown all the other interfaces (which
# dhclient will leave "up") and set ONBOOT=no to prevent ifup (our caller)
# from doing any further configuration on this fake interface.
#
echo "Discovering control net via dhcp ..."
if [ -x /sbin/dhclient ] && /sbin/dhclient -q $interfaces; then
    killall dhclient

    if [ -e $BOOTDIR/controlif ]; then
	interface=`cat $BOOTDIR/controlif`
	for _if in `ifconfig -s | awk '{ print $1 }' | grep -v Iface`
	do
	    if [ $_if != "lo" -a x$_if != x$interface ]
	    then
		echo "taking $_if down"
	        ifconfig $_if down
	    fi >>$LOGDIR/dhclient.log 2>&1
	done

        #
        # Now probe for wifi interfaces.  We don't want to
        # do this before dhcp'ing on the wired interfaces since
        # there is a possibility that the wifi interface will
        # grab an address and become the control net!
        #
        # This expects to load the madwifi-ng driver; the param states
        # that no "virtual AP" device is to be created on module load.
        # This is deferred until Emulab interface configuration time.
        #
        modprobe -q ath_pci autocreate=none
    fi
    ONBOOT="no"
else
    # eat flaming death
    exit 1
fi
