[%
# vi: ft=tt2

# Returns the network address of a certain interface type for a given host.
#
# @param argv.site      The site to use, default value: current.
# @param argv.host      The hostname to get the interface from.
# @param argv.type      The interface type, one of:
#                         web_int, web_ext, sip_int, sip_ext, ...
# @return out           The network address in CDIR format.

IF !argv.site.length;
  argv.site = 'current';
END;

IF !hosts.${argv.host}.defined;
  argv.host = 'self';
END;

out = '';
FOREACH iface IN sites.${argv.site}.hosts.${argv.host}.interfaces;
  NEXT UNLESS sites.${argv.site}.hosts.${argv.host}.exists(iface);
  NEXT UNLESS sites.${argv.site}.hosts.${argv.host}.$iface.type.grep('^' _ argv.type _ '$').size();

  IF sites.${argv.site}.hosts.${argv.host}.$iface.ip AND
     sites.${argv.site}.hosts.${argv.host}.$iface.netmask;
    PERL -%]
        use Net::Netmask;

        my $network = Net::Netmask->new("[% sites.${argv.site}.hosts.${argv.host}.${iface}.ip %]/[% sites.${argv.site}.hosts.${argv.host}.${iface}.netmask %]");
        $stash->set(out => $network->desc);
[%  END;
  END;

  RETURN;
END;

-%]
