#!/usr/bin/perl -w

=head1	NAME

set_fields - Set names & values for user-defined fields


Sample script updating the 4 user-defined fields of an OpenOffice.org
document and adding new keywords
Existing keywords are preserved
Old user-defined fields names and values are deleted and replaced

usage : set_fields filename [-options]
The 5 options are : -contact -organization -status -diffusion -keywords
 	set_fields foo.sxw -contact "Donald Duck" -organization "Genicorp"
 	set_fields foo.sxw -status "Complete" -keywords "software, office"

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
	'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 : 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 OpenOffice.org 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)
		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('My document management application Version 1.0');

	# 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)
my $editing_cycles = $doc->editing_cycles;
$doc->editing_cycles($editing_cycles + 1);

	# commit all and leave