#!/usr/bin/perl
#
# Copyright © 1999 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
# Copyright © 2008 Roger Leigh <rleigh@debian.org
#
# 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, see
# <http://www.gnu.org/licenses/>.
#
#######################################################################

use strict;
use warnings;
use File::Basename;
my $progname = basename($0);
my %db;

use DB_File;
use GDBM_File;
use MLDBM qw(GDBM_File Storable);

die "Filename missing\n" if !@ARGV;
die "$ARGV[0]: $!\n" if !-f $ARGV[0];

if ($progname =~ /catdb/) {
    tie %db, 'DB_File', $ARGV[0], O_RDONLY, 0664, $DB_HASH;
}
elsif ($progname =~ /catgdbm/) {
    tie %db, 'GDBM_File', $ARGV[0], GDBM_READER, 0644;
}
elsif ($progname =~ /catmldbm/) {
    tie %db, 'MLDBM', $ARGV[0], GDBM_READER, 0644;
}
else {
    die "Called for unknown db type\n";
}
shift;

my ($key, $val);

my @keys = sort(keys(%db));
@keys = @ARGV if (@ARGV > 0);

foreach $key (@keys) {
    print "-"x78, "\n";
    if (exists $db{$key}) {
	my $val = $db{$key};
	if (ref($val) eq "HASH") {
	    print "$key:\n";
	    foreach (keys(%{$val})) {
		print "  $_: $val->{$_}\n";
	    }
	} else {
	    print "$key:\n$db{$key}\n";
	}
    }
    else {
	print "*UNDEFINED*\n";
    }
}

untie %db;
exit 0;
