#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell

##########################################################################
# Copyright (c) 2010 Alexander Bluhm <alexander.bluhm@gmx.net>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
##########################################################################

use strict;
use warnings;
use Getopt::Long qw(:config posix_default bundling);
use OSPF::LSDB::YAML;

sub usage(@) {
    print STDERR "Error: @_\n" if @_;
    print STDERR <<EOF;
Convert OSPF LSDB in YAML format from old verion to current.

Usage: $0 [-h] [oldospf.yaml] [ospf.yaml]
    -h              help, print usage
    oldospf.yaml    input file, default stdin
    ospf.yaml       output file, default stdout
EOF
    exit(2);
}

sub main() {
    GetOptions(
	'h' => sub { usage() },
    ) or usage("Bad option");
    usage("Only two arguments allowed") if @ARGV > 2;

    my $yaml = OSPF::LSDB::YAML->new();
    if (@ARGV > 0) {
	$yaml->LoadFile($ARGV[0]);
    } else {
	local $/;
	$yaml->Load(<STDIN>);
    }

    if (@ARGV > 1) {
	$yaml->DumpFile($ARGV[1]);
    } else {
	print $yaml->Dump();
    }
}

main();
