#!/usr/bin/env perl

$tagsets = "tagsets";

$version = "";
$build = 0;
my %force;   # Names of module to force build
@tARGV = ();
for (; @ARGV > 0; ) {
    if ($ARGV[0] eq "-version") {
	$version = $ARGV[1];
        shift; shift; next;
    }
    if ($ARGV[0] eq "-build") {
	$build = 1;
        shift; next;
    }
    if ($ARGV[0] eq "-force") {
	$force{$ARGV[1]} = 1;
	shift; shift; next;
    }
    if ($ARGV[0] eq "-h")     { &usage (); }
    if ($ARGV[0] eq "help")   { &usage (); }
    if ($ARGV[0] eq "-help")  { &usage (); }
    if ($ARGV[0] eq "--help") { &usage (); }
    if ($ARGV[0] eq "-list")  { &list_distributions(); }
    @tARGV = (@tARGV, $ARGV[0]);
    shift;
}
@ARGV = @tARGV;
if ( @ARGV > 1) { &usage(); }

if ( @ARGV == 0) { 
    @list = <$tagsets/*.perl>;
    $file = $list[-1];
} else {
    $file = "$tagsets/$ARGV[0].perl";
}
print "examining perl modules based on $file\n";

# load the perl module list
open (FILE, $file) || die "ERROR: can't open perl module list: $file\n";
@list = <FILE>;
close (FILE);

# set the psconfig version:
if ("$version" eq "") {
    $version = $ENV{'PSVERSION'};
}
if ("$version" eq "") {
    $version = "default";
}


$prefix  = `csh -f psconfig.csh --prefix $version`; chomp $prefix;
$perldir = `csh -f psconfig.csh --perldir $version`; chomp $perldir;
$homedir = `pwd`; chomp $homedir;

print "psconfig version: $version\n";
print "prefix: $prefix\n";
print "perldir: $perldir\n";
print "PERL5LIB: $ENV{'PERL5LIB'}\n";
$psconfdir = $ENV{'PSCONFDIR'};

if ($build && ! -d $psconfdir) {
    mkdir $psconfdir || die "unable to create psconfig dir $psconfdir";
}

$Nmissing = 0;
@missing = ();
foreach $line (@list) {
    chop $line;
    if ($line =~ m|^\s*$|) { next; }
    if ($line =~ m|^\s*\#|) { next; }

    ($N, $module, $tarball, $modver, $prompts) = split (" ", $line);

    if ($modver eq "") { $modver = 0; } 
    system ("pscheckmods $module $modver");
    if ($? == 0) { 
	# print "$module: found\n";
	next; 
    }

    print "$module: missing\n";
    unless ($build or defined $force{$module} or defined $force{'all'}) { 
	$Nmissing ++;
	push @missing, $module;
	next; 
    }

    # try to build the module from ../../extperl/Module.*.tar.gz
    chdir "../../extperl" or die "Unable to find ../../extperl directory.";

    print "extract $module from $tarball\n";
    vsystem ("tar xvzf $tarball");
    
    ($tardir) = $tarball =~ m|(\S*).tar.gz|;
    print "tardir: $tardir\n";

    chdir $tardir;

    # build the MakeMaker makefile, setting the output directories
    if ($prompts) {
	@answers = split (",", $prompts);
	open (PIPE, "|perl Makefile.PL PREFIX=$prefix LIB=$perldir");
	foreach $answer (@answers) {
	    print PIPE "$answer\n";
	}
	close (PIPE);
    } else {
	vsystem ("perl Makefile.PL PREFIX=$prefix LIB=$perldir");
    }
    
    vsystem ("make < /dev/null");
    vsystem ("make install");

    chdir $homedir;
}
if (!$build) {
    if ($Nmissing > 0) {
	print "The following $Nmissing perl modules are missing from your system\n";
	foreach $name (@missing) {
	    print "  $name\n";
	}
	print "you may install them in your local path by re-running pscheckperl with -build\n";
    } else {
	print "no perl modules are missing from your system\n";
    }
}
exit 0;

sub usage {
    print STDERR "USAGE: pscheckperl [-version] [-build]\n";
    exit 2;
}

sub vsystem {
    print STDERR "@_\n";
    $status = system ("@_");
    $status;
}

sub list_distributions {
    @list = <$tagsets/*.perl>;
    foreach $line (@list) {
	chomp $line;
	($dist) = $line =~ m|$tagsets/(\S*).perl|;
	print STDERR "$dist\n";
    }
    exit 2;
}
