#!/usr/bin/perl
use strict;
use warnings;
use PAR::Dist;
use Getopt::Long;
use File::Spec;
use ExtUtils::MakeMaker;
use Config qw/%Config/;

my $verbose;
sub _diag ($) {
  print @_, "\n" if $verbose;
}

my $usage = <<HERE;
Usage: $0 [-v] -d BZDIR -f PARFILE
HERE

my $bzdir;
my $parfile;

GetOptions(
  'd|bugzilla-directory=s' => \$bzdir,
  'v|verbose' => \$verbose,
  'f|par-file=s' => \$parfile,
);

die $usage
  if not defined $bzdir
  or not -d $bzdir
  or not defined $parfile
  or not -f $parfile;

$bzdir = File::Spec->rel2abs($bzdir);
$parfile = File::Spec->rel2abs($parfile);

my $contains_binaries = PAR::Dist::contains_binaries($parfile);

my ($distname, $distversion,
    $distarch, $distperl     ) = PAR::Dist::parse_dist_name($parfile);

# check that we have a reasonable distribution name and version
if (not defined $distname or not defined $distversion) {
  my $text = <<HERE;
I could not infer distribution name or version from the name of the
source PAR file. I will set the name of the distribution to the name
of the source file and the version to 1.00.
If you don't understand this or don't care, just hit enter. (y/n)
HERE
  chomp $text;
  my $ok = ExtUtils::MakeMaker::prompt($text, "y");
  print "\n";
  die "Aborting!" unless $ok =~ /^\s*y(?:es)?\s*$/i;
  $distname = $parfile;
  $distname =~ s/\.par$//i;
  $distversion = '1.00';
}

# check that we're running under the correct architecture
my $explanation_binary = <<HERE;
The distribution seems to contain binary, compiled code.
Unless you know what you are doing, you should not install it.
HERE
my $explanation_nonbinary = <<HERE;
The distribution does not seem to contain binary, compiled code.
Most likely, it is portable and can be safely installed.
HERE
if (not defined $distarch
    or (not $distarch =~ /^any_?arch$/
        and not $distarch eq $Config{archname})) {
  my $default = $contains_binaries ? "n" : "y";
  my $explanation = $contains_binaries
                    ? $explanation_binary
                    : $explanation_nonbinary;
  my $text = <<HERE;
I'm not entirely sure the distribution you are about to install is
intended for your architecture.
${explanation}If this puzzles you, please contact the supplier of the PAR file
about it.
Do you want me to continue with the installation? (y/n)
HERE
  chomp $text;
  my $ok = ExtUtils::MakeMaker::prompt($text, $default);
  print "\n";
  die "Aborting!" unless $ok =~ /^\s*y(?:es)?\s*$/i;
}

# check that we're running under the correct perl 
if (not defined $distperl
    or (not $distperl =~ /^any_?version$/
        and not $distperl eq $Config{version})) {
  my $default = $contains_binaries ? "n" : "y";
  my $explanation = $contains_binaries
                    ? $explanation_binary
                    : $explanation_nonbinary;
  my $text = <<HERE;
I'm not entirely sure the distribution you are about to install is
intended for your version of perl.
${explanation}If this puzzles you, please contact the supplier of the PAR file
about it.
Do you want me to continue with the installation? (y/n)
HERE
  chomp $text;
  my $ok = ExtUtils::MakeMaker::prompt($text, $default);
  print "\n";
  die "Aborting!" unless $ok =~ /^\s*y(?:es)?\s*$/i;
}

# set the appropriate installation paths
my $extdir   = File::Spec->catdir($bzdir, 'external');
_diag "Installing into $extdir...";
my $libdir   = File::Spec->catdir($extdir, 'lib');
my $archdir  = File::Spec->catdir($extdir, 'arch');
my $bindir   = File::Spec->catdir($extdir, 'bin');
my $packlist = File::Spec->catfile($archdir, $distname, '.packlist');
my $man1dir  = File::Spec->catdir($extdir, 'man1');
my $man3dir  = File::Spec->catdir($extdir, 'man3');

for ($extdir, $libdir, $archdir, $bindir, $man1dir, $man3dir) {
  mkdir($_) if not -d $_;
}

# install
PAR::Dist::install_par(
  dist => $parfile,
  inst_lib => ($contains_binaries ? $archdir : $libdir),
  inst_archlib => $archdir,
  inst_bin => $bindir,
  inst_script => $bindir,
  inst_man1dir => $man1dir,
  inst_man3dir => $man3dir,
  packlist_read => $packlist,
  packlist_write => $packlist,
);



