function scan_repos()
{
    $unix = new unix();
    $rm = $unix->find_program("rm");
    $sock = new sockets();
    $APACHE_ACCOUNT = $unix->APACHE_SRC_ACCOUNT();
    $ArticaMetaStorage = $sock->GET_INFO("ArticaMetaStorage");
    if ($ArticaMetaStorage == null) {
        $ArticaMetaStorage = "/home/artica-meta";
    }
    @mkdir("{$ArticaMetaStorage}/nightlys", 0755, true);
    @mkdir("{$ArticaMetaStorage}/releases", 0755, true);
    @mkdir($GLOBALS["MAIN_PATH"], 0755, true);
    meta_events("Scanning {$ArticaMetaStorage}/releases");
    $unix = new unix();
    $files = $unix->DirFiles("{$ArticaMetaStorage}/releases");
    while (list($num, $ligne) = each($files)) {
        meta_events("Found {$ArticaMetaStorage}/releases/{$num}");
        $ARRAY["RELEASES"][$num] = scan_repos_split("{$ArticaMetaStorage}/releases/{$num}");
    }
    $files = $unix->DirFiles("{$ArticaMetaStorage}/nightlys");
    while (list($num, $ligne) = each($files)) {
        meta_events("Found {$ArticaMetaStorage}/nightlys/{$num}");
        $ARRAY["NIGHTLY"][$num] = scan_repos_split("{$ArticaMetaStorage}/nightlys/{$num}");
    }
    $dirs = $unix->dirdir("{$ArticaMetaStorage}/nightlys");
    while (list($num, $ligne) = each($dirs)) {
        $dirname = basename($num);
        if (!preg_match("#^artica-[\\.0-9]+#", $dirname)) {
            continue;
        }
        meta_events("Found {$num}");
        if (!is_file("{$num}/metaindex.txt")) {
            meta_events("Remove {$num} metaindex not found");
            shell_exec("{$rm} -rf {$num}");
            continue;
        }
        $arr = unserialize(@file_get_contents("{$num}/metaindex.txt"));
        if (count($arr) < 2) {
            meta_events("Remove {$num} metaindex not enough files");
            shell_exec("{$rm} -rf {$num}");
            continue;
        }
    }
    $dirs = $unix->dirdir("{$ArticaMetaStorage}/releases");
    while (list($num, $ligne) = each($dirs)) {
        $dirname = basename($num);
        if (!preg_match("#^artica-[\\.0-9]+#", $dirname)) {
            continue;
        }
        meta_events("Found {$num}");
        if (!is_file("{$num}/metaindex.txt")) {
            meta_events("Remove {$num} metaindex not found");
            shell_exec("{$rm} -rf {$num}");
            continue;
        }
        $arr = unserialize(@file_get_contents("{$num}/metaindex.txt"));
        if (count($arr) < 2) {
            meta_events("Remove {$num} metaindex not enough files");
            shell_exec("{$rm} -rf {$num}");
            continue;
        }
    }
    meta_events("Saving {$GLOBALS["MAIN_PATH"]}/updates.db");
    @file_put_contents("{$GLOBALS["MAIN_PATH"]}/updates.db", base64_encode(serialize($ARRAY)));
    @chown("{$GLOBALS["MAIN_PATH"]}/updates.db", $APACHE_ACCOUNT);
    @chmod("{$GLOBALS["MAIN_PATH"]}/updates.db", 0755);
}
function scan_repos()
{
    $unix = new unix();
    $rm = $unix->find_program("rm");
    $sock = new sockets();
    $APACHE_ACCOUNT = $unix->APACHE_SRC_ACCOUNT();
    $ArticaMaxPackages = intval($sock->GET_INFO("ArticaMaxPackages"));
    if ($ArticaMaxPackages == 0) {
        $ArticaMaxPackages = 5;
    }
    $ArticaMetaStorage = $sock->GET_INFO("ArticaMetaStorage");
    if ($ArticaMetaStorage == null) {
        $ArticaMetaStorage = "/home/artica-meta";
    }
    @mkdir("{$ArticaMetaStorage}/nightlys", 0755, true);
    @mkdir("{$ArticaMetaStorage}/releases", 0755, true);
    @mkdir($GLOBALS["MAIN_PATH"], 0755, true);
    meta_events("Scanning {$ArticaMetaStorage}/releases");
    scan_repos_progress("Scanning {$ArticaMetaStorage}/releases", 25);
    $unix = new unix();
    $files = $unix->DirFiles("{$ArticaMetaStorage}/releases");
    while (list($num, $ligne) = each($files)) {
        $NUM_INT = $num;
        scan_repos_progress("Scanning {$NUM_INT}", 25);
        if ($unix->file_time_min("{$ArticaMetaStorage}/releases/{$NUM_INT}") < 2880) {
            continue;
        }
        $NUM_INT = str_replace("artica-", "", $NUM_INT);
        $NUM_INT = str_replace(".tgz", "", $NUM_INT);
        $NUM_INT = str_replace(".", "", $NUM_INT);
        $XCOUNT[$NUM_INT] = $num;
    }
    if (count($XCOUNT) > $ArticaMaxPackages) {
        scan_repos_progress("{$XCOUNT} > {$ArticaMaxPackages}", 30);
        krsort($XCOUNT);
        $x = 0;
        while (list($num, $filename) = each($XCOUNT)) {
            scan_repos_progress("{$num} > {$filename}", 30);
            $x++;
            if ($x > $ArticaMaxPackages) {
                break;
            }
            $directory = str_replace(".tgz", "", $filename);
            scan_repos_progress("Removing {$ArticaMetaStorage}/releases/{$filename}", 30);
            @unlink("{$ArticaMetaStorage}/releases/{$filename}");
            if (is_dir("{$ArticaMetaStorage}/releases/{$directory}")) {
                shell_exec("{$rm} -rf {$ArticaMetaStorage}/releases/{$directory}");
            }
        }
    }
    $files = $unix->DirFiles("{$ArticaMetaStorage}/nightlys");
    while (list($num, $ligne) = each($files)) {
        $NUM_INT = $num;
        scan_repos_progress("Scanning {$NUM_INT}", 35);
        if ($unix->file_time_min("{$ArticaMetaStorage}/nightlys/{$NUM_INT}") < 2880) {
            continue;
        }
        $NUM_INT = str_replace("artica-", "", $NUM_INT);
        $NUM_INT = str_replace(".tgz", "", $NUM_INT);
        $NUM_INT = str_replace(".", "", $NUM_INT);
        $ZCOUNT[$NUM_INT] = $num;
    }
    if (count($ZCOUNT) > $ArticaMaxPackages) {
        krsort($ZCOUNT);
        $x = 0;
        while (list($num, $filename) = each($ZCOUNT)) {
            $x++;
            if ($x > $ArticaMaxPackages) {
                break;
            }
            $directory = str_replace(".tgz", "", $filename);
            scan_repos_progress("Removing {$ArticaMetaStorage}/nightlys/{$filename}", 36);
            @unlink("{$ArticaMetaStorage}/nightlys/{$filename}");
            if (is_dir("{$ArticaMetaStorage}/nightlys/{$directory}")) {
                shell_exec("{$rm} -rf {$ArticaMetaStorage}/nightlys/{$directory}");
            }
        }
    }
    $files = $unix->DirFiles("{$ArticaMetaStorage}/releases");
    while (list($num, $ligne) = each($files)) {
        if (!preg_match("#^artica-[\\.0-9]+#", $num)) {
            continue;
        }
        meta_events("Found package {$ArticaMetaStorage}/releases/{$num}");
        $ARRAY["RELEASES"][$num] = scan_repos_split("{$ArticaMetaStorage}/releases/{$num}");
    }
    $dirs = $unix->dirdir("{$ArticaMetaStorage}/releases");
    while (list($num, $ligne) = each($dirs)) {
        $dirname = basename($num);
        if (!preg_match("#^artica-[\\.0-9]+#", $dirname)) {
            continue;
        }
        meta_events("Found {$num}");
        if (!is_file("{$num}/metaindex.txt")) {
            meta_events("Remove {$num} metaindex not found");
            shell_exec("{$rm} -rf {$num}");
            continue;
        }
        $arr = unserialize(@file_get_contents("{$num}/metaindex.txt"));
        if (count($arr) < 2) {
            meta_events("Remove {$num} metaindex not enough files");
            shell_exec("{$rm} -rf {$num}");
            continue;
        }
    }
    $files = $unix->DirFiles("{$ArticaMetaStorage}/nightlys");
    while (list($num, $ligne) = each($files)) {
        if (!preg_match("#^artica-[\\.0-9]+#", $num)) {
            continue;
        }
        meta_events("Found package {$ArticaMetaStorage}/nightlys/{$num}");
        $ARRAY["NIGHTLY"][$num] = scan_repos_split("{$ArticaMetaStorage}/nightlys/{$num}");
    }
    $dirs = $unix->dirdir("{$ArticaMetaStorage}/nightlys");
    while (list($num, $ligne) = each($dirs)) {
        $dirname = basename($num);
        if (!preg_match("#^artica-[\\.0-9]+#", $dirname)) {
            continue;
        }
        scan_repos_progress("Found {$num}", 45);
        if (!is_file("{$num}/metaindex.txt")) {
            meta_events("Remove {$num} metaindex not found");
            shell_exec("{$rm} -rf {$num}");
            continue;
        }
        $arr = unserialize(@file_get_contents("{$num}/metaindex.txt"));
        if (count($arr) < 2) {
            meta_events("Remove {$num} metaindex not enough files");
            shell_exec("{$rm} -rf {$num}");
            continue;
        }
    }
    $dirs = $unix->dirdir("{$ArticaMetaStorage}/releases");
    while (list($num, $ligne) = each($dirs)) {
        $dirname = basename($num);
        if (!preg_match("#^artica-[\\.0-9]+#", $dirname)) {
            continue;
        }
        scan_repos_progress("Found {$num}", 50);
        if (!is_file("{$num}/metaindex.txt")) {
            meta_events("Remove {$num} metaindex not found");
            shell_exec("{$rm} -rf {$num}");
            continue;
        }
        $arr = unserialize(@file_get_contents("{$num}/metaindex.txt"));
        if (count($arr) < 2) {
            meta_events("Remove {$num} metaindex not enough files");
            shell_exec("{$rm} -rf {$num}");
            continue;
        }
    }
    scan_repos_progress("Creating index file", 60);
    meta_events("Saving {$GLOBALS["MAIN_PATH"]}/updates.db");
    @file_put_contents("{$GLOBALS["MAIN_PATH"]}/updates.db", base64_encode(serialize($ARRAY)));
    @chown("{$GLOBALS["MAIN_PATH"]}/updates.db", $APACHE_ACCOUNT);
    @chmod("{$GLOBALS["MAIN_PATH"]}/updates.db", 0755);
    if ($GLOBALS["OUTPUT"]) {
        scan_repos_progress("{done}", 95);
        sleep(5);
    }
    scan_repos_progress("{done}", 100);
}