#!/bin/bash
###
# Prepare networking from config files in data dir

set -e

source ${XENSOURCE_INVENTORY}

NETWORK_CONF=${FIRSTBOOT_DATA_DIR}/network.conf
UPGRADE="false"
[ -r ${FIRSTBOOT_DATA_DIR}/host.conf ] && . ${FIRSTBOOT_DATA_DIR}/host.conf

pif_attached() {
    local pif=$1
    attached=$($XE pif-list params=currently-attached uuid=$pif --minimal)
    test "$attached" == "true"
}

start() {
    [ "$UPGRADE" = true ] && return 0
    if [ -e ${NETWORK_CONF} ] ; then
        source ${NETWORK_CONF}

        # set up interface configuration:
        for iface in ${INTERFACES} ; do
            # check the config file exists, and source it:
            if [ ! -e ${FIRSTBOOT_DATA_DIR}/interface-${iface}.conf ]; then
                logger "Firstboot warning: config file interface-${iface}.conf was missing but specified in network.conf"
                continue
            fi
            source ${FIRSTBOOT_DATA_DIR}/interface-${iface}.conf

            # introduce the PIF (will fail in the case of an existing management
            # PIF but this is harmless). This completes the device renaming step.
            if PIF="$($XE pif-introduce mac=${iface} host-uuid=${INSTALLATION_UUID} device=${LABEL})" ; then
                logger "Successfully introduced PIF with mac ${iface} as ${PIF}"
            else
                logger "Warning: PIF introduce failed with mac ${iface}"
            fi

            # For each PIF, configure the IP address
            # XXX: to catch the case where a slave reconfigures its management interface
            # we skip errors at this point
            if PIF="$($XE pif-list host-uuid=${INSTALLATION_UUID} MAC=${iface} params=uuid --minimal)" ; then
                case $MODE in
                    dhcp|none)
                        $XE pif-reconfigure-ip uuid=${PIF} mode=${MODE} || true
                        ;;
                    static)
                        # loop over DNS1..DNSn making a comma-separated list:
                        unset dns
                        i=1
                        while :
                        do
                            d=`eval echo \\$DNS$i`
                            unset DNS$i
                            [ -n "$d" ] || break
                            [ -n "$dns" ] && dns="$dns,"
                            dns="$dns$d"
                            i=$((i+1))
                        done
                        domain=`echo $DOMAIN | tr ' ' ','`
                        $XE pif-reconfigure-ip uuid=${PIF} mode=static IP=${IP} netmask=${NETMASK} gateway=${GATEWAY} ${dns+DNS=$dns} || true
                        [ $domain ] && $XE pif-param-set uuid=${PIF} other-config:domain=$domain || true
                        ;;
                esac
            else
                logger "Warning: cannot configure IP settings for PIF with mac ${iface}"
            fi

            unset MODE IP NETMASK GATEWAY DOMAIN PIF LABEL 
        done

        # set management interface, if defined:
        if [ "x${ADMIN_INTERFACE}" != "x" ] ; then
            MGMT_PIF="$($XE pif-list params=uuid MAC="${ADMIN_INTERFACE}" --minimal)"
            # Unfortunately on a slave this command fails with End_of_file
            $XE host-management-reconfigure pif-uuid="${MGMT_PIF}"
        fi

        # we now attempt to plug all PIFs that are not already plugged:
        for pif in $($XE pif-list params=uuid --minimal | sed 's/,/ /g'); do
            if ! pif_attached pif; then
                $XE pif-plug uuid=$pif
            fi
        done
    fi
}

case $1 in
    start)  start ;;
esac

