#!/usr/bin/perl

# These are the patches to apply.
# Paths in patch files are relative to the root of the CPAN project
my (@patchesToApply) = (

    # Address a bogus warning we see in SnowLeopard inside Class::Std (6783174)
    './PatchFiles/Class-Std-0.011/lib/Class/Std.pm.diff',

    # <rdar://problem/9559834> CPANInternal-103 (and earlier?) no longer builds
    # Disables all HTTP-Proxy tests which seem to depend on external web servers.
    './PatchFiles/HTTP-Proxy-0.25/t/Utils.pm.diff',

    # IPC::LDT changes - replace use of fields / pseudohash with Perl 5.10
    # compatible hash; add can_read method as implemented in Evolution's site_perl_custom.
    './PatchFiles/IPC-LDT-2.03/LDT.pm.diff',
    './PatchFiles/IPC-LDT-2.03/t/data.t.diff',
    './PatchFiles/IPC-LDT-2.03/t/mixed.t.diff',

    # Eliminate dependency on the JSON module
    './PatchFiles/JSON-RPC-0.96/META.yml.diff',
    './PatchFiles/JSON-RPC-0.96/Makefile.PL.diff',
    './PatchFiles/JSON-RPC-0.96/lib/JSON/RPC/Client.pm.diff',
    # Make JSON::RPC happy with CumulusDB. It shouldn't pass in $self needlessly.
    './PatchFiles/JSON-RPC-0.96/lib/JSON/RPC/Server.pm.diff',
    './PatchFiles/JSON-RPC-0.96/t/02_server.t.diff',

    # Added krb5_set_default_realm function to Authen::Krb5.xs
    # <rdar://problem/8370128> CPANInternal-94 project fails to build with LLVM compiler 2.0.
    './PatchFiles/Authen-Krb5.diff',
    './PatchFiles/Krb5-1.9/Makefile.PL.diff',

    # Fix for https://github.com/mschilli/log4perl/issues/27
    './PatchFiles/Log-Log4perl-1.44/lib/Log/Log4perl/Appender/File.pm.diff',

    # The First Rule of Sys::Syslog is: You do not call "setlogsock". 
    # The Second Rule of Sys::Syslog is: You do not call "setlogsock". 
    # In the chroot environment, there is no unix domain socket for syslog
    # without launchd plist hackery and other build-time implications.  
    # Don't call setlogsock to force the use of /var/run/syslog if it doesn't exist.
    './PatchFiles/Net-Daemon-0.48/lib/Net/Daemon/Log.pm.diff',

    # Eliminate dependency on the JSON module
    './PatchFiles/Pod-ProjectDocs-0.40/META.yml.diff',
    './PatchFiles/Pod-ProjectDocs-0.40/Makefile.PL.diff',
    './PatchFiles/Pod-ProjectDocs-0.40/lib/Pod/ProjectDocs.pm.diff'
);


foreach my $patchFile (@patchesToApply) {
    &applyPatch($patchFile);
}
exit(0);

# Subroutine that applies the patches
sub applyPatch
{
    my ($patchFile) = @_;
    
    my ($status) = system("/usr/bin/patch -p0 < \"$patchFile\"");
    if ($status != 0) {
        print "Unable to apply $patchFile\n";
        exit($status >> 8);
    }
}
