#	Perl5 Binary Distribution setup
#	patches Config.pm

# usage BASE [yes]
# BASE is base directory default is .
# yes means confirmed

print "Perl5 Binary Distribution setup\n";
print "Perl version is $]\n";

$VERSION = $];
$VERSION = "5.6.0" if ($VERSION eq "5.006");
$VERSION = "5.6.1" if ($VERSION eq "5.006001");

if ($VERSION eq "5.00503") {
    $V2 = "5.005";
}
elsif ($VERSION eq "5.6.0") {
    $V2 = $VERSION;
}
elsif ($VERSION eq "5.6.1") {
    $V2 = $VERSION;
}else {
    $V2 = $VERSION;
    die "Untested version $VERSION\n";
}

my %opt = ();
my $cwd = `pwd`; chop $cwd;


if (@ARGV) { $BASE = shift @ARGV; } else { $BASE = $cwd; }
$yes = shift @ARGV;

#linux
#installprivlib='/usr/lib/perl5/5.00503'
#installarchlib='/usr/lib/perl5/5.00503/i686-linux'

#installsitelib='/usr/lib/perl5/site_perl/5.005'
#installsitearch='/usr/lib/perl5/site_perl/5.005/i686-linux'
#installman1dir='/usr/man/man1'
#installman3dir='/usr/lib/perl5/5.00503/man/man3'

print "This will configure Perl5 for base = $BASE\n";
if (!$yes) {
	yes_or_die();
}


# find config.pm file
$flist = `find . -name Config.pm`;
$flist = join(" ", split("\n", $flist));
# /dev/null is a hack to deal with problem on libc5 systems	
@files = `grep -l archname= /dev/null $flist`;
if (@files > 1) {
 ($file) = grep { /$^O\// } @files;
} else {
  $file = $files[0];
}
chop $file if $file =~ /\n$/s;

if (! -f $file) {
    print "Please run $0 from the top level directory of the Perl5 binary distribution.\n";
    exit;
}

$config = ReadFile($file);
$old_config = $config;

($ARCHITECTURE) = $config =~ m/^archname='(.*?)'$/m;
print "ARCHITECTURE = $ARCHITECTURE\n";

%changelist = ( 
 'installbin' => "$BASE/bin",
 'installscript' => "$BASE/bin",
 'bin' => "$BASE/bin",
 'binexp' => "$BASE/bin",
 'scriptdir' => "$BASE/bin",
 'scriptdirexp' => "$BASE/bin",

 'installprivlib' => "$BASE/lib/$VERSION",
 'privlib' => "$BASE/lib/$VERSION",
 'privlibexp' => "$BASE/lib/$VERSION",

 'installarchlib' => "$BASE/lib/$VERSION/$ARCHITECTURE",
 'archlib' => "$BASE/lib/$VERSION/$ARCHITECTURE",
 'archlibexp' => "$BASE/lib/$VERSION/$ARCHITECTURE",

 'installsitelib' => "$BASE/lib/site_perl/$V2",
 'sitelib' => "$BASE/lib/site_perl/$V2",
 'sitelibexp' => "$BASE/lib/site_perl/$V2",

 'installsitearch' => "$BASE/lib/site_perl/$V2/$ARCHITECTURE",
 'sitearch' => "$BASE/lib/site_perl/$V2/$ARCHITECTURE",
 'sitearchexp' => "$BASE/lib/site_perl/$V2/$ARCHITECTURE",

 'perlpath' => "$BASE/bin/perl",
 
 'installman1dir' => "$BASE/man/man1",
 'installman3dir' => "$BASE/man/man3" );


foreach $dir (keys %changelist) {
	#print "$dir='$changelist{$dir}'\n";
	$config =~ s/\n$dir=[^\n]+/\n$dir='$changelist{$dir}'/gs;
}


if ($config ne $old_config) {
	WriteFile($file, $config);
	print "Updated $file\n";
	print "Setup complete\n";
} else {
	print "No changes required\n";
	print "Setup complete\n";
}


#print "\nPlease add $BASE/bin to your PATH\n";


sub yes_or_die {
    my $m = shift || "Proceed?";
    print "$m [y] ";
    die "Bailing out\n" if scalar(<STDIN>) !~ /^\s*(y|$)/i;
}


sub ReadFile   # (filename)
{
    # read file in ascii mode
    my ($data);
    open (IN, "$_[0]") || die "Error: Can't open $_[0]\n";
    read (IN, $data, (-s $_[0]));
    close IN;
    return $data;
}


sub WriteFile   # (filename , data, time_stamp_file)
{
    # save file in ascii mode
    my ($mode);
    ($mode) = (stat($_[2]))[2] if (@_ >= 3);
    chmod(0666, $_[0]) if -e $_[0];
    my ($a, $m);
    ($a, $m) = (stat($_[2]))[8,9] if (@_ >= 3);
    open (OUT, ">$_[0]") || die "Error: Can't write $_[0]\n";
    print OUT $_[1];
    close OUT;
    utime ($a, $m, $_[0]) if (@_ >= 3);
    chmod($mode, $_[0]) if $mode;
}

