#!/usr/local/bin/php -q
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | Eventum - Defect Tracking System                                     |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003 Joao Prado Maia                                   |
// +----------------------------------------------------------------------+
// | Authors: Joo Prado Maia <jpm@impleo.net>                            |
// +----------------------------------------------------------------------+
//
// @(#) $Id: s.eventum 1.3 03/12/13 00:03:39-00:00 jpradomaia $
//
include_once("config.inc.php");
include_once(APP_INC_PATH . "class.command_line.php");
include_once(APP_PEAR_PATH . "XML_RPC/RPC.php");

list($user_email, $user_password, $url, $port, $relative_url) = Command_Line::getEnvironmentSettings();
if (empty($port)) {
    $port = 80;
}
if (empty($relative_url)) {
    $relative_url = '/';
}

if (count($HTTP_SERVER_VARS['argv']) == 1) {
    Command_Line::quit("Requirement argument not found");
}
// show usage information if user gave --help
if (($HTTP_SERVER_VARS['argv'][1] == '--help') || ($HTTP_SERVER_VARS['argv'][1] == 'help')) {
    Command_Line::usage(__FILE__);
}

$should_confirm = Command_Line::isSafeExecution();

$client = new XML_RPC_Client($relative_url . "rpc/xmlrpc.php", $url, $port);
//$client->setDebug(1);

// need to process authentication first
Command_Line::checkAuthentication(&$client, $user_email, $user_password);

$auth = array($user_email, $user_password);

// log command
Command_Line::log(&$client, $auth, join(" ", $HTTP_SERVER_VARS['argv']));

$issue_id = (integer) $HTTP_SERVER_VARS['argv'][1];
if ($issue_id > 0) {
    if (count($HTTP_SERVER_VARS['argv']) == 2) {
        Command_Line::printIssueDetails(&$client, $auth, $issue_id);
    } else {
        if ($should_confirm) {
            Command_Line::promptConfirmation(&$client, $auth, $issue_id, @$HTTP_SERVER_VARS['argv']);
        }
        switch ($HTTP_SERVER_VARS['argv'][2]) {
            case 'assign':
                if (count($HTTP_SERVER_VARS['argv']) == 3) {
                    Command_Line::quit("Missing parameter for the developer");
                }
                Command_Line::assignIssue(&$client, $auth, $issue_id, $HTTP_SERVER_VARS['argv'][3]);
                break;
            case 'add-replier':
            case 'ar':
                // adds a user to the list of authorized repliers
                if (count($HTTP_SERVER_VARS['argv']) == 3) {
                    Command_Line::quit("Missing parameter for the developer");
                }
                Command_Line::addAuthorizedReplier(&$client, $auth, $issue_id, $HTTP_SERVER_VARS['argv'][3]);
                break;
            case 'set-status':
                if (count($HTTP_SERVER_VARS['argv']) == 3) {
                    Command_Line::quit("Missing parameter for the status");
                }
                Command_Line::setIssueStatus(&$client, $auth, $issue_id, $HTTP_SERVER_VARS['argv'][3]);
                break;
            case 'add-time':
                if (count($HTTP_SERVER_VARS['argv']) == 3) {
                    Command_Line::quit("Missing parameter for time worked");
                }
                $check = (integer) $HTTP_SERVER_VARS['argv'][3];
                if ($check == 0) {
                    Command_Line::quit("Third argument to command 'add-time' should be a number");
                }
                Command_Line::addTimeEntry(&$client, $auth, $issue_id, $check);
                break;
            case 'list-files':
            case 'lf':
                Command_Line::printFileList(&$client, $auth, $issue_id);
                break;
            case 'get-file':
            case 'gf':
                if (count($HTTP_SERVER_VARS['argv']) == 3) {
                    Command_Line::quit("Missing parameter for the file number");
                }
                Command_Line::getFile(&$client, $auth, $issue_id, $HTTP_SERVER_VARS['argv'][3]);
                break;
            case 'close':
                Command_Line::closeIssue(&$client, $auth, $issue_id);
                break;

            // email related commands
            case 'list-emails':
            case 'le':
                // lists all emails for the given issue
                Command_Line::listEmails(&$client, $auth, $issue_id);
                break;
            case 'get-email':
            case 'ge':
                // views an email
                if (count($HTTP_SERVER_VARS['argv']) == 3) {
                    Command_Line::quit("Missing parameter for the email number");
                }
                if (@$HTTP_SERVER_VARS['argv'][4] == "--full") {
                    $full = true;
                } else {
                    $full = false;
                }
                Command_Line::printEmail(&$client, $auth, $issue_id, $HTTP_SERVER_VARS['argv'][3], $full);
                break;

            // note related commands
            case 'list-notes':
            case 'ln':
                // list notes for the given issues
                Command_Line::listNotes(&$client, $auth, $issue_id);
                break;
            case 'get-note':
            case 'gn':
                // view a note
                if (count($HTTP_SERVER_VARS['argv']) == 3) {
                    Command_Line::quit("Missing parameter for the note number");
                }
                Command_Line::printNote(&$client, $auth, $issue_id, $HTTP_SERVER_VARS['argv'][3]);
                break;
            case 'convert-note':
            case 'cn':
                // convert a note to an email
                if (empty($HTTP_SERVER_VARS['argv'][3])) {
                    Command_Line::quit("Missing parameter for the note number");
                }
                if (@$HTTP_SERVER_VARS['argv'][4] != 'draft' && @$HTTP_SERVER_VARS['argv'][4] != 'email' ) {
                    Command_Line::quit("4th parameter must be 'draft' or 'email'");
                }
                if (@$HTTP_SERVER_VARS['argv'][5] == 'authorize') {
                    $authorize_sender = true;
                } else {
                    $authorize_sender = false;
                }
                Command_Line::convertNote(&$client, $auth, $issue_id, $HTTP_SERVER_VARS['argv'][3], $HTTP_SERVER_VARS['argv'][4], $authorize_sender);
                break;
            
            // draft related commands
            case 'list-drafts':
            case 'ld':
                // list drafts
                Command_Line::listDrafts(&$client, $auth, $issue_id);
                break;
            case 'get-draft':
            case 'gd':
                // viewing a draft
                if (count($HTTP_SERVER_VARS['argv']) == 3) {
                    Command_Line::quit("Missing parameter for the draft number");
                }
                Command_Line::printDraft(&$client, $auth, $issue_id, $HTTP_SERVER_VARS['argv'][3]);
                break;
            case 'send-draft':
            case 'sd':
                // viewing a draft
                if (count($HTTP_SERVER_VARS['argv']) == 3) {
                    Command_Line::quit("Missing parameter for the draft number");
                }
                Command_Line::sendDraft(&$client, $auth, $issue_id, $HTTP_SERVER_VARS['argv'][3]);
                break;
                
            case 'redeem':
                // marking an issue as redeemed
                Command_Line::redeemIssue(&$client, $auth, $issue_id);
                break;
            
            case 'unredeem':
                // unmarks issue as redeemed incident
                Command_Line::unredeemIssue(&$client, $auth, $issue_id);
                break;
            
            default:
                Command_Line::quit("Unknown command '" . $HTTP_SERVER_VARS['argv'][2] . "'");
        }
    }
} else {
    if ($HTTP_SERVER_VARS['argv'][1] == 'developers') {
        Command_Line::printDeveloperList(&$client, $auth);
    } elseif ($HTTP_SERVER_VARS['argv'][1] == 'open-issues') {
        if (count($HTTP_SERVER_VARS['argv']) == 3) {
            if (@$HTTP_SERVER_VARS['argv'][2] == 'my') {
                $show_all_issues = false;
                $status = '';
            } else {
                $show_all_issues = true;
                $status = $HTTP_SERVER_VARS['argv'][2];
            }
        } elseif (count($HTTP_SERVER_VARS['argv']) == 4) {
            if (@$HTTP_SERVER_VARS['argv'][3] == 'my') {
                $show_all_issues = false;
            } else {
                $show_all_issues = true;
            }
            $status = $HTTP_SERVER_VARS['argv'][2];
        } else {
            $show_all_issues = true;
            $status = '';
        }
        Command_Line::printOpenIssues(&$client, $auth, $show_all_issues, $status);
    } elseif ($HTTP_SERVER_VARS['argv'][1] == 'list-status') {
        Command_Line::printStatusList(&$client, $auth);
    } elseif ($HTTP_SERVER_VARS['argv'][1] == 'customer') {
        if (count($HTTP_SERVER_VARS['argv']) != 4) {
            Command_Line::quit("Wrong parameter count");
        }
        Command_Line::lookupCustomer(&$client, $auth, $HTTP_SERVER_VARS['argv'][2], $HTTP_SERVER_VARS['argv'][3]);
    } elseif (($HTTP_SERVER_VARS['argv'][1] == 'weekly-report') || ($HTTP_SERVER_VARS['argv'][1] == 'wr')) {
        if (count(@$HTTP_SERVER_VARS['argv']) == 4) {
            // date range
            Command_Line::getWeeklyReport(&$client, $auth, 0, $HTTP_SERVER_VARS['argv'][2], $HTTP_SERVER_VARS['argv'][3]);
        } else {
            // weekly
            if (@$HTTP_SERVER_VARS['argv'][2] == '') {
                @$HTTP_SERVER_VARS['argv'][2] = 0;
            }
            Command_Line::getWeeklyReport(&$client, $auth, $HTTP_SERVER_VARS['argv'][2]);
        }
    } elseif ($HTTP_SERVER_VARS['argv'][1] == 'clock') {
        Command_Line::timeClock(&$client, $auth, @$HTTP_SERVER_VARS['argv'][2]);
    }  else {
        Command_Line::quit("Unknown parameter '" . $HTTP_SERVER_VARS['argv'][1] . "'");
    }
}

?>