#!/usr/bin/perl

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

##########################################################################
# Copyright (c) 2010-2012 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::gated;
use OSPF::LSDB::YAML;

sub usage(@) {
    print STDERR "Error: @_\n" if @_;
    print STDERR <<EOF;
Convert gated OSPF link state database to YAML file.  If the gated
dump content file is not given on the command line, gdc is invoked
on the local machine.

Usage: $0 [-h] [-D dump] [-H user\@host] [-S skip] [ospf.yaml]
    -h           help, print usage
    -D dump      file containg output of 'gdc dump'
    -H user\@host use ssh to login into user\@host to run gdc there
    -S skip      skip first number of gated entries in dump file
    ospf.yaml    output file, default stdout
EOF
    exit(2);
}

sub main() {
    my($file, $ssh, $skip);
    GetOptions(
	'h'   => sub { usage() },
	'D=s' => \$file,
	'H=s' => \$ssh,
	'S=i' => \$skip,
    ) or usage("Bad option");
    usage("Only one argument allowed") if @ARGV > 1;

    my $gated = OSPF::LSDB::gated->new(ssh => $ssh);
    my $ospf = $gated->parse(dump => !$file, file => $file, skip => $skip);

    my $yaml = OSPF::LSDB::YAML->new($gated);
    if (@ARGV > 0) {
	$yaml->DumpFile($ARGV[0]);
    } else {
	print $yaml->Dump();
    }
}

main();
