#!/usr/bin/env perl

use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use Carp;

my $name     = undef;
my $prod_version  = '0.1';
my $edition = '0';
my $product  = 'Documentation';
my $brand    = undef;
my $man      = 0;
my $help     = 0;
my $lang     = undef;
my $tmpl_dir = undef;
my $type      = 'Book';
my @valid_types = ('Book', 'Set', 'Article');

GetOptions(
    'h|help|?'   => \$help,
    'man'        => \$man,
    'name=s'     => \$name,
    'version=s'  => \$prod_version,
    'edition=s'  => \$edition,
    'product=s'  => \$product,
    'brand=s'    => \$brand,
    'lang=s'     => \$lang,
    'type=s'     => \$type,
) or pod2usage(2);
pod2usage(1) if $help;
pod2usage( -verbose => 2 ) if $man;
pod2usage(1) if ( !$name || $type !~ m/^(Book|Set|Article)$/i);

$type = ucfirst($type);

$tmpl_dir = $type . '_Template';

my $bookname = $name;
$bookname =~ s/_/ /g;

croak("directory $name exists!") if (-e $name);

my $cmd = '';

if (! $brand) {
    $cmd  = "cp -rf /usr/share/publican/Templates/common-$tmpl_dir $name";
}
else {
    if(! -d "/usr/share/publican/Templates/$brand-$tmpl_dir") {
        croak("Brand $brand does not contain a $tmpl_dir!\nValid brands are:\n" . `ls /usr/share/publican/Common_Content`);
    }
    $cmd  = "cp -rf /usr/share/publican/Templates/$brand-$tmpl_dir $name";
}

system($cmd);
if ($@) {
    croak "can't copy $tmpl_dir: $!";
}

$cmd = qq|mv $name/en-US/$type.xml $name/en-US/$name.xml|;
system($cmd);
if ($@) {
    croak "can't rename $type.xml: $!";
}

$cmd = qq|mv $name/en-US/$type.ent $name/en-US/$name.ent|;
system($cmd);
if ($@) {
    croak "can't rename Book.ent: $!";
}

$cmd
    = qq|sed -i -e 's/<productnumber>.*<\\/productnumber>/<productnumber>$prod_version<\\/productnumber>/g' $name/en-US/| . $type . qq|_Info.xml|;
system($cmd);
if ($@) {
    croak "can't update version: $!";
}

$cmd
    = qq|sed -i -e 's/<edition>.*<\\/edition>/<edition>$edition<\\/edition>/g'  $name/en-US/| . $type . qq|_Info.xml|;
system($cmd);
if ($@) {
    croak "can't update edition: $!";
}

$cmd = qq|find $name  -type f \| xargs sed -i -e 's/MYBOOKNAME/$bookname/g'|;
system($cmd);
if ($@) {
    croak "can't update MYBOOKNAME: $!";
}

$cmd = qq|find $name  -type f \| xargs sed -i -e 's/MYBOOK/$name/g'|;
system($cmd);
if ($@) {
    croak "can't update MYBOOK: $!";
}


$cmd = qq|find $name -type f \| xargs sed -i -e 's/MYPRODUCTNAME/$product/g'|;
system($cmd);
if ($@) {
    croak "can't update MYPRODUCTNAME: $!";
}

$cmd = qq|find $name -type f \| xargs sed -i -e 's/MYPRODUCT/$product/g'|;
system($cmd);
if ($@) {
    croak "can't update MYPRODUCT: $!";
}
	
if ( $lang and $lang ne '' ) {
    $cmd = qq|sed -i -e 's/XML_LANG.*\$/XML_LANG\\t= $lang/g' $name/Makefile|;
    system($cmd);
    if ($@) {
        croak "can't update XML_LANG: $!";
    }
    $cmd = qq|mv $name/en-US $name/$lang|;
    system($cmd);
    if ($@) {
        croak "can't update XML_LANG: $!";
    }
}

if( $type eq 'Set') {
	warn("You must edit your new Makefile and set the BOOKS, SET_REPO and SET_REPO_TYPE parameters\n");
}

__END__

=head1 NAME

create_book

=head1 SYNOPSIS

create_book --name=<book_name> [options]

 Options:
   -help               Brief help message
   -man                Full documentation
   --name              Name (Required)
   --version           Product Version
   --edition           Edition
   --product           Product
   --brand             Select Brand
   --lang              Source Language
   --type              Book|Set|Article

=head1 OPTIONS
   -help               Brief help message
   -man                Full documentation
   --name              Name (Required)
   --version           Product Version
   --edition           Edition
   --product           Product
   --brand             Select Brand
   --lang              Source Language
   --type              Book|Set|Artcile

=head2 -help

Print a brief help message and exits.

=head2 -man

Prints the manual page and exits.

=head2 --name

Name of Book/Article

=head2 --version

Version of the Product this Book is about. Defaults to 0.1

=head2 --edition

Edition of this Book. Defaults to 0

=head2 --product

Product which contains this Book. Defaults to Documentation.

=head2 --brand

Use the specified brand for branding output.

=head2 --lang

Use the specified langauge as the source language.

=head2 --type

Type of book. Defaults to Book, can also be Set or Article.

=head1 DESCRIPTION

B<This program> creates a new Book or Article for use with the publican package.

=head1 LICENSE

    Copyright (C) 2008 Red Hat, Inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

=cut

