#!/bin/php
<?php
if (!file_exists("/var/db/pw-update-instructions")) {
    passthru("svn checkout svn://genpkg-repos.petersweeze.de/repos/update-instructions /var/db/pw-update-instructions");
} else {
    $modtime = filemtime("/var/db/pw-update-instructions/.svn/wc.db");
    if (($modtime + (60 * 60)) > time()) {
        echo "update not older than 1h, skip update!\r\n";
    } else {
        passthru("svn update /var/db/pw-update-instructions");
    }
}

$cascade = file("/var/db/pw-update-instructions/main_target_revs/cascade.txt");
$blocked_revs = parse_blocked();

$myrev = -1;
foreach ($cascade as $line) {
    $line = trim($line);
    $exp = explode(": ", $line);
    $top = str_replace("top", "", $exp[0]);
    $top = trim($top);
    $target_rev = trim($exp[1]);

    $retcode = -1;
    $output = "";
    $affected = exec("pw-uniq.php iftop " . $top, $output, $retcode);
    $lowest_unblocked=-1;
    if ($retcode == 1) {
        if (isset($argv[1]) && $argv[1] == "debug") echo "TOP " . $top . " - Rev: " . $target_rev . " -> We are in!\r\n";
        if ($target_rev > $myrev) {
            $blocked=false;
            foreach($blocked_revs as $blocked_rev) {
                if($target_rev>=$blocked_rev["from"] && $target_rev<=$blocked_rev["to"]) {
                    $blocked=true;
                    echo "TOP " . $top . " - Rev: " . $target_rev . " -> Is blocked because ".$blocked_rev["desc"]."!\r\n";
                    $this_unblocked=$blocked_rev["from"]-1;
                    if($this_unblocked<$lowest_unblocked || $lowest_unblocked==-1){
                        $lowest_unblocked=$this_unblocked;
                    }
                }
            }
            if(!$blocked) {
                $myrev = $target_rev;
            }
        }
    } elseif ($retcode == 2) {
        if (isset($argv[1]) && $argv[1] == "debug") echo "TOP " . $top . " - Rev: " . $target_rev . " -> We are out!\r\n";
    } else {
        echo "TOP " . $top . " - Rev: " . $target_rev . " -> ERROR...!\r\n";
        print_r($output);
    }
}

if($myrev==-1){
    $myrev = $lowest_unblocked;
}

echo "Target Revision is: " . $myrev . "\r\n";
$exec = shell_exec("svn log --revision " . $myrev . " svn://genpkg-repos.petersweeze.de/repos");
$explode = explode(" | ", $exec);
echo "Target Revision is from: " . $explode[2] . "\r\n";
$myrev_dt = strtotime(substr(trim($explode[2]), 0, 25));
$myrev_age_dt = round((time() - $myrev_dt) / 24 / 60 / 60, 0);
echo "Target Revision is " . $myrev_age_dt . " days old!\r\n";
$top = shell_exec("pw-uniq.php getperc");
$top = explode(":", $top);
$top = round(trim($top[1]) / 10, 1);
echo "Our pw-uniq.php getperc says us, that we are in top " . $top . "% of pilot machines!\r\n";

function parse_blocked()
{
    $blocked = file("/var/db/pw-update-instructions/main_target_revs/manual_added_broken_revs.txt");
    $blk = array();
    foreach ($blocked as $line) {
        if (empty($line)) continue;

        $exp = explode(":", $line);
        $desc = trim($exp[1]);
        $exp2 = explode("-", $exp[0]);
        $revfrom = trim($exp2[0]);
        $revto = trim($exp2[1]);
        $blk[] = array("from" => $revfrom, "to" => $revto, "desc" => $desc);
    }
    return ($blk);
}