#!/opt/Guardian/Admin/php/bin/php
<?php
// α᡼ǤBOXȺBOXפʥȥ
//   - פʥȥȤ
//     + α᡼(С)ˤ̵ʤäå
//     + Sˤ¸֤ĶƼư줿å
//     + SϿ줿SͭƤȻפå
//   - guts_daily.sh ¹Ԥ륵֥ץ
//   - SΤߤǼ¹ԲǽrootΤߤǼ¹Բǽ
require_once 'rpc_lib.php';

mb_internal_encoding("EUC-JP");
mb_http_output("EUC-JP");
clearstatcache();   //statå奯ꥢ

$Dmode = false;     //ǥХå⡼
array_shift($argv);
while (($opt = array_shift($argv)) != null) {
    switch ($opt) {
        case '-d':
        case '--debug':
            $Dmode = true;
            break;
        default:
            fwrite(STDERR, "Error: unknown option (" . $opt . " ?)\n");
        case '-h':
        case '--help':
            usage();
    }
}

$isEntry = false;
$boxes = array( WALL_ETC_C . 'queue/snd.status', WALL_ETC_C . 'queue/del.status');
foreach ($boxes as $box) {
    if (!file_exists($box))
        continue;

    $data = @file($box);
    if ($data === false)
        continue;

    $data = array_strip($data);
    foreach ($data as $ent) {
        if (!$ent)
            continue;
        $isEntry = true;
        break;
    }
    unset($data);
    if ($isEntry)
        break;
}
if (!$isEntry) {
    if ($Dmode) print "NO DATA\n";
    exit(0);
}

//̿ȯ뤿ᡤФ餯ٻ(3ʬ)
if (!$Dmode) sleep(180);

$list = array();
$fasv = array();
$sv = getGDNServerList('mail');
if (count($sv) > 0) {
    $fnbase = tempnam(TMP_DIR, "holdlist");
    $cmd = MW_LIST . ' -C -l';
    $rtn = guts_exec_cmd_output_c('mail', $cmd, $fnbase . ".%s");
    foreach ($rtn as $id => $succ) {
        if ($succ !== true)
            $fasv[] = $id;
    }

    $files = glob($fnbase . ".*");
    if (is_array($files)) {
        foreach ($files as $fn) {
            $srvid = preg_replace("/^(\w*\.)+(\d+)$/", "$2", basename($fn));
            $v = @file($fn);
            foreach ($v as $x) {
                $x = trim($x);
                if ($x == '')
                    continue;
                $ent = split("\t", $x);
                $list[] = $ent[2] . '-' . $srvid;
                unset($ent);
            }
            unlink($fn);
        }
    }
    unlink($fnbase);
}

if ($Dmode) var_dump($sv, $fasv, $list); //ǥХåSꥹȡԸꥹȡα᡼ꥹȤ
unset($sv);

$lfp = get_lock(TMP_DIR, 'lock.qbox', false);
if ($lfp === false) {
    fwrite(STDERR, "Internal error: lock file open failed\n");
    exit(1);
}

foreach ($boxes as $box) {
    if (!file_exists($box))
        continue;

    $data = @file($box);
    if ($data === false)
        continue;
    $data = array_unique($data);
    $data = array_strip($data);
    if (count($data) <= 0)
        continue;

    $ents = array();
    foreach ($data as $ent) {
        if (!$ent)
            continue;
        if (in_array($ent, $list)) {
            $ents[] = $ent;
            continue;
        }
        $sid = preg_replace("/\w{8}-(\d+)/", "$1", $ent);
        if (in_array($sid, $fasv))
            $ents[] = $ent;
    }

    if ($Dmode) var_dump($box, $data, $ents); //ǥХåBOXեOLDꥹȡNEWꥹ

    $tmpf = tempnam(TMP_DIR, basename($box));
    $ents = array_strip($ents);
    $fp = fopen($tmpf, "w");
    if ($fp === false) {
        fwrite(STDERR, "Internal error: file open failed[" . $tmpf . "]\n");
        unlink($tmpf);
        continue;
    }
    if (is_array($ents) && count($ents) > 0)
        fputs($fp, join("\n", $ents) . "\n");
    else
        fputs($fp, '');
    fclose($fp);

    if (!su_mv($tmpf, $box)) {
        fwrite(STDERR, "Internal error: file move failed[to " . $box . "]\n");
        unlink($tmpf);
        continue;
    }
    su_chown('nobody:nobody', $box);
    su_chmod('0600', $box);

    unset($data, $ents);
}
release_lock($lfp);

exit(0);

//ˡ
function usage(){
    fwrite(STDERR, "Usage: " . basename($_SERVER['PHP_SELF']) . " [options]\n");
    fwrite(STDERR, " options...\n");
    fwrite(STDERR, "  -d [--debug]: debug mode. display internal parameters detail.\n");
    fwrite(STDERR, "  -h [--help] : display this.\n");
    exit(1);
}
