#! /usr/bin/perl

use strict;
use warnings;

use lib "/opt/vyatta/share/perl5/";
use XorpConfigParser;
use File::Copy;

my $orig_cfg = shift;
exit 1 unless $orig_cfg;

my $xcp = new XorpConfigParser();
$xcp->parse($orig_cfg);

my $interfaces = $xcp->get_node(['interfaces']);
exit 0 unless $interfaces; # tunnel interfaces not configured

my $intf_children = $interfaces->{'children'};
if (defined $intf_children){
  foreach my $intf (@{$intf_children}){
     if ( $intf->{'name'} =~ /tunnel tun[0-9]*/ ){
       my $intf_params = $xcp->get_node(['interfaces', $intf->{'name'}, 'parameters', 'ip']);
       if (defined $intf_params){
         foreach my $intf_param (@{$intf_params->{'children'}}){
           if ( ($intf_param->{'name'} =~ /tos/) or
                ($intf_param->{'name'} =~ /ttl/) or
                ($intf_param->{'name'} =~ /key/) ) {
              $xcp->create_node(['interfaces', $intf->{'name'}, $intf_param->{'name'}]);
           } elsif ($intf_param->{'name'} =~ /bridge-group/) {
              foreach my $bridge_option (@{$intf_param->{'children'}}){
                $xcp->create_node(['interfaces', $intf->{'name'}, $intf_param->{'name'}, $bridge_option->{'name'}]);
              }
           }
         }
         $xcp->comment_out_node($xcp->get_node(['interfaces', $intf->{'name'}, 'parameters']));
       }
    }
  }

  my $tmpname = "/tmp/vyatta_migrate_tunnel_options.$$";
  open (my $tmp, '>', $tmpname)
    or die "Can't open: $tmpname: $!";

  select $tmp;
  $xcp->output(0);
  select STDOUT;
  close $tmp;

  move($tmpname, $orig_cfg)
    or die "Move $tmpname to $orig_cfg failed: $!";
}

exit 0;
