#!/usr/bin/perl -w
######################################################################
#
# $Id: webjob-mldbm-create-client,v 1.15 2012/01/07 08:01:21 mavrik Exp $
#
######################################################################
#
# Copyright 2004-2012 The WebJob Project, All Rights Reserved.
#
######################################################################
#
# Purpose: Create one or more empty client hashes in 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:q', \%hOptions))
  {
    Usage($$phProperties{'Program'});
  }

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

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

  ####################################################################
  #
  # The be quiet flag, '-q', is optional.
  #
  ####################################################################

  $$phProperties{'BeQuiet'} = (exists($hOptions{'q'})) ? 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 clients, and insert a new sub-hash for
  # each client that doesn't exist.
  #
  ####################################################################

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

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

  MldbmDisconnect($phDbContext, \$sLocalError);

  1;


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

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


=pod

=head1 NAME

webjob-mldbm-create-client - Create one or more empty client hashes in an MLDBM database

=head1 SYNOPSIS

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

=head1 DESCRIPTION

This utility creates one or more empty client hashes in an MLDBM
database.

=head1 OPTIONS

=over 4

=item B<-d db>

Specifies the MLDBM database to update.

=item B<-q>

Be quiet (i.e., only report errors) while executing.

=back

=head1 AUTHOR

Klayton Monroe

=head1 SEE ALSO

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

=head1 LICENSE

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

=cut
