#!/usr/bin/perl

use Data::Dumper;
use strict;
use XML::Simple;
use File::Path qw(mkpath);

my %opts = ( r => '^(sm/|.+/sm/)');
LocalGetOptions(\%opts,
   ['c|master-db-file=s', 'Input configuration file (the original)'],
   ['C|slave-db-file=s', 'Output configuration file (the one without images)'],
   '',
   ['v|verbose','Verbose'],
   ['h|help','Command line help']) || die "bad options";

die "You need to specifiy 2 kimdaba index files (-c & -C)"
  if (!$opts{'c'} || !$opts{'C'});

Verbose("Reading $opts{c}\n");
my $doc1 = XMLin($opts{'c'}, ForceArray => 1, KeyAttr => 'bogus');
die "Couldn't load $opts{'c'}" if (!$doc1);

# remove the images
Verbose("Removing images node\n");
delete $doc1->{'images'};
$doc1->{'images'} = [];

# save out the results
Verbose("Saving results as $opts{'C'}\n");
XMLout($doc1, RootName => 'KPhotoAlbum', OutputFile => $opts{'C'});

sub Verbose {
    print @_ if ($opts{'v'});
}

#######################################################################
# Getopt::GUI::Long optionality
#

sub LocalGetOptions {
    if (eval {require Getopt::GUI::Long;}) {
  	import Getopt::GUI::Long;
        # optional configure call
	Getopt::GUI::Long::Configure(qw(display_help no_ignore_case));
  	return GetOptions(@_);
    }
    require Getopt::Long;
    import Getopt::Long;
    # optional configure call
    Getopt::Long::Configure(qw(auto_help no_ignore_case));
    GetOptions(LocalOptionsMap(@_));
}

sub LocalOptionsMap {
    my ($st, $cb, @opts) = ((ref($_[0]) eq 'HASH') 
  			    ? (1, 1, $_[0]) : (0, 2));
    for (my $i = $st; $i <= $#_; $i += $cb) {
  	if ($_[$i]) {
	    next if (ref($_[$i]) eq 'ARRAY' && $_[$i][0] =~ /^GUI:/);
  	    push @opts, ((ref($_[$i]) eq 'ARRAY') ? $_[$i][0] : $_[$i]);
  	    push @opts, $_[$i+1] if ($cb == 2);
  	}
    }
    return @opts;
}

=pod

=head1 NAME

kphototemplate - creates a template index.xml from a fully loaded one

=head1 SYNOPSIS

  kphototemplate -c index.xml -C index-new.xml

=head1 DESCRIPTION

B<kphototemplate> is designed to create a new and empty index.xml file
from an original one that is full of images.  It very simply reads in
the original file (specified with the I<-c> switch), removes the
images from it but leaves all the category names and information, and
then writes back out a new copy (specified with the I<-C> switch).

This is useful when you want to categorize a bunch of new images away
from your master image set, but still want the existing category
names, etc, for quick assignment.  When you get the new images and new
index database back home, use I<kphotomerge> to merge the new image
database back into the original.

Warning: when kphotoalbum starts up with the new index database file
it'll complain about it containing on images.  You can safely ignore
this warning.

=head1 OPTIONS

=over

=item -c INDEXFILE1

The main KPhotoAlbum DB index file from which the categories, etc will
be copied.

=item -C INDEXFILE2

The KPhotoAlbum DB index file to write back out.

=item -v

Verbose mode

=item -h

Help output.

=back

=head1 AUTHOR

Wes Hardaker <hardaker@users.sourceforge.net>

=head1 COPYRIGHT

Copyright (c) 2009  Wes Hardaker.  All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=cut

