#!perl -w

###############################################################################
##                                                                           ##
##    Copyright (c) 1995 - 2009 by Steffen Beyer.                            ##
##    All rights reserved.                                                   ##
##                                                                           ##
##    This package is free software; you can redistribute it                 ##
##    and/or modify it under the same terms as Perl itself.                  ##
##                                                                           ##
###############################################################################

use strict;
use Config;
use File::Spec;
use File::Copy;
use ExtUtils::MakeMaker;

my($cc,$exe,$delim,$found,$lib,$answer,$default,$file,$orig,$patchlevel,$PATCHLEVEL,$SUBVERSION);

my(@C_XS) = qw(Calc.pm Calc.xs DateCalc.c DateCalc.h ToolBox.h typemap);
my(@Perl) = qw(Calc.pm);

my %Parameters =
(
    'NAME'              => 'Date::Calc',
    'VERSION_FROM'      => 'Calc.pm',
    'PREREQ_PM'         =>
                              {
                                  'Carp::Clan'  => 5.3,
                                  'Bit::Vector' => 7.1
                              },
#   ($] >= 5.005 ?
#       ('ABSTRACT'     => 'Gregorian calendar date calculations',
#        'AUTHOR'       => 'Steffen Beyer <STBEY@cpan.org>') : ()),
#   ($] >= 5.005 && $^O eq 'MSWin32' && $Config{archname} =~ /-object\b/i ?
#       ('CAPI'         => 'TRUE') : ()),
    'dist'              => { COMPRESS => "gzip -9", SUFFIX => "gz" },
    'clean'             => { FILES => join(' ', @C_XS) }
);

($cc,$exe,$delim) = @Config::Config{'cc','_exe','path_sep'};

$cc =~ s/\s+-.+$//; #remove possible trailing options

$found = 0;
if ($cc =~ m|/:\[|)
{
    $found = -f "$cc$exe";
}
elsif ($cc =~ m|/|)
{
    $found = -f "$cc$exe" || -l $cc;
}
else
{
    foreach $lib (split($delim, $ENV{'PATH'}))
    {
    	$found = -f File::Spec->catfile($lib,"$cc$exe") and last;
    }
}

print <<"VERBATIM";

Config.pm indicates that your version of Perl was built with
this C compiler:

    $cc$exe

VERBATIM

if ($found)
{
    print <<"VERBATIM";
I have located this compiler on your system.

You now have the option to either install the (fast) C/XS implementation of
this module (recommended), or a (somewhat slower) pure-Perl implementation.

VERBATIM
}
else
{
    print <<"VERBATIM";
I cannot locate this compiler on your system.

Therefore you now have the option to install a (somewhat slower) pure-Perl
implementation of this module instead of the (fast) C/XS implementation.

If however the aforementioned C compiler really is installed on your system,
please make sure it can be found in the PATH and then try running this program
again. Or if you think I should have found this compiler, and if you are sure
it is available and functional, simply answer 'c' to the next question.

VERBATIM
}

$answer = '';
$default = $found ? 'c' : 'p';
while (1)
{
    $answer = prompt(q{Install C/XS version (answer 'c') or pure-Perl version (answer 'p')?}, $default); 
    last if $answer =~ /^[CcPp]$/;
}

foreach $file (@C_XS)
{
    if (-e $file) { unlink($file) or die "Can't unlink file <$file>: $!\n"; }
}

if ($answer =~ /^[Cc]$/)
{
    foreach $file (@C_XS)
    {
        $orig = File::Spec->catfile('src','C_XS',$file);
        copy($orig,$file) or die "Can't copy file <$orig> to <$file>: $!\n";
    }
    $Parameters{'OBJECT'} = '$(O_FILES)';
    WriteMakefile(%Parameters);
    
    $patchlevel = $0;
    $patchlevel =~ s![^/\\]*$!patchlevel.h!;
    
    $PATCHLEVEL = $Config{'PATCHLEVEL'} || $Config{'patchlevel'} || substr($],2,3);
    $SUBVERSION = $Config{'SUBVERSION'} || $Config{'subversion'} || substr($],5) || 0;
    
    if (open(PATCHLEVEL, ">$patchlevel"))
    {
        print "Writing $patchlevel for $^X ($])\n";
        printf PATCHLEVEL "#define PATCHLEVEL %d\n", $PATCHLEVEL;
        printf PATCHLEVEL "#define SUBVERSION %d\n", $SUBVERSION;
        close(PATCHLEVEL);
    }
    else
    {
        warn "Oops: Couldn't write file '$patchlevel': $!\n";
        warn "However, you might succeed in building this module anyway;\n";
        warn "Just try it!\n";
    }
}
else
{
    foreach $file (@Perl)
    {
        $orig = File::Spec->catfile('src','Perl',$file);
        copy($orig,$file) or die "Can't copy file <$orig> to <$file>: $!\n";
    }
    WriteMakefile(%Parameters);
}

__END__

