#!/usr/bin/env php
<?php
/**
 * The controller of xuanxuan.
 *
 * @copyright   Copyright 2009-2017 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
 * @license     ZPL (http://zpl.pub/page/zplv12.html)
 * @author      Gang Liu <liugang@cnezsoft.com> 
 * @package     xuanxuan 
 * @version     $Id$
 * @link        http://xuanxuan.chat
 */
array_shift($argv);
$basePath = dirname(dirname(__FILE__));
$username = 'www-data'; // Use the run user of your web server as username.

if(empty($argv) or $argv[0] == '--help' or $argv[0] == '-h')
{
    echo <<<EOD
Usage: xuanxuan.php {start|stop|restart|status} [options]

Options:
    -h --help Show help.
    -u --user Your web server's run user, default www-data

EOD;
    exit;
}


if($argv[0] == 'start' or $argv[0] == 'restart')
{
    if(isset($argv[1]) && ($argv[1] == '-u' or $argv[1] == '--user'))
    {
        if(empty($argv[2]))
        {
            echo "Must set a user as param.\n";
            exit;
        }
        $username = $argv[2];
    }
}

switch($argv[0])
{
case 'start':
    $xuanxuan = `ps aux|grep 'php $basePath/app/xuanxuan/server.php'|grep -v 'grep'`;
    if($xuanxuan)
    {
        echo "Xuanxuan is running\n";
    }
    else
    {
        echo `sudo sudo -u $username $basePath/app/xuanxuan/server.php >> $basePath/tmp/log/xuanxuan.log &`;
        sleep(1);
        $xuanxuan = `ps aux|grep 'php $basePath/app/xuanxuan/server.php'|grep -v 'grep'`;
        echo !empty($xuanxuan) ? "Start Xuanxuan success\n" : "Start Xuanxuan fail. You can see the log $basePath/tmp/log/xuanxuan.log\n";
    }
    break;
case 'stop':
    $xuanxuan = `ps aux|grep 'php $basePath/app/xuanxuan/server.php'|grep -v 'grep'|awk -F ' ' '{print $2}'`;
    if($xuanxuan)
    {
        echo `sudo kill -9 $xuanxuan`;
        sleep(1);
        $xuanxuan = `ps aux|grep 'php $basePath/app/xuanxuan/server.php'|grep -v 'grep'`;
        echo empty($xuanxuan) ? "Stop Xuanxuan success\n" : "Stop Xuanxuan fail. You can see the log $basePath/tmp/log/xuanxuan.log\n";
    }
    else
    {
        echo "Xuanxuan is not running\n";
    }
    break;
case 'restart':
    /* Stop */
    $xuanxuan = `ps aux|grep 'php $basePath/app/xuanxuan/server.php'|grep -v 'grep'|awk -F ' ' '{print $2}'`;
    if($xuanxuan)
    {
        echo "Stopping Xuanxuan\n";
        echo `sudo kill -9 $xuanxuan`;
        sleep(1);
        $xuanxuan = `ps aux|grep 'php $basePath/app/xuanxuan/server.php'|grep -v 'grep'`;
        if(empty($xuanxuan))
        {
            echo "Stop Xuanxuan succes\n";
        }
        else
        {
            echo "Stop Xuanxuan fail. You can see the log $basePath/tmp/log/xuanxuan.log\n";
            break;
        }
    }
    else
    {
        echo "Xuanxuan is not running\n";
    }

    /* Start */
    echo "Starting Xuanxuan\n";
    echo `sudo sudo -u $username $basePath/app/xuanxuan/server.php >> $basePath/tmp/log/xuanxuan.log &`;
    sleep(1);
    $xuanxuan = `ps aux|grep 'php $basePath/app/xuanxuan/server.php'|grep -v 'grep'`;
    echo !empty($xuanxuan) ? "Start Xuanxuan success\n" : "Start Xuanxuan fail. You can see the log $basePath/tmp/log/xuanxuan.log\n";
    break;
case 'status':
    $xuanxuan = `ps aux|grep 'php $basePath/app/xuanxuan/server.php'|grep -v 'grep'`;
    echo empty($xuanxuan) ? "Xuanxuan is not running\n" : "Xuanxuan is running\n";
}
