#!/usr/bin/perl

# Created on: 2008-03-25 16:07:36
# Create by:  ivanw
# $Id$
# $Revision$, $HeadURL$, $Date$
# $Revision$, $Source$, $Date$

use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use Data::Dumper qw/Dumper/;
use English qw/ -no_match_vars /;
use Path::Class;

our $VERSION = 0.001;
my ($name)   = $PROGRAM_NAME =~ m{^.*/(.*?)$}mxs;

my %option = (
    dir     => '',
    verbose => 0,
    man     => 0,
    help    => 0,
    VERSION => 0,
);

main();
exit 0;

sub main {

    Getopt::Long::Configure('bundling');
    GetOptions(
        \%option,
        'dir|d=s',
        'module|m=s',
        'force|f!',
        'verbose|v+',
        'man',
        'help',
        'VERSION!',
    ) or pod2usage(2);

    if ( $option{'VERSION'} ) {
        print "$name Version = $VERSION\n";
        exit 1;
    }
    elsif ( $option{'man'} ) {
        pod2usage( -verbose => 2 );
    }
    elsif ( $option{'help'} || !$option{dir} ) {
        pod2usage( -verbose => 1 );
    }

    # do stuff here
    my $data = '';
    my $dir = dir $option{dir};
    my @files = sort $dir->children;
    my @templates;
    while (my $file = shift @files) {
        if (-d $file) {
            push @files, sort $file->children;
        }
        else {
            push @templates, $file;
            my $template = $file->slurp;
            $template =~ s/^__/\0__/gxms;
            $template .= "\n" if $template !~ /\n\Z/xms;
            my $name = "__${file}__\n";
            $name =~ s{$dir/?}{};
            $data .= $name.$template;
        }
    }

    if ( $option{module} ) {
        my $file = file($option{module});
        if ( !-f $file ) {
            my $mod = $option{module};
            $mod =~ s{::}{/}gxms;
            $mod .= '.pm';
            $file = -f $mod ? file($mod) : file('lib', $mod);
            die "Could not find '$option{module}' to update!\n" if !-f $file;
        }
        my $content = $file->slurp;
        my ($head, $template) = split /^__DATA__$/xms, $content;

        if ( $head =~ /^=head2 \s+ Templates\r?\n.*?^=back$/xms ) {
            my $templates = join "\n\n=item *\n\n", @templates;
            $head =~ s/^=head2 \s+ Templates\r?\n.*?^=back$/=head2 Templates\n\n=over 4\n\n=item *\n\n$templates\n\n=back/xms;
        }

        my $fh = $file->openw;
        print {$fh} $head, "__DATA__\n", $data;
        close $fh;
    }
    else {
        print $data;
    }

    return;
}

__DATA__

=head1 NAME

dir2data - Takes a directory of templates and creates a perl file __DATA__
section compatible with Template::Provider::FromDATA

=head1 VERSION

This documentation refers to dir2data version 0.1.

=head1 SYNOPSIS

   dir2data [--verbose] --dir [dir] [--module lib/App/TemplateCMD/Templates.pm]

 OPTIONS:
  -d --dir=str       The directory containing templates
  -m --module=str    The module to wright to (just overwrights the __DATA__
                     section of the file)

  -v --verbose       Show more detailed option
     --version       Prints the version information
     --help          Prints this help information
     --man           Prints the full documentation for dir2data

=head1 DESCRIPTION

=head1 SUBROUTINES/METHODS

=head1 DIAGNOSTICS

=head1 CONFIGURATION AND ENVIRONMENT

=head1 DEPENDENCIES

=head1 INCOMPATIBILITIES

=head1 BUGS AND LIMITATIONS

There are no known bugs in this module.

Please report problems to Ivan Wills (ivanw@benon.com).

Patches are welcome.

=head1 AUTHOR

Ivan Wills - (ivanw@benon.com)

=head1 LICENSE AND COPYRIGHT

Copyright (c) 2011 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW, Australia 2077).
All rights reserved.

This module is free software; you can redistribute it and/or modify it under
the same terms as Perl itself. See L<perlartistic>.  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.

=cut
