#!/usr/bin/env perl6
use File::Mkdir;
use Panda;

subset Command of Str where
    'install' | 'list' | 'update' | 'info';

sub installprojects($panda, @args) {
    for @args -> $x {
        try {
            $panda.resolve($x);
            CATCH { say $! }
        };
    }
}

sub listprojects($panda) {
    for $panda.ecosystem.project-list -> $p {
        my $x = $panda.ecosystem.get-project($p);
        my $s;
        given $panda.ecosystem.project-get-state($x) {
            when 'installed'     { $s = '[installed]' }
            when 'installed-dep' { $s = '[installed as a dependency]' }
            default              { $s = '' }
        }
        printf "%-20s\t%s\n", $x.name, $s;
    }
}

sub projectinfo($panda, @args) {
    for @args -> $p {
        my $x = $panda.ecosystem.get-project($p);
        if $x {
            say $x.name => $x.version;
            say "Depends on:" => $x.dependencies.Str if $x.dependencies;
            given $panda.ecosystem.project-get-state($x) {
                when 'installed'     {
                    say "State:\t\tinstalled";
                }
                when 'installed-dep' {
                    say "State:\t\tinstalled as a dependency";
                }
            }
            for $x.metainfo.kv -> $k, $v {
                if $k ~~ none('version', 'name', 'depends') {
                    say $k.ucfirst => $v;
                }
            }
            say '';
        } else {
            say "Project '$p' not found"
        }
    }
}

sub execute(Command $c, $panda, @args) {
    given $c {
        when 'install' { 
            installprojects($panda, @args);
        }
        when 'list' {
            listprojects($panda);
        }
        when 'update' {
            $panda.ecosystem.update;
        }
        when 'info' {
            projectinfo($panda, @args);
        }
    }
}

# default opts for MAIN
if %*ENV<PANDA_DEFAULT_OPTS> {
    @*ARGS = %*ENV<PANDA_DEFAULT_OPTS> ~ (@*ARGS ?? ' ' ~ @*ARGS !! '');
}

# initialize the Panda object
my $panda;
{
    my $pandadir = %*ENV<HOME> ~ '/.panda';
    mkdir $pandadir, :p unless $pandadir.IO ~~ :d;

    my $projectsfile = "$pandadir/projects.json";
    unless $projectsfile.IO ~~ :f {
        run "wget http://feather.perl6.nl:3000/list -O $projectsfile";
    }

    $panda = Panda.new(
        srcdir       => "$pandadir/src",
        destdir      => %*ENV<HOME> ~ '/.perl6',
        statefile    => "$pandadir/state",
        projectsfile => "$pandadir/projects.json"
    );
}

multi MAIN (Command $command, *@args) {
    execute($command, $panda, @args);
}

multi MAIN () {
    while prompt('panda> ') -> $c {
        my ($command, @args) = $c.split(' ');
        if $command ~~ Command {
            try { execute($command, $panda, @args) };
            say $! if $!;
        } else {
            say "Unknown command: $command";
        }
    }
    say ''; # the newline after exiting the REPL
}

sub USAGE {
    say "Usage: panda [command [args]]

Options:
    help
        Display this message and quit
    install [module1 module2]
        Install a modules(s)
    list
        List the available packages
    info [module1 module2]
        Display information on a specific module(s)
    update
        Update the module database
";
    exit 0;
}

# vim: ft=perl6
