OSDN Git Service

- Changed version number for 2.3.2 code FREEZE part 2.
[ethna/ethna.git] / bin / ethna_handle.php
1 <?php
2 /**
3  *  ethna_handle.php
4  *
5  *  Ethna Handle Gateway
6  *
7  *  @author     Masaki Fujimoto <fujimoto@php.net>
8  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
9  *  @package    Ethna
10  *  @version    $Id$
11  */
12 while (ob_get_level()) {
13     ob_end_clean();
14 }
15
16 // setup path seprator
17 if (!defined('PATH_SEPARATOR')) {
18     if (OS_WINDOWS) {
19         /** include_path separator(Windows) */
20         define('PATH_SEPARATOR', ';');
21     } else {
22         /** include_path separator(Unix) */
23         define('PATH_SEPARATOR', ':');
24     }
25 }
26 $base = dirname(dirname(dirname(__FILE__)));
27 ini_set('include_path', $base.PATH_SEPARATOR.ini_get('include_path'));
28
29 require_once 'PEAR.php';
30 require_once 'Console/Getopt.php';
31 require_once 'Ethna/Ethna.php';
32
33 // fetch arguments
34 $arg_list = Console_Getopt::readPHPArgv();
35 if (Ethna::isError($arg_list)) {
36     echo $arg_list->getMessage()."\n";
37     exit(2);
38 }
39 array_shift($arg_list);
40
41 $eh =& new Ethna_Handle();
42
43 list($my_arg_list, $arg_list) = _Ethna_HandleGateway_SeparateArgList($arg_list);
44 $r = Console_Getopt::getopt($my_arg_list, "v", array("version"));
45 if (Ethna::isError($r)) {
46     usage($eh);
47     exit(1);
48 }
49
50 // ad-hoc:(
51 foreach ($r[0] as $opt) {
52     if ($opt[0] == "v" || $opt[0] == "--version") {
53         _Ethna_HandleGateway_ShowVersion();
54         exit(2);
55     }
56 }
57
58 if (count($arg_list) == 0) {
59     usage($eh);
60     exit(1);
61 }
62
63 $id = array_shift($arg_list);
64
65 $handler =& $eh->getHandler($id);
66 if (Ethna::isError($handler)) {
67     printf("no such command: %s\n\n", $id);
68     usage($eh);
69     exit(1);
70 }
71
72 // don't know what will happen:)
73 $handler->setArgList($arg_list);
74 $r = $handler->perform();
75 if (Ethna::isError($r)) {
76     printf("error occured w/ command [%s]\n  -> %s\n\n", $id, $r->getMessage());
77     if ($r->getCode() == 'usage') {
78         $handler->usage();
79     }
80     exit(1);
81 }
82
83 /**
84  *  usage
85  */
86 function usage(&$eh)
87 {
88     $handler_list = $eh->getHandlerList();
89     printf("usage: ethna [option] [command] [args...]\n\n");
90     printf("available options are as follows:\n\n");
91     printf("  -v, --version    show version and exit\n");
92     printf("\navailable commands are as follows:\n\n");
93     foreach ($handler_list as $handler) {
94         printf("  %s -> %s\n", $handler->getId(), $handler->getDescription());
95     }
96 }
97
98 /**
99  *  fetch options for myself
100  */
101 function _Ethna_HandleGateway_SeparateArgList($arg_list)
102 {
103     $my_arg_list = array();
104     for ($i = 0; $i < count($arg_list); $i++) {
105         if ($arg_list[$i]{0} == '-') {
106             // assume this should be an option for myself
107             $my_arg_list[] = $arg_list[$i];
108         } else {
109             break;
110         }
111     }
112     $arg_list = array_slice($arg_list, $i);
113
114     return array($my_arg_list, $arg_list);
115 }
116
117 /**
118  *  show version
119  */
120 function _Ethna_HandleGateway_ShowVersion()
121 {
122     $version = <<<EOD
123 Ethna %s
124
125 Copyright (c) 2004-2006,
126   Masaki Fujimoto <fujimoto@php.net>
127   halt feits <halt.feits@gmail.com>
128   Takuya Ookubo <sfio@sakura.ai.to>
129   nozzzzz <nozzzzz@gmail.com>
130   cocoitiban <cocoiti@comio.info>
131
132 http://ethna.jp/
133
134 EOD;
135     printf($version, ETHNA_VERSION);
136 }
137 ?>