#!/usr/bin/perl -w

=begin COPYRIGHT

jdresolve-dumpdb - Dumps the databases created by jdresolve into text format

Copyright (c) 1999 John Douglas Rowell

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 program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

=end COPYRIGHT
=cut

use strict;
use Getopt::Long;
use DB_File;

my $APPNAME = "jdresolve-dumpdb";
my $VERSION = "0.5.2";
my $AUTHOR = "John Douglas Rowell";
my $YEAR = 1999;
my $BUGS = "bugs\@jdrowell.com";

my %opts = ( help => 0, version => 0, database => '' );

my %optctl = (
	"h" => \$opts{help},
	"help" => \$opts{help},
	"v" => \$opts{version},
	"version" => \$opts{version},
	"database" => \$opts{database} );
my @optlst = ("h", "help", "v", "version", "database=s");

my $usage =<<"-x-";
Usage: $APPNAME [-hv] [--help] [--version] --database=<db path>

Report bugs to $BUGS
-x-

my $version =<<"-x-";
$APPNAME $VERSION
Copyright (C) $YEAR $AUTHOR
$APPNAME comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of $APPNAME
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.
-x-

GetOptions(\%optctl, @optlst);

if ($opts{help}) {
	print <<"-x-"; exit;
$version
$usage
Dumps the hosts/classes database created by jdresolve to stdout.

   --help or -h
      help (this text).
   --version or -v
      display version information.
   --database=<db path>
      path to database that holds resolved hosts/classes

-x-
}

if ($opts{version}) {
	print <<"-x-"; exit;
$version
try $APPNAME --help for help
-x-
}

if ($opts{database} eq '') {
        print <<"-x-"; exit;
$usage
try $APPNAME --help for help
-x-
}

$opts{database} eq '' and die "please specify a database";

my %DB;
my $ipmask = '^(\d+)\.(\d+)\.(\d+)\.(\d+)';

tie(%DB, 'DB_File', $opts{database}) or die "can't open database '$opts{database}'";

my $i = 1;

for (%DB) {
	if ($i++ %2) {
		if (/$ipmask$/) { print "$_ " } 
		else { print join('.', reverse(split /\./)), ' ' }
	} else { print "$_\n" }
}

