#!/usr/bin/perl -w
######################################################################
#
# $Id: webjob-mldbm-dump-db,v 1.15 2012/01/07 08:01:22 mavrik Exp $
#
######################################################################
#
# Copyright 2006-2012 The WebJob Project, All Rights Reserved.
#
######################################################################
#
# Purpose: Dump the contents of an MLDBM database.
#
######################################################################

use strict;
use Data::Dumper;
use Fcntl qw(:DEFAULT :flock);
use File::Basename;
use FindBin qw($Bin $RealBin); use lib ("$Bin/../lib/perl5/site_perl", "$RealBin/../lib/perl5/site_perl", "/usr/local/webjob/lib/perl5/site_perl", "/opt/local/webjob/lib/perl5/site_perl");
use Getopt::Std;
use WebJob::MldbmRoutines;
use WebJob::Properties 1.008;

BEGIN
{
  ####################################################################
  #
  # The Properties hash is essentially private. Those parts of the
  # program that wish to access or modify the data in this hash need
  # to call GetProperties() to obtain a reference.
  #
  ####################################################################

  my (%hProperties);

  $hProperties{'CommonRegexes'} = PropertiesGetGlobalRegexes();

  sub GetProperties
  {
    return \%hProperties;
  }
}

######################################################################
#
# Main Routine
#
######################################################################

  ####################################################################
  #
  # Punch in and go to work.
  #
  ####################################################################

  my ($phProperties);

  $phProperties = GetProperties();

  $$phProperties{'Program'} = basename(__FILE__);

  ####################################################################
  #
  # Get Options.
  #
  ####################################################################

  my (%hOptions);

  if (!getopts('d:', \%hOptions))
  {
    Usage($$phProperties{'Program'});
  }

  ####################################################################
  #
  # A database, '-d', is required.
  #
  ####################################################################

  if (!exists($hOptions{'d'}) || !defined($hOptions{'d'}) || length($hOptions{'d'}) < 1)
  {
    Usage($$phProperties{'Program'});
  }
  $$phProperties{'DbFile'} = $hOptions{'d'};

  ####################################################################
  #
  # Connect to the specified DB.
  #
  ####################################################################

  my ($phDb, $phDbContext, $sLocalError);

  $phDbContext = MldbmNewContext
  (
    {
      'DbFile'     => $$phProperties{'DbFile'},
      'DbFlags'    => O_RDONLY,
      'LockFlags'  => LOCK_SH,
      'LockMode'   => "<",
    }
  );
  if (!MldbmConnect($phDbContext, \$sLocalError))
  {
    print STDERR "$$phProperties{'Program'}: Error='$sLocalError'\n";
    exit(2);
  }
  $phDb = $$phDbContext{'DbHandle'};

  ####################################################################
  #
  # Dump the DB hash.
  #
  ####################################################################

  $Data::Dumper::Purity = 1;
  $Data::Dumper::Indent = 1;
  $Data::Dumper::Varname = "phWebJobDb";
  print STDOUT Dumper($phDb);

  ####################################################################
  #
  # Shutdown and go home.
  #
  ####################################################################

  MldbmDisconnect($phDbContext, \$sLocalError);

  1;


######################################################################
#
# Usage
#
######################################################################

sub Usage
{
  my ($sProgram) = @_;
  print STDERR "\n";
  print STDERR "Usage: $sProgram -d db\n";
  print STDERR "\n";
  exit(1);
}


=pod

=head1 NAME

webjob-mldbm-dump-db - Dump the contents of an MLDBM database.

=head1 SYNOPSIS

B<webjob-mldbm-dump-db> B<-d db>

=head1 DESCRIPTION

This utility dumps the contents of an MLDBM database. The output
produced by this utility can be fed to webjob-mldbm-load-db(1) to
initialize a database that has been created with
webjob-mldbm-create-db(1).

=head1 OPTIONS

=over 4

=item B<-d db>

Specifies the MLDBM database to dump.

=back

=head1 AUTHOR

Klayton Monroe

=head1 SEE ALSO

webjob-mldbm-create-db(1), webjob-mldbm-delete-db(1), webjob-mldbm-load-db(1)

=head1 LICENSE

All documentation and code are distributed under same terms and
conditions as B<WebJob>.

=cut
