#!/bin/bash
#
set -e
#
# Create bash profile script
#
cat <<SCRIPT >/root/.bash_profile
#!/bin/bash
#
export PS1="\h(bash)#"
echo "Use vtysh to connect to FRR daemon"
echo
SCRIPT
#
# Build hosts file
#
{% for k,v in hostvars.items() if k != inventory_hostname and v.af.ipv4|default(False) %}
echo "{%- if v.loopback.ipv4 is defined %}{{ v.loopback.ipv4|ipaddr('address') }} {% endif %}
{%- for l in v.interfaces|default([]) if 'ipv4' in l and l.ipv4 != True and l.ipv4|ipv4 %}{{ l.ipv4|ipaddr('address') }} {% endfor %}{{ k }}" >>/tmp/hosts
{% endfor %}
sort /tmp/hosts|uniq >>/etc/hosts

#
# Enable FRR modules (if not using containerlab bind-mounted /etc/frr/daemons)
#
{% if clab is not defined or clab.binds|default({})=={} %}
{% set modlist = {'bgp':'bgpd','ospf':['ospfd','ospf6d'],'isis':'isisd','vrf':'bgpd','mpls':'ldpd'} %}
{% for m in module|default([]) if modlist[m] is defined %}
{%   for frr_m in [modlist[m]]|flatten %}
echo "{{ frr_m }}=yes" >>/etc/frr/daemons
{%   endfor %}
{% endfor %}

/usr/lib/frr/frrinit.sh restart
{% endif %}

#
# Create loopbacks and stub devices
#
{% for i in (interfaces+[{ 'type': 'loopback', 'ifname': 'lo0'}]) if i.type in ['loopback','stub'] %}
if [ ! -e /sys/devices/virtual/net/{{ i.ifname }} ]; then
  ip link add {{ i.ifname }} type dummy
  ip link set dev {{ i.ifname }} up
fi
{% endfor %}

#
# Add vtysh.conf file
echo "service integrated-vtysh-config" >/etc/frr/vtysh.conf
#
# Set Ethernet interface MTU
{% for l in interfaces|default([]) if l.mtu is defined %}
ip link set {{ l.ifname }} mtu {{ l.mtu }}
{% endfor %}
#
# Rest of initial configuration done through VTYSH
#
cat >/tmp/config <<CONFIG
hostname {{ inventory_hostname }}
!
{% if 'ipv6' in af %}
ipv6 forwarding
{% endif %}
!
{% if loopback is defined %}
interface lo0
 no shutdown
{%  if loopback.ipv4 is defined %}
 ip address {{ loopback.ipv4 }}
{%  endif %}
{%  if loopback.ipv6 is defined %}
 ipv6 address {{ loopback.ipv6 }}
{%  endif %}
!
{% endif %}
{% for l in interfaces|default([]) %}
interface {{ l.ifname }}
! no shutdown
{% if l.name is defined %}
 description {{ l.name }}{{ " ["+l.role+"]" if l.role is defined else "" }}
{% elif l.type|default("") == "stub" %}
 description Stub interface
{% endif %}
{% if l.ipv4 is defined and (l.ipv4 is string or loopback.ipv4 is defined) %}
 ip address {{ l.ipv4 if l.ipv4 is string else loopback.ipv4 }}
{% else %}
 ! no ip address
{% endif %}
{% if l.ipv6 is defined %}
{%  if l.ipv6 is string and l.ipv6|ipv6 %}
 ipv6 address {{ l.ipv6 }}
{%  endif %}
 ipv6 nd ra-interval 5
 no ipv6 nd suppress-ra
{% else %}
 ! Note, currently does not take for OS configured interfaces; known issue
 ipv6 nd suppress-ra
{% endif %}
{% if l.bandwidth is defined %}
 bandwidth {{ l.bandwidth  }}
{% endif %}
!
{% endfor %}
do write
CONFIG
vtysh -f /tmp/config

# Workaround for FRR issue with disabling ipv6 (https://github.com/FRRouting/frr/issues/7738)
{% for l in interfaces if l.type in ['lan','p2p','stub'] and l.ipv6 is not defined %}
sysctl -qw net.ipv6.conf.{{ l.ifname }}.disable_ipv6=1
{% endfor %}

exit 0
