#!/usr/bin/perl

use strict;
use warnings;

use FindBin;
use lib "$FindBin::RealBin/../lib";
use Ceni::Backend;
use Getopt::Long;

my $dbg = 0;
my $iface;
my $in = '/etc/network/interfaces';

GetOptions('debug' => \$dbg, 'iface=s' => \$iface, 'input=s' => \$in,);

if (not $in or not $iface) {
	print "Usage: Ceni_read_config --iface <iface> [--input <file>]\n";
	exit 0;
}

my $source = new Ceni::Backend({ debug => $dbg, file => $in, });

$source->parse_eni or exit 1;

my $config = $source->get_iface_conf($iface);

if ($config) {
	for my $k (sort keys %{$config}) {
		if ($k eq 'stanza') {
			for my $s (sort keys %{ $config->{$k} }) {
				print $s . ',' . $config->{$k}->{$s} . "\n";
			}
			next;
		}
		elsif ($k =~ /^(pre-|post-)?(up|down)$/) {
			for my $s (@{ $config->{$k} }) {
				print $k . ',' . $s . "\n";
			}
			next;
		}
		print $k . ',' . $config->{$k} . "\n";
	}
}

__END__

=head1 NAME

Ceni_read_config - read network interface configuration from input file

=head1 SYNOPSIS

  Ceni_read_config --iface <iface> [--input <file>]

=head1 DESCRIPTION

This script reads an input file containing ifupdown configuration data, and
prints the configuration data.

=head1 OPTIONS

=over

=item B<--iface> <iface>

The target network interface with kernel name <iface>.
Exmaples of <iface> are "eth0", "ath0" and "wlan0".

=item B<--input> <file>

Input file containing network interface ifupdown configuration.
If no input file is specified, /etc/network/interfaces is assumed.

=back

=head1 AUTHOR

Kel Modderman, E<lt>kel@otaku42.deE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2007 - 2010 by Kel Modderman

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this package; if not, see <http://www.gnu.org/licenses>

On Debian GNU/Linux systems, the text of the GPL-2 license can be
found in /usr/share/common-licenses/GPL-2.

=cut
