#!/usr/bin/perl
#
# Write dependency list from Koha cpanfile, in Debian format.
#
# Copyright 2010  Catalyst IT, Ltd
#
# 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 3 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, see <http://www.gnu.org/licenses/>.

use Modern::Perl;
use Parallel::ForkManager;
use Sys::CPU;

use C4::Installer::PerlModules;

# These are packages that may not be in the apt archive in a way that
# apt-file can find, e.g. in the Koha repo rather than the regular
# debian one.
my %overrides = (
    'HTTP::OAI'            => 'libhttp-oai-perl (>= 3.2) | libhttp-oai-3.27-perl, libhttp-oai-perl (<< 4.0) | libhttp-oai-3.27-perl',
    'CGI::Compile'         => 'libcgi-compile-perl (>= 0.25) | libcgi-compile-perl (<< 0.24)',
    'DBD::SQLite'          => 'libdbd-sqlite3-perl',
    'Starman'              => 'starman',
);

# These are packages we're going to ignore
my %ignore = (
    'Data::Pagination'       => 1,
    'CHI'                    => 1,
    'CHI::Driver::Memcached' => 1,
);

my $prefix = "^/usr/((lib|share)/perl5|(lib|share)/perl/[0-9.]+|(lib|share)/.*-linux-gnu.*/perl/[0-9.]+|(lib|share)/.*-linux-gnu.*/perl5/[0-9.]+)";

my $pm   = Parallel::ForkManager->new( Sys::CPU::cpu_count() );

my $modules = C4::Installer::PerlModules->new();
my $prereqs = $modules->prereqs;
foreach my $phase ($prereqs->phases) {
    foreach my $type ($prereqs->types_in($phase)) {
        my $reqs = $prereqs->requirements_for($phase, $type);

        MODULE_LOOP:
        foreach my $module ( $reqs->required_modules ) {
            my $pid = $pm->start and next MODULE_LOOP;

            next if $ignore{$module};
            my $subpath = $module;
            $subpath =~ s,::,/,g;

            my $output = qx(apt-file -l -x search "$prefix/$subpath.pm\$");
            my @temp   = split( /\n/, $output );
            my @lines  = ();

            # Remove packages that are required/essential and always installed on
            # a Debian system. Debian packages should not have unversioned
            # dependencies on such packages.


            # skip perl-base and problematic version specific libperl* and
            # perl-module* packages (they get installed as deps. anyway)
            foreach my $line (@temp) {
                if ( $line ne "perl-base" and $line !~ /^libperl5\./ and $line !~ /^perl-modules-5\./ ) {
                    @lines = ( @lines, $line );
                }
            }

            if ( exists $overrides{$module} ) {
                print "$overrides{$module}\n";
            }
            elsif ( scalar(@lines) == 1 && $lines[0] ne "" ) {
                my $pkg = $lines[0];
                print "$pkg\n";
            }
            elsif ( scalar(@lines) > 1 ) {
                foreach my $pkg (@lines) {
                    print " | " if ( $pkg ne $lines[0] );
                    print "$pkg";
                }
                print "\n";
            }
            elsif ( scalar(@temp) != 0 ) {
                # hmm, skip module
            }
            elsif ( $type ne 'requires' ) {
                # Ignore because we don't have it and we don't care.
            }
            else {
                print "EEEK: unknown package for $module\n";
            }

            $pm->finish; # Terminates the child process
        }
    }
}
$pm->wait_all_children;
