=head1	NAME

oobuild - OpenOffice.org file creation utility - (c) Genicorp 2004


oobuild filename.sxw

oobuild filename.sxw --title "My Document" --subject "Test"

oobuild filename --class spreadsheet --source "data.csv" --tablesize "8x16"

cat data.txt | oobuild filename.sxw -

=head1	OPTIONS

	--class		Document class (text, spreadsheet, drawing, presentation)
			Default: text
	--creator	The author of the document. Default: the current user's
			login name.
	--date		Creation date. Default is current local time.
			If provided, must be in YYYY-MM-DDTHH:MM:SS format
	--description	The description (abstract) of the document. Default: none.
	--force		(no value). If this option is on, any existing file with
			the same path as the target file will be replaced. Without
			this option, the program will fail if the target exists.
	--generator	Software signature to be stored in the file (not visible
			for the end user).
			Default: "Genicorp OpenOffice::OODoc <version>"
	--keywords	A list of comma-separated keywords. Default: none.
	--source	A text file, to be used as the content of the document.
			If the document class is 'text', each line is loaded as a new
			paragraph with the standard style. If the document class is
			'spreadsheet', the file is processed as CSV data and loaded
			in one sheet. If the document class is neither 'text' nor
			'spreadsheet', the file is not processed.
			If source = '-', or if a '-' argument is provided, the data
			file is read through the standard input.
	--subject	The subject of the document. Default: none.
	--tablename	The name of the sheet to be created if the document class is
			'spreadsheet' and if a data file is provided. Default: the name
			of the data file, or "Unnamed Sheet" if the data is read from
			the standard input.
	--tablesize	The size of the sheet to be created if the document class is
			'spreadsheet' and if a data file is provided, in 'HxW' format
			where H is the number of lines and W the number of columns.
			Default: '16x8'
	--title		The title of the document. Default: "Untitled".



use OpenOffice::OODoc	1.204;
use Getopt::Long;

my $INPUT	= undef;
my $input	= undef;

my $generator = 'Genicorp OpenOffice::OODoc ' . $OpenOffice::OODoc::VERSION;

my $targetfile = $ARGV[0]
	or die "Usage: oobuild <filename> [--options]\n";

	''			=> \(my $stdin = undef),
	'class=s'		=> \(my $class = 'text'),
	'date=s'		=> \(my $date = ooLocaltime),
	'generator=s'		=> \$generator,
	'title=s'		=> \(my $title = 'Untitled'),
	'subject=s'		=> \(my $subject = ''),
	'description=s'		=> \(my $description = ''),
	'keywords=s'		=> \(my $keywords = ''),
	'creator=s'		=> \(my $creator = scalar getpwuid($<)),
	'source=s'		=> \(my $source = undef),
	'tablesize=s'		=> \(my $tablesize = '16x8'),
	'tablename=s'		=> \(my $tablename = undef),
	'force'			=> \(my $force = undef)

if ( -e $targetfile && ! defined $force)
	die	"File $targetfile exists. I don't create it.\n"	.
		"Use --force to replace it.\n";

my $archive	= ooFile($targetfile, create => $class)
	or die "File creation failure\n";


my $meta	= ooMeta(archive => $archive);
$meta->keywords(split ',', $keywords) if $keywords;


if ($stdin || $source)
	my $INPUT = undef;
	if ($stdin || ($source eq '-'))
		$source = '-';
		if ( -e $source && -r $source )
			open(SOURCE, "<", $source);
	my $content = ooDocument(archive => $archive);
	if ($class eq 'text')
		my $first_para = $content->getParagraph(0);
		while (my $para = <$INPUT>)
			$content->appendParagraph(text => $para);
		$content->removeElement($first_para) if $first_para;
	elsif ($class eq 'spreadsheet')
		my ($cols, $lns) = split 'x', $tablesize;
		unless ($tablename)
			if ($source gt '-')
				$tablename = $source;
				$tablename = 'Unnamed Sheet';
		my $first_sheet = $content->getTable(0);
		my $sheet = $content->appendTable($tablename, $cols, $lns);
		$content->removeElement($first_sheet) if $first_sheet;
		ROW: for (my $i = 0; my $record = <$INPUT>; $i++)
			last ROW unless $record;
			chomp $record;
			my @data = split ';', $record;
			my $row = $content->getTableRow($sheet, $i);
			CELL: for (my $j = 0; my $value = shift @data; $j++)
				last CELL unless defined $value;
				$content->cellValue($row, $j, $value);
		warn "Source text loading not allowed for this class\n";
	close $INPUT unless ($source eq '-');