The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl
#-----------------------------------------------------------------------------
#	$Id : odf_set_fields 0.4 2010-01-27 JMG$
#-----------------------------------------------------------------------------

=head1	NAME

odf_set_fields - Set names & values for user-defined fields

=head1	USAGE

odf_set_fields <filename> -options 

=head1	SYNOPSIS

Sample script updating or creating 4 user-defined fields of an ODF
file and adding new keywords. Existing keywords are preserved. Old user-defined
fields names and values are deleted and replaced. The revision number of the
document is incremented by 1.

The keywords must be passed as a comma-separated list through the -keywords
option.

The user-defined fields/options are :
-contact -organization -status -diffusion

Examples:

odf_set_fields foo.odt -contact "Donald Duck" -organization "Genicorp"

odf_set_fields foo.odt -status "Complete" -keywords "software, office"

=cut

use OpenOffice::OODoc::Meta;
use Getopt::Long;

	# default values for the user-defined fields (examples)
my $contact		= 'Corporate Editor';
my $organization	= 'Foo Unlimited';
my $status		= 'Draft';
my $diffusion		= 'Public/Unclassified';
my $keywords		= undef;

	# get the command line options
GetOptions
	(
	'contact=s'		=> \$contact,
	'organization=s'	=> \$organization,
	'status=s'		=> \$status,
	'diffusion=s'		=> \$diffusion,
	'keywords=s'		=> \$keywords
	);

	# get the command line argument as filename
my $filename	= $ARGV[0]
	or die "usage : odf_set_fields filename [-options]\n";

	# create a meta-data object linked to the file
my $doc	= OpenOffice::OODoc::Meta->new(file => $filename)
	or die "I can't open $filename as an ODF document\n";

	# set the user-defined fields using a list of name/value pairs
	# (the names are hard-coded in my example but they could be
	# dynamically defined just like the values)
$doc->user_defined
		(
		Contact		=> $contact,
		Organization	=> $organization,
		Status		=> $status,
		Diffusion	=> $diffusion
		);

	# add the new keyword list (got from the -keyword command line option
	# if any). The 'keywords' method needs a list, so we have to split the
	# content of the -keywords option, assuming separator=comma
	# There is no risk to introduce some keyword redundancy here, because
	# the 'keywords' method from OpenOffice::OODoc::Meta adds only non-existing
	# keywords
$doc->keywords(split(",", $keywords))	if $keywords;

	# because I'm sometimes a perfectionist, I want to
	# put the program signature in the 'generator' field (unavailable for
	# the end-user, but readable/updateable for OpenOffice::OODoc::Meta just as for
	# the OpenOffice.org software); useful to allow any other program to know
	# if this version comes directly from the StarOffice/OpenOffice suite or
	# has been generated by my program
$doc->generator('odf_set_fields Version 0.4');

	# with the same kind of idea, I want to
	# increment the editing cycles count (just to mimic the OpenOffice.org
	# software each time the file is edited, and maybe
	# to help some other workflow/versioning management program)
$doc->increment_editing_cycles;

	# commit all and leave
$doc->save;
exit;