#! perl -wl
open FI,$ARGV[0] or die;
#output of ghc-pkg list then cleaned: 05cleaned
while(<FI>){
    chomp;
    die "bad line a($_)" unless /^(\S+)-([0-9.]+)$/;
    $a{$1}=$2;
}

open FI,$ARGV[1] or die;
while(<FI>){
    chomp;
    die unless /^(\S+)-([0-9.]+)$/;
    #print STDERR "INFO adding b constraint $1 $2";
    $b{$1}=$2;
}
print " -- $0 : part 1 : built-in packages not superseded by stackage";
for(sort keys%a){
    next if defined($b{$_});
    print "constraint: any.$_ == $a{$_}";
}
print " -- $0 : part 2 : overlaps between builtins and stackage";
for(sort keys%a){
    next unless defined($b{$_});
    if($a{$_} eq $b{$_}) {
        print "constraint: any.$_ == $a{$_}";
    } else {
        #print "constraint: any.$_ == $a{$_} || == $b{$_}";

        # previously we did the above line, but this resulted in some
        # packages depending on the builtin package (14.21 and parsec)
        # and some depending on the stackage package, which induced
        # cabal hell.

        # things might go wrong with this override, as a long time ago
        # we (probably) also had it, but switched to this OR but did
        # not record why.

        print " --  note : overriding builtin $a{$_}";
        print STDERR "INFO overriding builtin $_ $a{$_} with stackage $b{$_}";
        print "constraint: any.$_ == $b{$_}";
    }
}
print " -- $0 : part 3 : stackage";
for(sort keys%b){
    next if defined($a{$_});
    print "constraint: any.$_ == $b{$_}";
}
