#!/bin/sh
#
#  This script sets up the /etc/network/interface file for the new
# image.
#
# Steve
# --
# http://www.steve.org.uk/


prefix=$1

#
#  Source our common functions
#
if [ -e /usr/lib/xen-tools/common.sh ]; then
    . /usr/lib/xen-tools/common.sh
else
    . ./hooks/common.sh
fi


#
# Log our start
#
logMessage Script $0 starting


#
#  Make sure we have an /etc/network directory.
#
mkdir -p ${prefix}/etc/network


#
#  A function to setup DHCP for our new image.
#
setupDynamicNetworking ()
{
    #
    # The host is using DHCP.
    #
    cat <<E_O_DHCP > ${prefix}/etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet dhcp
# post-up ethtool -K eth0 tx off

#
# The commented out line above will disable TCP checksumming which
# might resolve problems for some users.  It is disabled by default
#
E_O_DHCP
}



#
#  A function to setup static IP addresses for our new image.
#
setupStaticNetworking ()
{
    #
    #  if $p2p is set then add a "pointopoint" setting.
    #
    point='';
    if [ ! -z "${p2p}" ]; then
        point="pointopoint ${p2p}"
    else
        point=''
    fi

    #
    # broadcast address?
    #
    bcast='';
    if [ ! -z "${broadcast}" ]; then
      bcast=" broadcast ${broadcast}"
    fi

    #
    # We have a static IP address
    #
    cat <<E_O_STATIC >${prefix}/etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
 address ${ip1}
 gateway ${gateway}
 netmask ${netmask}
 ${bcast}
 ${point}
 # post-up  ethtool -K eth0 tx off

#
# The commented out line above will disable TCP checksumming which
# might resolve problems for some users.  It is disabled by default
#
E_O_STATIC

    interface=1
    count=2

    while [ "${count}" -le "${ip_count}" ]; do

        value=\$ip${count}
        value=`eval echo $value`

        logMessage Adding etho:${interface}

        cat <<E_O_STATIC >>${prefix}/etc/network/interfaces
auto eth0:${interface}
iface eth0:${interface} inet static
 address ${value}
 netmask ${netmask}
 # post-up  ethtool -K eth0 tx off
E_O_STATIC
        count=`expr $count + 1`
        interface=`expr $interface + 1`
    done

    #
    # Hooks are run chrooted, hence the resolv.conf is moved
    # temporarily to /etc/resolv.conf.old. Use that file, it
    # will be restored after hooks are run.
    #
    if [ '' != "$nameserver" ]; then
        rm -f ${prefix}/etc/resolv.conf.old
        for ns in $nameserver; do
            echo "nameserver $ns" >>${prefix}/etc/resolv.conf.old
        done
    else
        cp /etc/resolv.conf ${prefix}/etc/resolv.conf.old
    fi
}




#
#  Call the relevant function
#
if [ -z "${dhcp}" ]; then
    logMessage "Setting up static networking"
    setupStaticNetworking

else
    logMessage "Setting up DHCP networking"
    setupDynamicNetworking
fi


#
#  Log our finish
#
logMessage Script $0 finished
