#!/usr/bin/perl

#----------------------------------------------------------------------
# Copyright 1999-2003 Mitel Networks Corporation
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#----------------------------------------------------------------------

use esmith::Image::Manifest;
use Getopt::Long;
$|=1;


#--Subroutine for usage message--------------------------------------

sub usage () {
    my $usageMsg =<<END_TEXT;
    
    mergeDB merges in a field+value pair for each record from one 
            DB-format file to another.
    
    USAGE:
    mergeDB [REQUIRED] [OPTIONAL]
    
    REQUIRED
    --source=SOURCE_DB   DB file to use as a starting point for FIELD_ID source 
    --target=TARGET_DB   DB file to be updated with FIELD_ID from SOURCE_DB
                         NOTE: This file will be updated, and must be writable                     
    --field=FIELD_ID     the record field name to merge
                         
    OPTIONAL  
    --default=<name>     The default value for the new field if the  
                          record does not exist in SOURCE_DB.
                          Default is "unknown"
    --help               Display this help screen
    --verbose            Verbose output
    
END_TEXT
    
    print $usageMsg;
    exit 0;
}


#--Command Line Processing-------------------------------------------
my ($o_source, $o_target, $o_field, $o_def, $o_verbose, $o_help);
&GetOptions (
             "source=s" => \$o_source, 
             "target=s" => \$o_target, 
             "field=s" => \$o_field, 
             "default=s" => \$o_def, 
             "verbose" => \$o_verbose, 
             "help"=> \$o_help);  
&usage if $o_help;
&usage unless $o_source;
&usage unless $o_target;
&usage unless $o_field;

#--Verify variables--------------------------------------------------

my $targetDB = $o_target; 
my $sourceDB = $o_source;
my $field = $o_field;
my $verbose = 0;
if ($o_verbose)
{ $verbose = 1; }

print "Parameters for this run:\n---------------------------------------\n"
. "  Source DB: $sourceDB\n"
. "  Target DB: $targetDB\n"
. "  Field Name: $field\n";
print "***ERROR: $targetDB must be writable!!\n\n" unless -w $targetDB;
$default="unknown";
$default=$o_def if $o_def;
print "  Default Field Value: $default\n";

#--MAIN-------------------------------------------------------------
my $old   = esmith::Image::Manifest->open($targetDB);
my $sample = esmith::Image::Manifest->open_ro($sourceDB);

foreach my $package ( $old->packages ) {
    print "." unless $verbose;
    print "Checking ", $package->key, ": " if $verbose;

    my $sampleRecord = $sample->get($package->key);
 
    my $value = (defined $sampleRecord) ? $sampleRecord->prop("$field") : "$default";
    
    print "$value\n" if $verbose;
    
    $package->set_prop($field, $value);
}

print "\ndone.\n\n";
if ( $field eq "Distribution") {
  print "Don't forget to copy over the actual distribution listings\n"
     ."from $sourceDB to the top of $targetDB\n";
}
