#!/usr/bin/env perl
# Make a pom.xml for using Zanata.

=pod

=head1 NAME

B<zanata_pom_xml_make> - Make a pom.xml for using Zanata

=head1 SYNOPSIS

B<zanata_pom_xml_make> -h | --help

B<zanata_pom_xml_make> [options] E<lt>pom.xmlE<gt>

=head1 DESCRIPTION
Make a pom.xml for using Zanata.

It either creates a new pom.xml or update an existing pom.xml with
Zanata plugin, Zanata pluginRepositories,
and Zanata maven plugin version.

The original file will be backed up with '.bak' by default.

=head1 ARGUMENTS

=over 4

=item pom.xml

pom.xml to be processed.

=back

=head1 OPTIONS

By default, if none of the options '-m', '-k' are defined,
E<lt>srcDirE<gt> and E<lt>transDirE<gt> will be added to E<lt>configureE<gt>.

However, this behavior is not performed if at least one of the mentioned options are specified.

=over 4

=item B<-h, --help>:

Print brief help message and exits.

=item B<-e, --excludes=PATTERN>

Insert a file exclude pattern as E<lt>excludesE<gt>I<PATTERN>E<lt>/excludesE<gt> to E<lt>configureE<gt>

=item B<-i, --includes=PATTERN>

Insert a file include pattern as E<lt>includesE<gt>I<PATTERN>E<lt>/includesE<gt> to E<lt>configureE<gt>

=item B<-k, --skip>

Insert a E<lt>skipE<gt>I<true>E<lt>/skipE<gt> to E<lt>configureE<gt>

=item B<-m, --enableModules>

Insert a E<lt>enableModulesE<gt>I<true>E<lt>/enableModulesE<gt> to E<lt>configureE<gt>

=item B<-s, --srcDir=DIR>

Set the source directory by
insert a E<lt>zanata.srcDirE<gt>I<DIR>E<lt>/zanata.srcDirE<gt> to E<lt>propertiesE<gt>.
Usually this is a directory that contains .pot files.
Either an absolute path or related to the pom.xml

Default is '.'.


=item B<-t, --transDir=DIR>

Set the translation directory by
insert a E<lt>zanata.transDirE<gt>I<DIR>E<lt>/zanata.transDirE<gt> to E<lt>propertiesE<gt>.
Usually this is a directory that contains .po files.
Either an absolute path or related to the pom.xml

Default is '.'.

=item B<-o, --backupSuffix=STRING>

Set the suffix for backup file.

Default is '.bak'

=item B<-p, --noPluginRepostories>

Do not insert Zanata pluginRepositories.

=back

=head1 FILES

=item zanata.cfg

Configure file for this program.
Directories will be visited in following order:
'~/.config', '/etc', 'etc' and '.'


See 'man zanata_pom_xml_make' for more infomation.
=cut

# Ensure it runs on RHEL5
use 5.008_008;
use strict;
use File::Basename;
use File::Spec;
use File::Copy;
use File::Find::Rule;
use Getopt::Long;
use Pod::Usage;
use XML::Twig::XPath;
#use Data::Dumper;
use lib "lib";
use Zanata::Util;
use Zanata::Util::PomXML;

# path variables
my $scriptDir=dirname($0);

# Definition
my $cfgFilename="zanata.cfg";
my @cfgFileSearchPaths=( "$ENV{'HOME'}/.config", '/etc', 'etc', '.');

## Parse options
my $help=0;
my %optH=(
    'help' => '\$help'
    , 'noPluginRepostories' => 0
    , 'backupSuffix' => '.bak'
);

GetOptions(\%optH,
    , 'help|h'
    , 'excludes|e=s'
    , 'includes|i=s'
    , 'skip|k'
    , 'enableModules|m'
    , 'srcDir|s=s'
    , 'transDir|t=s'
    , 'backupSuffix|o=s'
    , 'noPluginRepostories|p'
) or pod2usage(-1);

### Display help
pod2usage(1) if $help;
pod2usage( {-verbose=>1}) if @ARGV == 0;

my %confH=();
my $isAutoAddDir=1;

for my $k (keys %optH){
    if ($k eq "excludes"){
	$confH{$k}=$optH{$k};
    }elsif($k eq "includes"){
	$confH{$k}=$optH{$k};
    }elsif($k eq "skip"){
	$confH{$k}='true';
	$isAutoAddDir=0;
    }elsif($k eq "enableModules"){
	$confH{$k}='true';
	$isAutoAddDir=0;
    }
}

my $srcDir=($optH{'srcDir'}) ? $optH{'srcDir'} : '.';
my $transDir=($optH{'transDir'}) ? $optH{'transDir'} : '.';

my $pomXml_file=$ARGV[0];

# Subroutines

sub zanataPluginRepoElement_new{
    my ($id, $name, $url, $releases, $snapshots)=@_;
    my $zPRepoStr=<<END;
<pluginRepository>
    <id>$id</id>
    <name>$name</name>
    <url>$url</url>
    <releases>
	<enabled>$releases</enabled>
      </releases>
      <snapshots>
	<enabled>$snapshots</enabled>
      </snapshots>
</pluginRepository>
END

    return Zanata::Util::xml_element_new_xml($zPRepoStr);
}

###########################################################
# Main program
#
my %projInfo={};
my $zanataUtil=Zanata::Util->init(\%projInfo);
my $zanataPluginVersion=$zanataUtil->{'cfgVarH'}->{'ZANATA_MVN_PLUGIN_VER'};

## New a pomXml
my $pomXml;
if (-e $pomXml_file){
    my $content=File::Slurp::read_file($pomXml_file) or die "$!";
    $pomXml=Zanata::Util::PomXML->new(content=>$content, zanataPluginVersion=>$zanataPluginVersion);
}else{
    $pomXml=Zanata::Util::PomXML->new( zanataPluginVersion=>$zanataPluginVersion);
}

## Add options
while( my($conf, $value)= each(%confH)){
    $pomXml->add_configuration_overridable($conf, content=>$value);
}

## Add srcDir and tranDir if isAutoAddDir=1
if ($isAutoAddDir){
    $pomXml->add_configuration_overridable('srcDir', content=>$srcDir);
    $pomXml->add_configuration_overridable('transDir', content=>$transDir);
}

## Add config files
$pomXml->add_configuration_overridable('userConfig', content=>'${HOME}/.config/zanata.ini');
$pomXml->add_configuration_overridable('projectConfig', content=>'zanata.xml');

## Write the final output to pom.xml
#$pomXml->to_xml()->print;
$pomXml->to_xml($pomXml_file);

