[%
    hostgroups = {};
    alias      = {};
    last_kunde = {};
    tab        = "\t";

    FOREACH host IN hosts;
        UNLESS last_kunde.id.defined && last_kunde.id == host.kunde.id;
            IF last_kunde.id.defined %]

[%          END;
            last_kunde = host.kunde -%]
# Kunde #[% host.kunde.id %]:[% host.kunde.name %]
[%      END;
        SET host.fqdn = host.ip UNLESS host.fqdn.defined;

        contact_groups = [];
        FOR suffix IN [ '', '-bf' ];
            IF host.flags.item("monitoring$suffix");
                hostgroup = "$host.kunde.name$suffix";
                contact_groups.push(hostgroup);
                SET hostgroups.$hostgroup = [] UNLESS hostgroups.exists(hostgroup);
                hostgroups.$hostgroup.push(host.fqdn);
                SET alias.$hostgroup = "$host.kunde.id$suffix" UNLESS alias.exists(hostgroup);
            END;
        END;

        IF host.exists('nagiosconf');
            nc = host.nagiosconf;
	ELSE;
	    nc = {};
	END;
	SET nc.host_name      = host.fqdn                UNLESS nc.exists('host_name');
	SET nc.alias          = host.fqdn                UNLESS nc.exists('alias');
	SET nc.address        = host.ip                  UNLESS nc.exists('ip');
	SET nc.use            = 'generic-host'           UNLESS nc.exists('use');
	SET nc.contact_groups = contact_groups.join(',') UNLESS nc.exists('contact_groups');
	SET nc.parents        = host.parents.join(',')   IF !nc.exists('parents') and host.parents.size;
%]
define host {
[%
        FOREACH attr IN nc;
            tabs = ( 31 - attr.key.length ) / 8;
	    SET tabs = 1 IF tabs < 1;
-%]
	[% attr.key; tab.repeat(tabs); attr.value %]
[%      END -%]
}
[%  END %]

# Hostgroups:
[%  FOR hostgroup IN hostgroups %]
define hostgroup {
	hostgroup_name	[% hostgroup.key %]
	alias		[% alias.${hostgroup.key} %]
	members		[% hostgroup.value.join(',') %]
}
[%   END -%]
