
use Cwd qw(abs_path);
use File::Basename;
use File::Find;
use ExtUtils::MakeMaker;

my $dirname = abs_path( dirname(__FILE__) );
my @path = split( /\//, $dirname );
pop(@path);
pop(@path);
my $wc_dir = join( "/", @path );

foreach my $manifest_pm ( glob("lib/Zymonic/Manifest/*.pm") )
{
    open( FILE, "+<", $manifest_pm );
    my @lines = <FILE>;
    seek FILE, 0, 0;
    foreach my $line (@lines)
    {
        $line =~ s/Working_copy\s*=\s*'.*'/Working_copy = '$wc_dir'/g;
        print FILE $line;
    }
    close FILE;
}

# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
    NAME         => 'Zymonic',
    VERSION_FROM => 'lib/Zymonic/Base.pm',                                        # finds \$VERSION
    AUTHOR       => 'Alex Masidlover et al. (alex.masidlover@zednax.com)',
    ABSTRACT     => 'Zymonic Workflow System',
    EXE_FILES    => [qw(bin/zymonic_toolkit.pl bin/zymonic_toolkit_curses.pl bin/zymonic_daemon.pl)],
    PREREQ_PM    => {
        'CGI'                              => 4.08,                               # License as Perl
        'Test::Taint'                      => 0,                                  # License as Perl
        'Test::More'                       => 0,                                  # License as Perl
        'Test::Deep'                       => 0,                                  # License as Perl
        'XML::Simple'                      => 0,                                  # License as Perl
        'Data::Compare'                    => 0,                                  # License as Perl
        'Data::Dumper'                     => 0,                                  # License as Perl
        'Data::Structure::Util'            => 0,                                  # License as Perl
        'DBI'                              => 1.19,                               # License as Perl
        'Unicode::UCD'                     => 0,                                  # License as Perl
        'Unix::Syslog'                     => 0,                                  # License as Perl
        'Net::LDAP'                        => 0,                                  # License as Perl
        'Net::LDAP::Schema'                => 0,                                  # License as Perl
        'POSIX'                            => 0,                                  # License as Perl
        'MIME::Lite'                       => 0,                                  # License as Perl
        'Exception::Class'                 => 0,                                  # License as Perl
        'File::Find'                       => 0,                                  # License as Perl
        'Cwd'                              => 0,                                  # License as Perl
        'File::Basename'                   => 0,                                  # License as Perl
        'Clone'                            => 0,                                  # License as Perl
        'File::Touch'                      => 0,                                  # License as Perl
        'URI::Escape'                      => 0,                                  # License as Perl
        'Business::CreditCard'             => 0,                                  # License as Perl
        'HTML::Entities'                   => 0,                                  # License as Perl
        'Date::Calc'                       => 0,                                  # License as Perl
        'Date::Calc::XS'                   => 0,                                  # License as Perl
        'Date::Business'                   => 0,                                  # License as Perl
        'Locale::Currency::Format'         => 0,                                  # License as Perl
        'DateTime::Format::Strptime'       => 0,                                  # License as Perl
        'DateTime'                         => 0,                                  # License as Perl
        'Date::Parse'                      => 0,                                  # License as Perl
        'Encode'                           => 0,                                  # License as Perl
        'Time::Piece'                      => 0,                                  # License as Perl
        'Text::CSV::Encoded'               => 0,                                  # License as Perl
        'POSIX::strptime'                  => 0,                                  # License as Perl
        'GraphViz'                         => 0,                                  # License as Perl
        'IPC::ShareLite'                   => 0,                                  # License as Perl
        'Storable'                         => 0,                                  # License as Perl
        'Scalar::Util'                     => 0,                                  # License as Perl
        'Locale::Currency'                 => 0,                                  # License as Perl
        'Locale::Country'                  => 0,                                  # License as Perl
        'Time::Local'                      => 0,                                  # License as Perl
        'Mail::Sendmail'                   => 0,                                  # License as Perl
        'Crypt::OpenSSL::AES'              => 0,                                  # License as Perl
        'File::Copy'                       => 0,                                  # License as Perl
        'File::Basename'                   => 0,                                  # License as Perl
        'File::Path'                       => 0,                                  # License as Perl
        'File::Which'                      => 0,                                  # License as Perl
        'Expect'                           => 0,                                  # License as Perl
        'Algorithm::FloodControl'          => 0,                                  # License as Perl
        'IO::Socket'                       => 0,                                  # License as Perl
        'IO::Socket::SSL'                  => 1.94,                               # License as Perl
        'Taint::Util'                      => 0,                                  # License as Perl
        'XML::XPath'                       => 0,                                  # License as Perl
        'File::Slurp'                      => 0,                                  # License as Perl
        'Digest::SHA'                      => 0,                                  # License as Perl
        'Date::Manip::TZ'                  => 0,                                  # License as Perl
        'XML::LibXML'                      => 0,                                  # License as Perl
        'PPI'                              => 0,                                  # License as Perl
        'HTML::Scrubber'                   => 0,                                  # License as Perl
        'List::MoreUtils'                  => 0,                                  # License as Perl
        'Sys::Hostname'                    => 0,                                  # License as Perl
        'Config::Simple'                   => 0,                                  # License as Perl
        'APR::Status'                      => 0,                                  # Apache software license
        'Image::Magick'                    => 0,                                  # Apache 2.0 software license
        'Data::UUID'                       => 0,                                  # License as Perl
        'Pod::Snippets'                    => 0,                                  # License as Perl
        'Math::Round'                      => 0,                                  # License as Perl
        'Algorithm::Diff'                  => 0,                                  # License as Perl
        'HTML::FormatText::WithLinks'      => 0,                                  # License as Perl
        'Net::Ping'                        => 0,                                  # License as Perl
        'URI'                              => 0,                                  # License as Perl
        'JSON'                             => 0,                                  # License as Perl
        'File::Find::Rule'                 => 0,                                  # License as Perl
        'Date::Manip'                      => 0,                                  # License as Perl
        'Apache::ConfigParser'             => 0,                                  # License as Perl
        'Sys::Info::OS'                    => 0,                                  # License as Perl
        'Module::Reload'                   => 0,                                  # License as Perl
        'Business::Barcode::EAN13'         => 0,                                  # License as Perl
        'Module::Pluggable::Object'        => 0,                                  # License as Perl
        'HTML::TreeBuilder'                => 0,                                  # License as Perl
        'String::Random'                   => 0,                                  # License as Perl
        'Algorithm::CheckDigits'           => 0,                                  # Licesnse as Perl
        'Net::OAuth2::AuthorizationServer' => 0,                                  # License as Perl
        'DBD::SQLite'                      => 0,                                  # License as Perl
        'HTML::Strip'                      => 0,                                  # License as Perl
        'Data::ICal'                       => 0,                                  # License as Perl
        'Date::ICal'                       => 0,                                  # License as Perl
        'Proc::ProcessTable'               => 0,                                  # License as Perl
    },
);

# callback to do the tidying.
sub do_version
{
    my $file = $File::Find::name;

    return unless $file =~ /\.pm$/;

    open( FILE, "+<" . $file );

    my @lines = <FILE>;

    seek FILE, 0, 0;

    foreach my $line (@lines)
    {
        $line =~ s/VERSION\s*=\s*'.*'/VERSION = '$main::version'/g;
        print FILE $line;
    }
    close FILE;
}
