#!c:/strawberry/perl/bin/perl.exe
#
# setup_spmp.pl - setup strawberry perl mod_perl
#
# This script ASSUMES that strawberry perl is installed in c:/strawberry
# (the default).  It looks for apache in the usual places, but you can
# override the location interactively

use strict ;
use warnings;
use ExtUtils::MakeMaker;
use Getopt::Std ;
use LWP::Simple;
use File::Copy;
use Config;
require Win32;
require File::Spec;


die "This only works for Win32" unless $^O =~ /Win32/i;

my $mploc = "http://downloads.aptest.com/sperl/mp/5.12_x86" ;

# archives to install using pip
my @pars = qw( libapreq2-2.12-MSWin32-x86-multi-thread-5.12.par
               mod_perl-2.0.4-MSWin32-x86-multi-thread-5.12.par );

# pieces to put in the modules directory
my @mods = qw( mod_perl.so mod_apreq2.so );

# dlls to put in the apache bin directory
my @dlls = qw( libapreq2.dll ) ;
               
use vars qw($opt_n) ;
 
my $options = "nv" ;

getopts($options) || usage() ;

# where is Apache?
my $modDir = "" ;
my $binDir = "" ;

my ($apache2, $apache, $apache22);
my @drives = drives();

# find a possible Apache2 directory
APACHE2: {
    for my $drive (@drives) {
        for my $p ('Apache2', 'Apache22', 'Program Files/Apache2', 
                   'Program Files/Apache Group/Apache2',
                   'Program Files/Apache Group/Apache2.2',
                   'Program Files/Apache Software Foundation/Apache2.2') {
            my $candidate = File::Spec->catpath($drive, $p);
            if (-d $candidate) {
                $apache2 = $candidate;
                last APACHE2;
            }
        }
    }
}

if ($apache2) {
    print "Found Apache  in $apache2\n";
    $apache2 =~ s/\\/\//g;
} 
my $ans = prompt(qq{Install mod_perl for "$apache2"?}, 'yes');
exit 0 if ($ans !~ /^y/i) ;

$modDir = $apache2."/modules";
$binDir = $apache2."/bin";

# okay - we know where to put the mod_perl files

foreach (@pars) {
    my $cmd = "pip $mploc/$_" ;
    print "Installing $_\n";
    qx($cmd) unless ($opt_n);
    if ($?) {
        print "There was an error: $?\n";
        exit 1;
    }
}

# download the apache modules

foreach (@mods) {
    my $cmd = "lwp-download $mploc/$_ $_";
    print "$cmd\nInstalling $_ into $modDir\n";
    qx($cmd) unless ($opt_n);
    if ($?) {
        print "There was an error: $?\n";
        exit 1;
    }
    copy "$_", "$modDir";
    unlink "$_";
}

# download the DLLs

foreach (@dlls) {
    my $cmd = "lwp-download $mploc/$_ $_";
    print "Installing $_ into $binDir\n";
    qx($cmd) unless ($opt_n);
    if ($?) {
        print "There was an error: $?\n";
        exit 1;
    }
    copy "$_",$binDir;
    unlink "$_";
}

sub usage {
    print "usage: setup_spmp.pl [-n -v]\n";
    exit 1;
}

sub fix_path {
    my $file = shift;
    $file = Win32::GetShortPathName($file);
    $file =~ s!\\!/!g;
    return $file;
}

sub drives {
    my @drives = ();
    eval{require Win32API::File;};
    return map {"$_:\\"} ('C' .. 'Z') if $@;
    my @r = Win32API::File::getLogicalDrives();
    return unless @r > 0;
    for (@r) {
        my $t = Win32API::File::GetDriveType($_);
        push @drives, $_ if ($t == 3 or $t == 4);
    }
    return @drives > 0 ? @drives : undef;
}
