#!/usr/bin/perl -w
######################################################################
#
# $Id: webjob-mldbm-delete-job-key,v 1.9 2012/01/07 08:01:22 mavrik Exp $
#
######################################################################
#
# Copyright 2004-2012 The WebJob Project, All Rights Reserved.
#
######################################################################
#
# Purpose: Delete one or more job keys 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('c:d:j:r', \%hOptions))
  {
    Usage($$phProperties{'Program'});
  }

  ####################################################################
  #
  # A client ID, '-c', is required.
  #
  ####################################################################

  $$phProperties{'ClientId'} = (exists($hOptions{'c'})) ? $hOptions{'c'} : undef;

  if (!defined($$phProperties{'ClientId'}))
  {
    Usage($$phProperties{'Program'});
  }

  if ($$phProperties{'ClientId'} !~ /^$$phProperties{'CommonRegexes'}{'ClientId'}$/)
  {
    print STDERR "$$phProperties{'Program'}: ClientId='$$phProperties{'ClientId'}' Error='Value does not pass muster.'\n";
    exit(2);
  }

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

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

  ####################################################################
  #
  # A job, '-j', is required.
  #
  ####################################################################

  $$phProperties{'Job'} = (exists($hOptions{'j'})) ? $hOptions{'j'} : undef;

  if (!defined($$phProperties{'Job'}))
  {
    Usage($$phProperties{'Program'});
  }

  if ($$phProperties{'Job'} !~ /^$$phProperties{'CommonRegexes'}{'MldbmKeyName'}$/)
  {
    print STDERR "$$phProperties{'Program'}: Job='$$phProperties{'Job'}' Error='Value does not pass muster.'\n";
    exit(2);
  }

  ####################################################################
  #
  # The reverse keys flag, '-r', is optional.
  #
  ####################################################################

  $$phProperties{'ReverseKeys'} = (exists($hOptions{'r'})) ? 1 : 0;

  ####################################################################
  #
  # 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 keys, and delete any that exist.
  #
  ####################################################################

  my $sKey = $$phProperties{'ClientId'} if ($$phProperties{'ReverseKeys'});

  foreach my $sTempKey (@ARGV)
  {
    if ($$phProperties{'ReverseKeys'})
    {
      $$phProperties{'ClientId'} = $sTempKey
    }
    else
    {
      $sKey = $sTempKey
    }
    if (exists($$phDb{$$phProperties{'ClientId'}}))
    {
      my $phClient = $$phDb{$$phProperties{'ClientId'}};
      if (exists($$phClient{'Jobs'}{$$phProperties{'Job'}}{$sKey}))
      {
        delete($$phClient{'Jobs'}{$$phProperties{'Job'}}{$sKey});
        $$phDb{$$phProperties{'ClientId'}} = $phClient;
      }
    }
    else
    {
      print STDERR "$$phProperties{'Program'}: Warning='Client ($$phProperties{'ClientId'}) 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 [-r] [-d db] -c client-id -j job key [key ...]\n";
  print STDERR "\n";
  exit(1);
}


=pod

=head1 NAME

webjob-mldbm-delete-job-key - Delete one or more job keys from an MLDBM database.

=head1 SYNOPSIS

B<webjob-mldbm-delete-job-key> B<[-r]> B<[-d db]> B<-c client-id> B<-j job> B<key [key ...]>

=head1 DESCRIPTION

This utility deletes one or more job keys from an MLDBM database.

=head1 OPTIONS

=over 4

=item B<-c client-id>

Specifies the client ID to update.

=item B<-d db>

Specifies the MLDBM database to update.

=item B<-j job>

Specifies the job to update.

=back

=head1 AUTHOR

Klayton Monroe

=head1 SEE ALSO

webjob-mldbm-create-job(1), webjob-mldbm-delete-job(1), webjob-mldbm-get-job-kvps(1), webjob-mldbm-list-jobs(1), webjob-mldbm-set-job-kvps(1)

=head1 LICENSE

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

=cut
