use Module::Build;
# See perldoc Module::Build for details of how this works

my $build_pkg   = eval { require Apache::TestMB }
                ? 'Apache::TestMB'
                : 'Module::Build';

my $class = $build_pkg->subclass(code => <<'EOF');
use Cwd;

my $pkg_dir = "./nebclient";

sub ACTION_code {
    my $self = shift;

    $self->SUPER::ACTION_code(@_);

    my $old_pwd = getcwd();
    chdir $pkg_dir;

    unless (-e "configure") {
        system("./autogen.sh") == 0 or die "install failed: $?";
    }

    chdir $old_pwd;
}

sub ACTION_build {
    my $self = shift;

    $self->SUPER::ACTION_build(@_);

    my $old_pwd = getcwd();
    chdir $pkg_dir;

    unless (-e "Makefile") {
        system("sh ./configure") == 0 or die "build failed: $?";
    }

    system("make") == 0 or die "build failed: $?";

    chdir $old_pwd;
}

# Do not attempt to install nebclient.  It is bundled in this package for
# testing only.
#
# sub ACTION_install {
#     my $self = shift;
# 
#     $self->SUPER::ACTION_install(@_);
# 
#     my $old_pwd = getcwd();
#     chdir $pkg_dir;
# 
#     system("make install") == 0 or die "install failed: $?";
# 
#     chdir $old_pwd;
# }

sub ACTION_clean {
    my $self = shift;

    $self->SUPER::ACTION_clean(@_);

    my $old_pwd = getcwd();
    chdir $pkg_dir;

    system("make clean") == 0 or die "install failed: $?";

    chdir $old_pwd;
}
EOF

$class->new(
    module_name         => 'Nebulous',
    dist_version_from   => 'lib/Nebulous/Server.pm',
    author              => 'Joshua Hoblitt <jhoblitt@cpan.org>',
    license             => 'gpl',
    create_makefile_pl  => 'passthrough',
    requires            => {
        'Config::YAML'          => '1.42',
        'URI'                   => '1.30',
        'SOAP::Lite'            => '0.69',
        'Log::Log4perl'         => '0.48',
        'DBI'                   => '1.53',
        'DBD::mysql'            => '3.0007',
        'Class::Accessor::Fast' => 0,
        'Params::Validate'      => '0.73',
        'Net::Server::Daemonize'=> '0.05',
        'Sys::Statistics::Linux::DiskUsage' => '0.02',
        'File::ExtAttr'         => '1.03',
    },
    build_requires      => {
        'Apache2::SOAP'         => 0,
        'Apache::DBI'           => '1.05',
        'Apache::Test'          => '1.27',  # bundles Apache::TestMB
        'Apache::TestMB'        => 0,
        'File::Temp'            => 0,
        'Test::More'            => '0.49',
        'Test::URI'             => '1.06',
    },
    recommends          => {
        'Test::Distribution'    => '1.22',
    },
    script_files        => [qw(
        bin/neb-addvol
        bin/neb-cat
        bin/neb-cp
        bin/neb-df
        bin/neb-locate
        bin/neb-initdb
        bin/neb-ls
        bin/neb-mv
        bin/neb-rm
        bin/neb-touch
        bin/nebdiskd
    )],
)->create_build_script;
