<%#
kind: snippet
name: preseed_networking_setup
description: this will configure your host networking, it configures your primary interface as well
    as other configures NICs. It supports physical, VLAN and Alias interfaces. It's intended to be
    called in your preseed finish template.
%>
<% subnet = @host.subnet -%>
<% dhcp = subnet.dhcp_boot_mode? -%>

real=`ip -o link | grep <%= @host.mac -%> | awk '{print $2;}' | sed s/://`
cat << EOF > /etc/network/interfaces
#loopback
auto lo
iface lo inet loopback

#<%= @host.primary_interface.identifier %>
auto $real
allow-hotplug $real
iface $real inet <%= dhcp ? 'dhcp' : 'static' %>
<% unless dhcp -%>
    address <%= @host.ip %>
    gateway <%= @host.subnet.gateway  %>
    netmask <%= @host.subnet.mask  %>
    dns-nameservers <%= @host.subnet.dns_primary %> <%= @host.subnet.dns_secondary %>
    dns-search <%= @host.domain %>
<% end %>
EOF

<% @host.managed_interfaces.each do |interface| %>
<% next if !interface.managed? || interface.subnet.nil? || interface.primary -%>
<% subnet = interface.subnet -%>
<% virtual = interface.virtual? -%>
<% dhcp = subnet.nil? ? false : subnet.dhcp_boot_mode? -%>
real=`ip -o link | grep <%= interface.mac -%> | awk '{print $2;}' | sed s/:$//`
<% if virtual -%>
real=`echo <%= interface.identifier -%> | sed s/<%= interface.physical_device -%>/$real/`
<% end -%>

cat << EOF >> /etc/network/interfaces
#<%= interface.identifier %>
auto $real
iface $real inet <%= dhcp ? 'dhcp' : 'static' %>
<% unless dhcp -%>
    address <%= interface.ip %>
    netmask <%= subnet.mask %>
<% end %>
EOF
<% end -%>
