#! /usr/bin/perl
# Daemon to report progress (part of MJohn)
# Collects and sends attack progress to the server
# !!! Experimental: this all is subject to change very soon !!!
# Daemon should run to upload results. Start it and do not stop.

use strict;
use warnings;

# Configuration
# TODO: libconfig-simple-perl
use Config::Simple;
# Hash table of configured properties
my %C;
# TODO: do we need to reload config on sighup?
Config::Simple->import_from('~/.john/mjohn.conf', \%C);

chdir $C{store};

# We traverse our folders in the store.
while (1) {
    for (<$C{user}_*/>) {
        chdir $_;
        # Capture/calculate speed
        # TODO: is it all/enough?
        # TODO: should not we parse --status output right here?
        # TODO: dirty. Use other way to redirect output.
        # TODO: use john used for command, not just from config.
        system qq/"$C{john}" --status="$C{user}" 2> "$C{user}.status"/;
        # TODO: look into logs.
        # TODO: see diff on .pot file.
        # TODO: capture speed, eta, what else?
        # Commit progress info: .status, .pot, .rec, .log
        # TODO: real quoting.
        # TODO: are all that file in all our dirs? Think about it.
        # TODO: failed commands does not have pot/rec/log files.
        my $files = ' -- "' . join('" "', map { "$C{user}.$_" } qw/status pot rec log/) . '"';
        warn "adding $files";
        `git add $files`;
        `git commit -m auto2 -o $files`;
        # Some lyrics: here we go back to the store, in most cases we
        # could just "cd .." but if we previously went through symlink
        # then we could not go back through "..", though it seems that
        # there should not be any symlinks in the store.
        chdir $C{store};
    }
    # TODO: it could be worth to send smaller packs.
    `git push origin master:master`;
    # TODO: probably we should sleep delay minus time spent on work.
    sleep $C{daemon_delay};
}