#!/usr/bin/perl
#-----------------------------------------------------------------------------
#	$Id : odf_set_title 0.3 2010-01-27 JMG$
#-----------------------------------------------------------------------------

=head1	NAME

odf_set_title - Set the title of a document using the first heading of the content

=head1	USAGE

odf_set_title <filename> <new title>

=head1	SYNOPSIS

This sample script outputs the current title of an ODF file, then it replaces
it using a command line argument as the new title. If the command line doesn't
provide a new title, then the program uses the first heading text in the
document body. If the document doesn't contain any heading element, nothing
is changed.  

=cut
 
use OpenOffice::OODoc	2.112;

my $new_title   = undef;

	# create an ODF file object
	# using the 1st command line argument as filename
my $oofile	= odfContainer($ARGV[0]);
	# exit if $filename isn't available or can't be open
	# as a regular ODF file
die "Unavailable file $ARGV[0]\n" unless $oofile;

	# create a metadata-aware object, linked to the File object
my $doc_meta	= odfMeta(container => $oofile);

	# extract the title form the metadata object
	# (without argument, the 'title' method is a 'get' accessor)
my $title	= $doc_meta->title;

	# if the title is defined, display it and exit
if (defined $title)
	{
	print "The existing title is \"$title\"\n";
	}

if (defined $ARGV[1])
        {
        $new_title = $ARGV[1];
        }
elsif (! defined $title)
	{
	# use the 1st heading of the content
	# create a content-aware object linked to the same container
	my $doc_text	= odfDocument
			(
			container	=> $oofile,
			read_only	=> 'true'
			);
	# get the text of the first heading element
	my $text = $doc_text->getHeadingText(0);
	if ($text)
		{
		# use this text, if defined, as the title
		$new_title = $text;
		}
	else
		{
		# there was no heading in the document,
		# so nothing is done
		warn	"No heading text in the document.\n"	.
			"Nothing is changed.\n";
		}
	}
		
		# the new title (if any) is saved
if (defined $new_title)
	{
	# set the new title
	print "The new title is \"$new_title\"\n";
	$doc_meta->title($new_title);
	# update the revision number
	$doc_meta->increment_editing_cycles;
	# commit the update
	$oofile->save;
	}

exit;