#!/usr/bin/perl

=encoding UTF-8
=cut

=head1 DESCRIPTION

=cut

# common modules
use strict;
use warnings FATAL => 'all';
use feature 'say';
use utf8;
use open qw(:std :utf8);

use Carp;
use HTTP::Tiny;
use JSON::PP;

# global vars

# subs

# main
sub main {

    my $response = HTTP::Tiny->new()->get('http://127.0.0.1:29167/api/3/betas');

    die 'not 200' unless $response->{status} eq '200';

    my $betas = decode_json $response->{content};

    foreach my $beta (@$betas) {
        next if $beta->{status} ne 'occupied';

        my $port = $beta->{port};

        warn "## $port\n";
        my $result = `dp | grep $port`;

        if ($result eq '') {
            `rm -rf /local/creator/$port`;
            `rm -rf /local/creator/meta/$port.json`;
            warn "ok\n";
        } else {
            # TODO - сделать автоудаление докерных контейнеров
            warn "Can't delete beta, need to delete docker conatainers\n";
        }

    }


    say '#END';
}
main();
__END__
