#!/usr/bin/env raku
use v6.d;
use Data::Dump::Tree;
# rakudo must be able to parse json, so it doesn't
# make sense to require a dependency to parse it
our sub from-json($text) { ::("Rakudo::Internals::JSON").from-json($text) }

sub MAIN() {
    my $dir = '../elucid8-sandpit'.IO.relative;
    my %config = EVALFILE("$dir/config/02-plugins.raku");
    my %meta = from-json("$dir/META6.json".IO.slurp);
    my @plugins = %config<plugins>.list;
    my %p-versions;
    my SetHash $zef-install .= new;
    for @plugins -> $p {
        $p ~~ / (.+?) ':ver<'  (.+)  '>' | (.+) $ /;
        my $p-base = ~$/[0];
        my $p-v = ($/[1] // '*').Str;
        %p-versions{$p-base} = '';
        require ::($p-base);
        CATCH {
            $zef-install{$p-base}++;
            %p-versions{$p-base} = 'not installed';
            next
        }
        unless %p-versions{$p-base} eq 'not installed' {
            my $v = ::($p-base).new.config<version>;
            %p-versions{$p-base} = $v;
            $zef-install{$p-base}++ if (($v !eq $p-v) and ($p-v !eq '*'))
        }
    }
    ddt %p-versions, :title<versions>;
    ddt $zef-install.keys, :title<install>;
    my %installables = %meta<provides>;
    my $cant-install = $zef-install.keys (-) %installables.keys;
    my $install = $zef-install.keys (-) $cant-install.keys;
    ddt $cant-install.keys, :title<Can't install>;
    ddt $install.keys, :title<Installing>;
    my $proc;
    temp $*CWD;
    &chdir($dir);
    $proc = run <<zef --dry -/test install .>>, :merge, :err, :out ;
    $proc.out.slurp.say
}
