#! perl -lw
# parametrized version of process-dryrun2 for better use in scripts.
die unless defined($outdir=$ARGV[0]);
open S1,">$outdir/singles.list" or die;
open S2,">$outdir/multiples.list" or die;
while(defined($fn=<STDIN>)){
    chomp$fn;
    die unless $fn=~/dry-(.*)\.out$/;
    $p=$1;
    open FI,$fn or die;
    while(<FI>){
        chomp;
        #Warning the package list is old.
        #Run 'cabal update' to get the latest list
        last if /Resolving dependencies\.\.\./;
    }
    unless($_ eq "Resolving dependencies..."){
        print STDERR "weird 1st line $fn";
        close FI;
        next;
    }
    $good=1;
    $todo=0;
    undef$pversion;
    undef@deps;
    while(<FI>){
        chomp;
        $good=0 if /Could not resolve dependencies/;
        $good=0 if /\(latest:/;
        $good=0 if /internal error: could not construct a valid install plan/; # edentv-4.10.0, lts-7.18
        if(/^In order, the following would be installed/){
            $todo=1;
            next;
        } else {push@deps,$_}
        if(/All the requested packages are already installed/){
            print STDERR "duplicate $fn";
            $good=0;
        }
        if($todo){
            $good=0 if /\(/;
            $pversion=$_;
        }
    }
    close FI;
    next unless$good;
    die "not todo $fn" unless $todo;
    if (@deps == 0){
        die "no lines $fn";
        next;
    } else {
        $a=pop@deps;
        die unless defined($pversion);
        die unless $a eq $pversion;
        $a =~ /(.+)-[0-9.]+$/;
        die unless $1 eq $p;
        if (@deps == 0){
            print S1 $pversion;
        } else{
            print S2 $pversion;
        }}
}

