#!/bin/vbash
source /opt/vyatta/etc/functions/script-template

if [ "$(id -g -n)" != 'vyattacfg' ] ; then
    exec sg vyattacfg -c "/bin/vbash $(readlink -f $0) $@"
fi

# Configuration items start here
configure

{% for intf in interfaces if intf.gateway.protocol|default('') == 'vrrp' %}
{%   for af in 'ipv4','ipv6' if af in intf.gateway %}
{% set vrrp_name = "IF" ~ intf.ifindex ~ af -%}

set high-availability vrrp group {{ vrrp_name }} interface {{ intf.ifname }}
set high-availability vrrp group {{ vrrp_name }} vrid {{ intf.gateway.vrrp.group }}
set high-availability vrrp group {{ vrrp_name }} address {{ intf.gateway[af] }}

{%     if 'priority' in intf.gateway.vrrp %}
set high-availability vrrp group {{ vrrp_name }} priority {{ intf.gateway.vrrp.priority }}
{%     endif %}
{%     if not intf.gateway.vrrp.preempt|default(False) %}
set high-availability vrrp group {{ vrrp_name }} no-preempt
{%     endif %}

{%   endfor %}
{% endfor %}

# Commit, save and exit from subshell

commit
save
exit
