#!/usr/bin/perl -w
######################################################################
#
# $Id: webjob-mldbm-delete-client,v 1.15 2012/01/07 08:01:21 mavrik Exp $
#
######################################################################
#
# Copyright 2004-2012 The WebJob Project, All Rights Reserved.
#
######################################################################
#
# Purpose: Delete one or more client hashes from an MLDBM database.
#
######################################################################

use strict;
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 optional.
  #
  ####################################################################

  $$phProperties{'DbFile'} = (exists($hOptions{'d'})) ? $hOptions{'d'} : "/var/webjob/db/mldbm/client.db";

  ####################################################################
  #
  # If there isn't at least one argument left, it's an error.
  #
  ####################################################################

  if (scalar(@ARGV) < 1)
  {
    Usage($$phProperties{'Program'});
  }

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

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

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

  ####################################################################
  #
  # Iterate over the list of clients, and delete any that exist.
  #
  ####################################################################

  foreach my $sClientId (@ARGV)
  {
    if ($sClientId !~ /^$$phProperties{'CommonRegexes'}{'ClientId'}$/)
    {
      print STDERR "$$phProperties{'Program'}: Warning='Client ($sClientId) does not pass muster. Proceeding anyway.'\n";
    }
    if (exists($$phDb{$sClientId}))
    {
      delete($$phDb{$sClientId});
    }
    else
    {
      print STDERR "$$phProperties{'Program'}: Warning='Client ($sClientId) does not exist in DB.'\n";
    }
  }

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

  MldbmDisconnect($phDbContext, \$sLocalError);

  1;


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

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


=pod

=head1 NAME

webjob-mldbm-delete-client - Delete one or more client hashes from an MLDBM database.

=head1 SYNOPSIS

B<webjob-mldbm-delete-client> B<[-d db]> B<client-id [client-id ...]>

=head1 DESCRIPTION

This utility deletes one or more client hashes from an MLDBM database.
Any data stored in the client hash will be discarded.

=head1 OPTIONS

=over 4

=item B<-d db>

Specifies the MLDBM database to update.

=back

=head1 AUTHOR

Klayton Monroe

=head1 SEE ALSO

webjob-mldbm-create-client(1), webjob-mldbm-list-clients(1)

=head1 LICENSE

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

=cut
