#
# Module::Build build script for Wookee.
#
# (C) 2005-2007 Julian Mehnle <julian@mehnle.net>
# $Id$
#
##############################################################################

use Module::Build 0.26;

my $class = Module::Build->subclass( code => <<'EOF' );

    use lib 'lib';
    use Wookee;
    
    sub process_extra_files {
        my ($self, $dir) = @_;
        $dir ||= $element;
        File::Find::find(
            {
                wanted      => sub {
                    $File::Find::prune = 1 if -d and /\.svn$/;  # Exclude .svn/ dirs.
                    return if not -f;                           # Handle files only.

                    my $destination = $self->copy_if_modified(
                        from    => $File::Find::name,
                        to      => File::Spec->catfile($self->blib, $File::Find::name)
                    );
                    return if not defined($destination);        # Already up to date?

                    chmod((stat($File::Find::name))[2], $destination)
                        or warn("Couldn't set permissions on $destination: $!");
                },
                no_chdir    => 1
            },
            $dir
        );
    }

    sub process_etc_files  { shift->process_extra_files('etc')  }
    sub process_sbin_files { shift->process_extra_files('sbin') }
    sub process_res_files  { shift->process_extra_files('res')  }
    
    sub process_doc_files {
        my ($self) = @_;
        
        $self->process_extra_files('doc');
        
        my $markup_wookee_filename  = 'doc/markup.wookee';
        my $markup_html_filename    = 'blib/doc/markup.html';
        
        if (not -s $markup_html_filename) {
            # markup.html has not yet been created (or is empty), so create it.
            print("Building $markup_html_filename...\n");
            print("$markup_wookee_filename -> $markup_html_filename\n");
            
            my $markup_wookee_file  = IO::File->new($markup_wookee_filename);
            my $markup_wookee       = join('', $markup_wookee_file->getlines());
            my $markup_html         = BlockWiki->new()->parseBlock($markup_wookee);
            my $markup_html_file    = IO::File->new($markup_html_filename, '>');
            $markup_html_file->print(
                "<html>\n" .
                "   <head><title>Wookee Markup</title></head>\n" .
                "   <body>$markup_html</body>\n" .
                "</html>\n"
            );
        }
    }

EOF

my $build = $class->new(
    module_name     => 'Wookee',
    dist_author     => [
        'Michael Buschbeck <michael@buschbeck.net>',
        'Julian Mehnle <julian@mehnle.net>'
    ],
    license         => 'perl',
    requires        => {
        # Core requirements:
        perl                => '5.006',
        IO::Handle          => 0,
        IO::Dir             => 0,
        version             => 0
    },
    recommends      => {
        # Wookee module requirements:
        Error               => 0,
        Apache::Auth::UserDB
                            => '0.11',
        Locale::Maketext    => 0,
        Safe                => 0
    },
    build_requires  => {
        # Build requirements:
        perl                => '5.008',
        Module::Build       => '0.26',
        version             => 0,
        Test::Simple        => 0
    },
    install_path    => {
        etc             => '/etc/wookee',
        sbin            => '/usr/sbin',
        doc             => '/usr/share/doc/libwookee-perl',
        res             => '/usr/share/libwookee-perl'
    },
    create_makefile_pl
                    => 'passthrough',
    sign            => 1
);

$build->add_build_element($_)
    foreach qw(etc sbin doc res);

$build->create_build_script();

