#!/usr/bin/env raku

use Shelve6::Logging;
use Shelve6::Config;
use Shelve6::Server;
use Shelve6::Store;
use Shelve6::Repository;

my $log = Shelve6::Logging.new('main');

sub MAIN(Str :$config-file = 'config.yaml') {
    $log.info("Shelve6 starting...");
    $log.debug("Reading configuration from $config-file...");
    # XXX validate top level keys and that the top is a hash, and that repositories
    # is a list
    my %config = load-config($config-file);

    my $store = Shelve6::Store.new(|%config<store>);
    my $server = Shelve6::Server.new(|%config<server>);
    my @repositories;
    for @(%config<repositories>) -> $repo {
        my $repo-object = Shelve6::Repository.new(:$server, :$store, |%($repo));
        push @repositories, $repo-object;
    }

    $log.debug("Starting components...");
    $store.start;
    for @repositories { .start }
    $server.start;

    $log.info("Application initialization complete!");

    signal(SIGINT).tap: {
        $log.debug("Shutting down components...");
        $server.stop;
        for @repositories { .stop }
        $store.stop;
        $log.info("Clean shutdown, see you later!");
        exit;
    }

    sleep;
}
