## Start post_install_network_config generated code
#if $getVar('promisc_nics', '') != ""
    #set promisc_interfaces = [promisc.strip() for promisc in $promisc_nics.split(',') if promisc.strip()]
#else
    #set promisc_interfaces = []
#end if
#if $hostname != ""
# set the hostname
echo "$hostname" > /etc/hostname
/bin/hostname $hostname
#end if

#set osversion = $getVar("os_version","")

$SNIPPET('preseed_hosts')

#set $num_ns_search = $len($name_servers_search)
#if $num_ns_search > 0
sed -i -e "/^search /d" /etc/resolv.conf
echo -n "search " >>/etc/resolv.conf
        #for $nameserversearch in $name_servers_search
echo -n "$nameserversearch " >>/etc/resolv.conf
        #end for
echo "" >>/etc/resolv.conf
#end if

#set $num_ns = $len($name_servers)
#if $num_ns > 0
sed -i -e "/^nameserver /d" /etc/resolv.conf
    #for $nameserver in $name_servers
echo "nameserver $nameserver" >>/etc/resolv.conf
    #end for
#end if

echo '' > /tmp/network_log

# get physical interfaces
physical_interfaces=""
for physical_interface in \$(ip -o link | cut -d: -f2 | grep -v lo); do
    echo "processing physical interface \${physical_interface}" >> /tmp/network_log
    if [ -z "\${physical_interfaces}" ]; then
        physical_interfaces="\${physical_interface}"
    else
        physical_interfaces="\${physical_interfaces} \${physical_interface}"
    fi
    eval "physical_interface_\${physical_interface}=\${physical_interface}"
done
echo "physical interfaces: \${physical_interfaces}" >> /tmp/network_log

# map physical interface to mac address
mac_names=""
for physical_interface in \${physical_interfaces}; do
    mac=\$(ip link show \${physical_interface} | awk 'match(\$0,/link\/ether/) {print \$2}')
    mac_name=\$(echo \${mac} |tr 'a-z' 'A-Z' | tr ':' '_')
    if [ -z "\${mac_name}" ]; then
        mac_names="\${mac_name}"
    else
        mac_names="\${mac_names} \${mac_name}"
    fi
    echo "mapping physical interface \${physical_interface} to mac \${mac}" >> /tmp/network_log
    eval "physical_interface_mac_\${physical_interface}=\$mac"
    eval "physical_mac_interface_\${mac_name}=\${physical_interface}"
    eval "mac_name_\${mac_name}=\$mac"
done

echo "list physical interfaces to mac" >> /tmp/network_log
for physical_interface in \${physical_interfaces}; do
    eval "mac=\\${physical_interface_mac_\${physical_interface}}"
    echo "physical interface to mac: \${physical_interface} => \${mac}" >> /tmp/network_log
done

echo "list mac to physical interface mapping" >> /tmp/network_log
for mac_name in \${mac_names}; do
    eval "mac=\\${mac_name_\${mac_name}}"
    eval "physical_interface=\\${physical_mac_interface_\${mac_name}}"
    echo "mac to physical interface: \${mac} => \${physical_interface}" >> /tmp/network_log
done

logical_interfaces=""
#set ikeys = $interfaces.keys()
#import re
#set $vlanpattern = $re.compile("([a-zA-Z0-9]+)[\.][0-9]+")
#set $subinterfacepattern = $re.compile("([a-zA-Z0-9]+)[:][0-9]+")
#set $numbondingdevs = 0
#for $iname in $ikeys
    #set $idata = $interfaces[$iname]
    #set $mac = $idata.get("mac_address", "").upper()
    #set $interface_type = $idata.get("interface_type", "").lower()
    #if $interface_type in ("master","bond","bonded_bridge_slave")
        #set $numbondingdevs += 1
    #end if
    #if $interface_type in ("master","bond","bridge","bonded_bridge_slave")
        #continue
    #end if
    #if $vlanpattern.match($iname)
        #set $interface_matched = $vlanpattern.match($iname)
        #set $interface_name = $interface_matched.group(1)
logical_interface=${interface_name}
    #elif $subinterfacepattern.match($iname)
        #set $interface_matched = $subinterfacepattern.match($iname)
        #set $interface_name = $interface_matched.group(1)
logical_interface=${interface_name}
    #else
logical_interface=$iname
    #end if
if [ -z "\${logical_interfaces}" ]; then
    logical_interfaces="\${logical_interface}"
else
    logical_interfaces="\${logical_interfaces} \${logical_interface}"
fi
eval "logical_interface_\${logical_interface}=\${logical_interface}"
    #if $mac != ""
        #set mac_name = $mac.replace(':', '_')
physical_interface=\${physical_mac_interface_${mac_name}}
eval "logical_interface_mapping_\${logical_interface}=\${physical_interface}"
if [ ! -z "\${physical_interface}" ]; then
    eval "physical_interface_mapping_\${physical_interface}=\${logical_interface}"
fi
    #else
eval "physical_interface=\\${logical_interface_mapping_\${logical_interface}}"
if [ -z "\${physical_interface}" ]; then
    eval "logical_interface_mapping_\${logical_interface}="
fi
    #end if
#end for

echo "logical interfaces: \${logical_interfaces}" >> /tmp/network_log

echo "finish mapping logical interfaces in network config to physical interfaces" >> /tmp/network_log
for key in \${logical_interfaces}; do
    eval "physical_interface=\\${logical_interface_mapping_\${key}}"
    echo "map logical interface \${key} to physical interface \${physical_interface}" >> /tmp/network_log
done
for key in \${physical_interfaces}; do
    eval "logical_interface=\\${physical_interface_mapping_\${key}}"
    echo "map physical interface \${key} to logical interface \${logical_interface}" >> /tmp/network_log
done

# map unmapped logical interface to the same name physical interface
echo "mapping unmapped logical interfaces to the same name physical interfaces" >> /tmp/network_log
for logical_interface in \${logical_interfaces}; do
    eval "physical_interface=\\${logical_interface_mapping_\${logical_interface}}"
    if [ ! -z "\${physical_interface}" ]; then
        echo "ignore logical interface \${logical_interface} where its physical interface is \${physical_interface}" >> /tmp/network_log
        continue
    fi
    # check if the same name physical interface is mapped
    eval "mapped_logical_interface=\\${physical_interface_mapping_\${logical_interface}}"
    if [ ! -z "\${mapped_logical_interface}" ]; then
        echo "ignore logical interface \${logical_interface} since the same name physical interface is mapped by logical interface \${mapped_logical_interface}" >> /tmp/network_log
    continue
    fi
    # check if the same name physical interface exists
    eval "mapped_logical_interface=\\${physical_interface_\${logical_interface}}"
    if [ -z "\${mapped_logical_interface}" ]; then
        echo "ignore logical interface \${logical_interface} since the same name physical interface does not exist" >> /tmp/network_log
    continue
    fi
    eval "logical_interface_mapping_\${logical_interface}=\${logical_interface}"
    eval "physical_interface_mapping_\${logical_interface}=\${logical_interface}"
    echo "set logical interface \${logical_interface} to the same physical interface" >> /tmp/network_log
done

echo "finish mapping unmapped logical interfaces in network config to the same name physical interfaces" >> /tmp/network_log
for key in \${logical_interfaces}; do
    eval "physical_interface=\\${logical_interface_mapping_\${key}}"
    echo "map logical interface \${key} to physical interface \${physical_interface}" >> /tmp/network_log
done
for key in \${physical_interfaces}; do
    eval "logical_interface=\\${physical_interface_mapping_\${key}}"
    echo "map physical interface \${key} to logical interface \${logical_interface}" >> /tmp/network_log
done


unset_physical_interfaces=""
echo "get unset physical interfaces from \${physical_interfaces}" >> /tmp/network_log
for physical_interface in \${physical_interfaces}; do
    eval "logical_interface=\\${physical_interface_mapping_\${physical_interface}}"
    if [ ! -z "\${logical_interface}" ]; then
        echo "physical interface \${physical_interface} is already set to \${logical_interface}" >> /tmp/network_log
        continue
    fi
    if [ -z "\${unset_physical_interfaces}" ]; then
        unset_physical_interfaces="\${physical_interface}"
    else
        unset_physical_interfaces="\${unset_physical_interfaces} \${physical_interface}"
    fi
    eval "unset_physical_interface_\${physical_interface}=\${physical_interface}"
done
echo "unset physical interfaces \${unset_physical_interfaces}" >> /tmp/network_log

unset_logical_interfaces=""
echo "get unset logical interfaces from \${logical_interfaces}" >> /tmp/network_log
for logical_interface in \${logical_interfaces}; do
    eval "physical_interface=\\${logical_interface_mapping_\${logical_interface}}"
    if [ ! -z "\${physical_interface}" ]; then
        echo "logical interface \${logical_interface} is already set to \${physical_interface}" >> /tmp/network_log
        continue
    fi
    if [ -z "\${unset_logical_interfaces}" ]; then
        unset_logical_interfaces="\${logical_interface}"
    else
        unset_logical_interfaces="\${unset_logical_interfaces} \${logical_interface}"
    fi
done
echo "unset logical interfaces \${unset_logical_interfaces}" >> /tmp/network_log

sorted_unset_physical_interfaces=\$(printf '%s\n' \${unset_physical_interfaces} | sort | xargs)
echo "sorted unset physical interfaces \${sorted_unset_physical_interfaces}" >> /tmp/network_log
sorted_unset_logical_interfaces=\$(printf '%s\n' \${unset_logical_interfaces} | sort | xargs)
echo "sorted unset logical interfaces \${sorted_unset_logical_interfaces}" >> /tmp/network_log

# map unset logical interface to unset physical interface
echo "map unset logical interfaces \${sorted_unset_logical_interfaces} to unset physical interfaces \${sorted_unset_physical_interfaces}" >> /tmp/network_log
unset_logical_interfaces=""
for logical_interface in \${sorted_unset_logical_interfaces}; do
    set \${sorted_unset_physical_interfaces}
    physical_interface_num=\$#;
    if [ \${physical_interface_num} -gt 0 ]; then
        physical_interface=\$1
        shift 1
        sorted_unset_physical_interfaces="\$@"
        echo "map unset logical interface \${logical_interface} to unset physical interface \${physical_interface}" >> /tmp/network_log
        eval "physical_interface_mapping_\${physical_interface}=\${logical_interface}"
        eval "logical_interface_mapping_\${logical_interface}=\${physical_interface}"
    else
        echo "remain unset logical interface \${logical_interface} since there is no remain unset physical interfaces" >> /tmp/network_log
        if [ -z "\${unset_logical_interfaces}" ]; then
            unset_logical_interfaces="\${logical_interface}"
        else
            unset_logical_interfaces="\${unset_logical_interfaces} \${logical_interface}"
        fi
    fi
done
sorted_unset_logical_interfaces=\${unset_logical_interfaces}
unset_physical_interfaces=\${sorted_unset_physical_interfaces}

echo "finish mapping unmapped logical interfaces in network config to unmapped physical interfaces" >> /tmp/network_log
for key in \${logical_interfaces}; do
    eval "physical_interface=\\${logical_interface_mapping_\${key}}"
    echo "map logical interface \${key} to physical interface \${physical_interface}" >> /tmp/network_log
done
for key in \${physical_interfaces}; do
    eval "logical_interface=\\${physical_interface_mapping_\${key}}"
    echo "map physical interface \${key} to logical interface \${logical_interface}" >> /tmp/network_log
done

echo "unset physical interfaces \${sorted_unset_physical_interfaces}" >> /tmp/network_log
echo "unset logical interfaces \${sorted_unset_logical_interfaces}" >> /tmp/network_log

# map remaining unmapped logical interfaces
echo "map remaining unmapped logical interfaces" >> /tmp/network_log
if [ ! -z "\${sorted_unset_logical_interfaces}" ]; then
    # get all available logical interfaces which the same name physical interface is not used
    available_logical_interfaces=""
    for logical_interface in \${logical_interfaces}; do
        eval "mapped_logical_interface=\\${physical_interface_mapping_\${logical_interface}}"
        if [ -z "\${mapped_logical_interface}" ]; then
            eval "available_logical_interface_\${logical_interface}=\${logical_interface}"
            if [ -z "\${available_logical_interfaces}" ]; then
                available_logical_interfaces="\${logical_interface}"
            else
                available_logical_interfaces="\${available_logical_interfaces} \${logical_interface}"
            fi
        else
            echo "ignore logical interface \${logical_interface} since the same name physical interface mapped to logical interface \${mapped_logical_interface}" >> /tmp/network_log
        fi
    done

    # add extra logical interfaces name to physical interfaces
    if [ -z "\${physical_interfaces}" ]; then
        physical_interfaces="\${available_logical_interfaces}"
    else
        physical_interfaces="\${physical_interfaces} \${available_logical_interfaces}"
    fi
    echo "updated physical interfaces: \${physical_interfaces}" >> /tmp/network_log

    #first map logical interface to the same name physical interface if that physical interface name is not used
    unset_logical_interfaces=""
    for logical_interface in \${sorted_unset_logical_interfaces}; do
        eval "available_logical_interface=\\${available_logical_interface_\${logical_interface}}"
        if [ ! -z "\${available_logical_interface}" ]; then
            eval "physical_interface_mapping_\${available_logical_interface}=\${logical_interface}"
            eval "logical_interface_mapping_\${logical_interface}=\${available_logical_interface}"
        else
            if [ -z "\${unset_logical_interfaces}" ]; then
                unset_logical_interfaces="\${logical_interface}"
            else
                unset_logical_interfaces="\${unset_logical_interfaces} \${logical_interface}"
            fi
        fi
    done
    sorted_unset_logical_interfaces=\${unset_logical_interfaces}

    # map remain unset logical interfaces to available logical interface names
    for logical_interface in \${sorted_unset_logical_interfaces}; do
        for available_logical_interface in \${available_logical_interfaces}; do
            eval "mapped_logical_interface=\\${physical_interface_mapping_\${available_logical_interface}}"
            if [ -z "\${mapped_logical_interface}" ]; then
                eval "physical_interface_mapping_\${available_logical_interface}=\${logical_interface}"
                eval "logical_interface_mapping_\${logical_interface}=\${available_logical_interface}"
                break
            fi
        done
    done
fi
unset_logical_interfaces=""
sorted_unset_logical_interfaces=""

echo "finish mapping ramaining unmapped logical interfaces" >> /tmp/network_log
for key in \${logical_interfaces}; do
    eval "physical_interface=\\${logical_interface_mapping_\${key}}"
    echo "map logical interface \${key} to physical interface \${physical_interface}" >> /tmp/network_log
done
for key in \${physical_interfaces}; do
    eval "logical_interface=\\${physical_interface_mapping_\${key}}"
    echo "map physical interface \${key} to logical interface \${logical_interface}" >> /tmp/network_log
done

# map remaining unmapped physical interfaces
echo "map remaining unmapped physical interfaces" >> /tmp/network_log
if [ ! -z "\${sorted_unset_physical_interfaces}" ]; then
    # get all available physical interfaces which the same name logical interface is not used
    available_physical_interfaces=""
    for physical_interface in \${physical_interfaces}; do
        eval "mapped_physical_interface=\\${logical_interface_mapping_\${physical_interface}}"
        if [ -z "\${mapped_physical_interface}" ]; then
            eval "available_physical_interface_\${physical_interface}=\${physical_interface}"
            if [ -z "\${available_physical_interfaces}" ]; then
                available_physical_interfaces="\${physical_interface}"
            else
                available_physical_interfaces="\${available_physical_interfaces} \${physical_interface}"
            fi
        else
            echo "ignore physical interface \${physical_interface} since the same name logical interface mapped to physical interface \${mapped_physical_interface}" >> /tmp/network_log
        fi
    done

    # add extra physical interfaces name to logical interfaces
    if [ -z "\${logical_interfaces}" ]; then
        logical_interfaces="\${available_physical_interfaces}"
    else
        logical_interfaces="\${logical_interfaces} \${available_physical_interfaces}"
    fi
    echo "updated logical interfaces: \${logical_interfaces}" >> /tmp/network_log

    #first map physical interface to the same name logical interface if that logical interface name is not used
    unset_physical_interfaces=""
    for physical_interface in \${sorted_unset_physical_interfaces}; do
        eval "available_physical_interface=\\${available_physical_interface_\${physical_interface}}"
        if [ ! -z "\${available_physical_interface}" ]; then
            eval "logical_interface_mapping_\${available_physical_interface}=\${physical_interface}"
            eval "physical_interface_mapping_\${physical_interface}=\${available_physical_interface}"
        else
            if [ -z "\${unset_physical_interfaces}" ]; then
                unset_physical_interfaces="\${physical_interface}"
            else
                unset_physical_interfaces="\${unset_physical_interfaces} \${physical_interface}"
            fi
        fi
    done
    sorted_unset_physical_interfaces=\${unset_physical_interfaces}

    # map remain unset physical interfaces to logical interface name as available physical interface names
    for physical_interface in \${sorted_unset_physical_interfaces}; do
        for available_physical_interface in \${available_physical_interfaces}; do
            eval "mapped_physical_interface=\\${logical_interface_mapping_\${available_physical_interface}}"
            if [ -z "\${mapped_physical_interface}" ]; then
                eval "logical_interface_mapping_\${available_physical_interface}=\${physical_interface}"
            eval "physical_interface_mapping_\${physical_interface}=\${available_physical_interface}"
                break
            fi
        done
    done
fi
unset_physical_interfaces=""
sorted_unset_physical_interfaces=""

echo "finish mapping ramaining unmapped physical interfaces" >> /tmp/network_log
for key in \${logical_interfaces}; do
    eval "physical_interface=\\${logical_interface_mapping_\${key}}"
    echo "map logical interface \${key} to physical interface \${physical_interface}" >> /tmp/network_log
done
for key in \${physical_interfaces}; do
    eval "logical_interface=\\${physical_interface_mapping_\${key}}"
    echo "map physical interface \${key} to logical interface \${logical_interface}" >> /tmp/network_log
done

for key in \${logical_interfaces}; do
    eval "physical_interface=\\${logical_interface_mapping_\${key}}"
    if [ ! -z "\${physical_interface}" ]; then
        eval "physical_mac=\\${physical_interface_mac_\${physical_interface}}"
    else
        physical_mac=""
    fi
    if [ ! -z "\${physical_mac}" ]; then
        physical_mac=\$(echo \${physical_mac} | tr 'A-Z' 'a-z')
        echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"\${physical_mac}\", ATTR{type}==\"1\", KERNEL==\"eth*\", NAME=\"\$key\"" >> /etc/udev/rules.d/70-persistent-net.rules.new
        echo "add network interface \$key mac \${physical_mac} into udev rules" >> /tmp/network_log
    else
        echo "network interface \$key does not find mac address to add to udev rules" >> /tmp/network_log
    fi
done

#if $numbondingdevs > 0
# we have bonded interfaces, so set max_bonds
if [ -f "/etc/modprobe.conf" ]; then
    echo "options bonding max_bonds=$numbondingdevs" >> /etc/modprobe.conf
fi
#end if

## =============================================================================
## Rewrite the interfaces file and make sure we preserve the loopback device
echo "auto lo" > /etc/network/interfaces
echo "   iface lo inet loopback" >> /etc/network/interfaces
echo "" >> /etc/network/interfaces

#set $nameserver_set = 0

#for $iname in $ikeys
    #set $idata = $interfaces[$iname]
    #set $mac = $idata.get("mac_address", "").upper()
    #set $iface_type = $idata.get("interface_type", "").lower()
    #set $mtu                  = $idata.get("mtu", "")
    #set $static               = $idata.get("static", "")
    #set $management           = $idata.get("management", "")
    #set $ip                   = $idata.get("ip_address", "")
    #set $netmask              = $idata.get("netmask", "")
    #set $if_gateway           = $idata.get("if_gateway", "")
    #set $static_routes        = $idata.get("static_routes", "")
    #set $iface_master         = $idata.get("interface_master", "")
    #set $bonding_opts         = $idata.get("bonding_opts", "")
    #set $bridge_opts          = $idata.get("bridge_opts", "")

used_logical_interface_$iname=$iname

    #if $iface_type in ("slave","bridge_slave","bonded_bridge_slave")
        #set $static = 1
    #end if
    #if $ip == ""
        #set $static = ""
    #end if
echo "auto $iname" >> /etc/network/interfaces

    #if $static
echo "iface $iname inet static" >> /etc/network/interfaces
    #else
echo "iface $iname inet manual" >> /etc/network/interfaces
    #end if

    #if $iface_type not in ("master","bond","bridge","bonded_bridge_slave")
        #if $vlanpattern.match($iname)
            #pass
        #else
            #set $interface_matched = $subinterfacepattern.match($iname)
            #if $interface_matched
                #set $interface_name = $interface_matched.group(1)
logical_interface=$interface_name
            #else
logical_interface=$iname
            #end if
eval "physical_interface=\\${logical_interface_mapping_\${logical_interface}}"
if [ ! -z "\${physical_interface}" ]; then
    eval "physical_mac=\\${physical_interface_mac_\${physical_interface}}"
fi
if [ ! -z "\${physical_mac}" ]; then
    echo "   hwaddress ether \${physical_mac}" >> /etc/network/interfaces
fi
            #if not $subinterfacepattern.match($iname)
if [ -f "/etc/modprobe.conf" ] && [ ! -z "\${physical_interface}" ]; then
    grep \${physical_interface} /etc/modprobe.conf | sed "s/\${physical_interface}/$iname/" >> /etc/modprobe.conf.cobbler
    grep -v \${physical_interface} /etc/modprobe.conf >> /etc/modprobe.conf.new
    rm -f /etc/modprobe.conf
    mv /etc/modprobe.conf.new /etc/modprobe.conf
fi
            #end if
        #end if
    #end if

    #if $iface_type in ("master","bond","bonded_bridge_slave")
        ## if this is a bonded interface, configure it in modprobe.conf
if [ -f "/etc/modprobe.conf" ]; then
        #if $osversion == "rhel4"
    echo "install $iname /sbin/modprobe bonding -o $iname $bonding_opts" >> /etc/modprobe.conf.cobbler
        #else
    echo "alias $iname bonding" >> /etc/modprobe.conf.cobbler
        #end if
fi
        #set $bondslaves = ""
        #for $bondiname in $ikeys
            #set $bondidata                = $interfaces[$bondiname]
            #set $bondiface_type           = $bondidata.get("interface_type", "").lower()
            #set $bondiface_master         = $bondidata.get("interface_master", "")
            #if $bondiface_master == $iname
                #set $bondslaves += $bondiname + " "
            #end if
        #end for
echo "   bond-slaves $bondslaves" >> /etc/network/interfaces

        #if $bonding_opts != ""
            #for $bondopts in $bonding_opts.split(" ")
                 #set [$bondkey, $bondvalue] = $bondopts.split("=")
echo "   bond-$bondkey $bondvalue" >> /etc/network/interfaces
            #end for
        #end if
    #elif $iface_type in ("slave","bond_slave") and $iface_master != ""
echo "   bond-master $iface_master" >> /etc/network/interfaces
    #end if

    #if $iface_type == "bridge"
        #set $bridgeslaves = ""
        #for $bridgeiname in $ikeys
            #set $bridgeidata                = $interfaces[$bridgeiname]
            #set $bridgeiface_type           = $bridgeidata.get("interface_type", "").lower()
            #set $bridgeiface_master         = $bridgeidata.get("interface_master", "")
            #if $bridgeiface_master == $iname
                #set $bridgeslaves += $bridgeiname + " "
            #end if
        #end for
echo "    bridge_ports $bridgeslaves" >> /etc/network/interfaces
        #if $bridge_opts != ""
        #for $bridgeopts in $bridge_opts.split(" ")
            #set [$bridgekey, $bridgevalue] = $bridgeopts.split("=")
echo "   bridge_$bridgekey $bridgevalue" >> /etc/network/interfaces
            #end for
        #end if
    #elif $iface_type in ["bridge_slave", "bonded_bridge_slave"] and $iface_master != ""
        #pass
    #end if

    #if $static
        #if $iname in $promisc_interfaces
echo "   address 0.0.0.0" >> /etc/network/interfaces
echo "   up ip link set $iname up" >> /etc/network/interfaces
echo "   up ip link set $iname promisc on" >> /etc/network/interfaces
echo "   down ip link set $iname promisc off" >> /etc/network/interfaces
echo "   down ip link set $iname down" >> /etc/network/interfaces
        #elif $ip != ""
echo "   address $ip" >> /etc/network/interfaces
            #if $netmask == ""
                ## Default to 255.255.255.0?
                #set $netmask = "255.255.255.0"
            #end if
echo "   netmask $netmask" >> /etc/network/interfaces
            #import netaddr
            #set interface_network = $netaddr.IPNetwork('%s/%s' % ($ip, $netmask))
            #set interface_network_str = $str($interface_network)
            #if $if_gateway != ""
echo "   gateway $if_gateway" >> /etc/network/interfaces
            #elif $gateway != ""
                #set gateway_address = $netaddr.IPAddress($gateway)
                #if $gateway_address in $interface_network
echo "   gateway $gateway" >> /etc/network/interfaces
                #end if
            #end if
        #end if
    #else
        #pass
    #end if

    #if $mtu != ""
echo "   mtu $mtu" >> /etc/network/interfaces
    #end if

    #if not $nameserver_set
        #if $iface_type not in ("slave","bond_slave","bridge_slave","bonded_bridge_slave")
            #set $nameservers = ' '.join($name_servers)
            #set $nameserver_search = ' '.join($name_servers_search)
            #if $nameservers != ""
echo "   dns-nameservers $nameservers" >> /etc/network/interfaces
            #end if
            #if $nameserver_search != ""
echo "   dns-search $nameserver_search" >> /etc/network/interfaces
            #end if
            #set $nameserver_set = 1
        #end if
    #end if

    #for $route in $static_routes
        #set routepattern = $re.compile("[0-9/.]+:[0-9.]+")
        #if $routepattern.match($route)
            #set [$network, $router] = $route.split(":")
echo "   up ip route add $network via $router dev $iname" >> /etc/network/interfaces
        #else
echo "   # Warning: invalid route: $route" >> /etc/network/interfaces
        #end if
    #end for

echo "" >> /etc/network/interfaces
#end for

for logical_interface in \${logical_interfaces}; do
    eval "used_logical_interface=\\${used_logical_interface_\${logical_interface}}"
    if [ ! -z "\${used_logical_interface}" ]; then
        # ignore logical interface that is already generated in above
        echo "ignore used logical interface \${logical_interface}" >> /tmp/network_log
        continue
    fi
    echo "add logical interface \${logical_interface} into network config since it is not set above" >> /tmp/network_log
    eval "physical_interface=\\${logical_interface_mapping_\${logical_interface}}"
    if [ ! -z "\${physical_interface}" ]; then
        echo "auto \${logical_interface}" >> /etc/network/interfaces
        echo "iface \${logical_interface} inet static" >> /etc/network/interfaces
        eval "mac=\\${physical_interface_mac_\${physical_interface}}"
        if [ ! -z "\$mac" ]; then
            echo "   hwaddress ether \${mac}" >> /etc/network/interfaces
        fi
        echo "" >> /etc/network/interfaces
        if [ -f "/etc/modprobe.conf" ] && [ ! -z "\${physical_interface}" ]; then
            grep \${physical_interface} /etc/modprobe.conf | sed "s/\${physical_interface}/\${logical_interface}/" >> /etc/modprobe.conf.cobbler
            grep -v \${physical_interface} /etc/modprobe.conf >> /etc/modprobe.conf.new
            rm -f /etc/modprobe.conf
            mv /etc/modprobe.conf.new /etc/modprobe.conf
        fi
    fi
done

if [ -f "/etc/modprobe.conf" ]; then
    cat /etc/modprobe.conf.cobbler >> /etc/modprobe.conf
    rm -f /etc/modprobe.conf.cobbler
fi
if [ -f "/etc/udev/rules.d/70-persistent-net.rules" ]; then
    rm -f /etc/udev/rules.d/70-persistent-net.rules
fi
if [ -f "/etc/udev/rules.d/70-persistent-net.rules.new" ]; then
    mv /etc/udev/rules.d/70-persistent-net.rules.new /etc/udev/rules.d/70-persistent-net.rules
fi
## End post_install_network_config generated code
