<?php

require_once dirname(__FILE__) . '/../../../../www/config.php';
if (!isset($argv[1])) {
    exit('You must start the script like this (# of articles) : php test-backfillcleansubject.php 20000' . "\n");
} else {
    $pdo = new nzedb\db\Settings();
    $nntp = new NNTP(['Settings' => $pdo]);
    if ($nntp->doConnect() !== true) {
        exit($pdo->log->error("Unable to connect to usenet."));
    }
    $backfill = new Backfill(['NNTP' => $nntp, 'Settings' => $pdo]);
    $groups = new Groups(['Settings' => $pdo]);
    $grouplist = $groups->getActive();
    foreach ($grouplist as $name) {
        dogroup($name["name"], $argv[1]);
    }
}
function dogroup($name, $articles)
{
    global $backfill, $pdo;
    $backfill->backfillAllGroups($name, $articles);
    echo $pdo->log->primaryOver("Type y and press enter to continue, n to quit.\n");
    if (trim(fgets(fopen("php://stdin", "r"))) == 'y') {
        return true;
    } else {
        exit($pdo->log->primary("Done"));
    }
}
        $nntp = new NNTP(['Settings' => $groups->pdo]);
        $binaries = new Binaries(['NNTP' => $nntp, 'Groups' => $groups, 'Settings' => $groups->pdo]);
        foreach ($grouplist as $group) {
            if ($nntp->doConnect() !== true) {
                exit;
            }
            dogroup($group, $binaries);
            $nntp->doQuit();
        }
    } else {
        $nntp = new NNTP();
        $binaries = new Binaries(['NNTP' => $nntp, 'Settings' => $nntp->pdo]);
        if ($nntp->doConnect() !== true) {
            exit;
        }
        dogroup($argv[1], $binaries);
        $nntp->doQuit();
    }
}
/**
 * @param array $group
 * @param Binaries $binaries
 *
 * @return bool
 */
function dogroup($group, $binaries)
{
    $binaries->updateGroup($group);
    echo "Press enter to continue, type n and press enter to quit.\n";
    $cmd = trim(fgets(fopen("php://stdin", "r")));
    if ($cmd == '') {