#!/opt/Guardian/Admin/php/bin/php
<?php
require_once "util.php";
require_once "send_config.php";

define('S_TYPE', 'web');

/****************************************/
/* function                             */
/****************************************/

/* 顼 */
function error($s) {
    if ($s) 
        print "ERROR: " . $s . "\n";
    else 
        print "error.";
    exit(-1);
}

/* ɽ */
function usage($err) {
    if ($err) print $err . "\n";
    print "Usage: pushWebWG [options] type\n";
    print "  type: Type         [all|admin|admhp|rule|user|url|keywd|mtype|schedule|msg|httpd|mail|template|httpclient|license] \n";
    print "    -s: server_id .. [ServerID, ..] \n";
    print "    -f: Files ..     [FileName, ..] \n";
    print "    -r: [reboot filter Server] \n";
    print "    -d: [remove file] \n";

    exit(1);
}

/****************************************/
/* main                                 */
/****************************************/

$type = null;
$server_ids = null;
$files = null;
$restart = false;
$action = true;

if (count($argv) == 1) {
    usage(null);
}

/*  */
array_shift($argv);
while (($opt = array_shift($argv)) != null) {
    if ($opt{0} != '-') {
        $type = $opt;
        break;
    }
    switch ($opt{1}) {
    case 's':   // Server id
        $server_id = array_shift($argv);
        $server_ids = preg_split("/,/", $server_id);
        break;
    case 'f':   // names
        $file = array_shift($argv);
        $files = preg_split("/,/", $file);
        break;
    case 'r':   // restart
        $restart = true;
        break;
    case 'd':   // remove
        $action = false;
        break;
    default:
        break;
        usage(null);
    }
}
if (!$type)
    $type = array_shift($argv);
if (count($argv) > 0)
    error('A lot of parameters.');

$conf = new Conf($type, $server_ids, $files, $action, $restart);

/* ϥå */
if (!$conf->isValid(&$err)) {
    usage($err);
}

$fp = get_lock();
if (!$fp) {
    error('file is locked now.');
}

/* ¹ */
if ($conf->execute(&$err)) {
    release_lock($fp);
    print "finish.(" . $conf->toString() . ")\n";
    exit(0);
} else {
    release_lock($fp);
    error($err);
}

/****************************************/
/* class Conf                           */
/****************************************/
class Conf {
    var $type;          // all|rule|user|url|keywd|mtype|schedule|msg|httpd|mail|template|httpclient|license
    var $server_ids;    // array ServerId
    var $files;         // array files

    var $action = true;
    var $restart = false;

    /*  */
    var $types = array('all','admin','admhp','rule','user','url','keywd','mtype','schedule','msg','httpd','mail','template','httpclient','license');

    /* -f ɬܥ */
    var $ftypes = array('msg');

    /* եǥ쥯ȥ */
    var $filedir = array();

    /* եĥ */
    var $suffix = array();

    /* 󥹥ȥ饯 */
    function Conf(  $type = null,
                    $server_ids = null,
                    $files = null,
                    $action = true,
                    $restart = false
                    )
    {
        $this->type = $type;
        $this->server_ids = $server_ids;
        $this->files = $files;
        $this->action = $action;
        $this->restart = $restart;

        $this->filedir = array( 'msg' => WG_ETC_C .  "msg/");

        $this->suffix  = array( 'msg' => '.html');
    }

    /* եƱ */
    function execute($err) {
        // type='all' λ $type = null
        $type = $this->type;
        if ($type == 'all') $type = null;

        // type='keywd' λ files = array('wg')
        if ($this->type == 'keywd') $this->files = array('wg');

        // ServerID ꤢ
        if ($this->server_ids) {
            foreach($this->server_ids as $id) {
                // СTimesatempϹʤ
                // Timestamp.<sid>̤Ϳ
                if (!guts_send_config($id, S_TYPE, $type, $this->files, $this->action, $this->restart, &$out, false, false)) {
                    return false;
                }
            }
        } else {
            // СTimesatemp⹹
            if (!guts_send_config(null, S_TYPE, $type, $this->files, $this->action, $this->restart, &$out)) {
                return false;
            }
        }
        return true;
    }

    /* ϥå */
    function isValid($err) {
        // Type
        if (!$this->type) {
            $err = 'no type.';
            return false;
        }
        if (!in_array($this->type, $this->types)) {
            $err = 'invalid type.';
            return false;
        }
        // Type : -f ɬܤΥ
        if (in_array($this->type, $this->ftypes)) {
            if (!$this->files || count($this->files) == 0) {
                $err = "must set " . $this->type . " files[-f].";
                return false;
            }
        }
        // ե¸ߥå
        if ($this->files) {
            $dir = $this->filedir[$this->type];
            $suffix = $this->suffix[$this->type];

            foreach($this->files as $file) {
                $file = $dir . $file . $suffix;
                if (!file_exists($file)) {
                    $err = "don't file exists ($file).";
                    return false;
                }
            }
        }
        // Server ID
        if ($this->server_ids) {
            $list = getGDNServerList(S_TYPE);
            foreach($this->server_ids as $id) {
                $flag = false;
                foreach($list as $s) {
                    if ($s->id == $id) {
                        $flag = true;
                        break;
                    }
                }
                if (!$flag) {
                    $err = "ServerID: $id is not WEBGUARDIAN server.";
                    return false;
                }
            }
        }
        return true;
    }
    function toString() {
        $s = '';
        if ($this->type)       $s = "type:" . $this->type . " ";
        if ($this->server_ids) $s.= "server_id(" . join(",", $this->server_ids) . ") ";
        if ($this->files)      $s.= "files(" . join(",", $this->files) . ") ";
        if (!$this->action)    $s.= "remove ";
        if ($this->restart)    $s.= "restart ";

        return $s;
    }
}
?>
