OSDN Git Service

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