ignore releases dir
[linksys-wrt3g-tools.git] / scripts / linksys-wrt3g.php
index ae7317bd3a00f50e42af14b743f61c440c50ae69..d9d72884fbd34098e8780f32a0decb05c0e23500 100755 (executable)
 require_once 'Wrt3g.php';
 require_once 'Console/CommandLine.php';
 
-//default config options
-$GLOBALS['linksys-wrt3g-tools'] = array(
-    'host' => null,
-    'user' => 'admin',
-    'password' => null,
-);
-
-$configFile = dirname(__FILE__) . '/../config.php';
-if (file_exists($configFile)) {
-    require_once $configFile;
-}
-
 $parser = new Console_CommandLine();
-$parser->description = 'Tool to control Linksys WRT3g routers';
+$parser->description = "Tool to control Linksys WRT3g routers";
 $parser->version = '0.0.1';//FIXME: dynamic
 $parser->addOption(
     'host',
@@ -37,7 +25,7 @@ $parser->addOption(
         'description' => 'IP/Hostname to connect to',
         'help_name'   => 'HOST',
         'action'      => 'StoreString',
-        'default'     => $GLOBALS['linksys-wrt3g-tools']['host']
+        'default'     => null
     )
 );
 $parser->addOption(
@@ -48,7 +36,7 @@ $parser->addOption(
         'description' => 'Admin user name',
         'help_name'   => 'USER',
         'action'      => 'StoreString',
-        'default'     => $GLOBALS['linksys-wrt3g-tools']['user']
+        'default'     => 'admin'
     )
 );
 $parser->addOption(
@@ -59,17 +47,57 @@ $parser->addOption(
         'description' => 'Password for admin user',
         'help_name'   => 'PASS',
         'action'      => 'StoreString',
-        'default'     => $GLOBALS['linksys-wrt3g-tools']['password']
+        'default'     => null
+    )
+);
+$parser->addOption(
+    'verbosity',
+    array(
+        'short_name'  => '-v',
+        'long_name'   => '--verbose',
+        'description' => 'Show more details (more to see more details)',
+        'action'      => 'Counter',
+    )
+);
+$parser->addOption(
+    'dummy',
+    array(
+        'long_name'   => '--dummy',
+        'description' => "Use dummy router data, not real ones.
+Dummy responses can be controlled with the host parameter; 3-letter numeric hosts are interpreted as HTTP response code",
+        'action'      => 'StoreTrue',
     )
 );
 
-$parser->addCommand(
+$stCmd = $parser->addCommand(
     'status',
     array(
         'aliases'     => array('s', 'st'),
-        'description' => 'Show the router status'
+        'description' => 'Show the connection status'
+    )
+);
+$stCmd = $parser->addCommand(
+    'card',
+    array(
+        'aliases'     => array('c'),
+        'description' => 'Show the PC card/SIM status'
+    )
+);
+$stCmd = $parser->addCommand(
+    'all',
+    array(
+        'aliases'     => array('a'),
+        'description' => 'Show all status details'
+    )
+);
+$stCmd = $parser->addCommand(
+    'authstatus',
+    array(
+        'description' => 'Show the connection status the old way (authenticated)'
     )
 );
+
+
 $parser->addCommand(
     'reboot',
     array(
@@ -77,6 +105,12 @@ $parser->addCommand(
         'description' => 'Reboot the router'
     )
 );
+$stCmd = $parser->addCommand(
+    'saveConfig',
+    array(
+        'description' => 'Saves the router configuration into the config file'
+    )
+);
 
 try {
     $result = $parser->parse();
@@ -87,9 +121,16 @@ try {
 
 try {
     $router = new Wrt3g();
-    $router->host     = $result->options['host'];
-    $router->user     = $result->options['user'];
-    $router->password = $result->options['password'];
+    $router->verbosity = $result->options['verbosity'];
+    $router->loadConfig($result->options);
+
+    if ($result->options['dummy']) {
+        require_once 'Wrt3g/DummyRequest.php';
+        $router->requestClass = 'Wrt3g_DummyRequest';
+        $router->log('Using dummy data', 1);
+    }
+
+    $router->log('Command: ' . $result->command_name, 2);
 
     switch ($result->command_name) {
     case 'reboot':
@@ -100,11 +141,32 @@ try {
         }
         break;
 
+    case 'saveConfig':
+        $router->config->save($router->config->getConfigFilePath());
+        break;
+
+    case 'all':
+    case 'card':
     case 'status':
     default:
-        $arStatus = $router->getStatus();
+        if ($result->command_name == 'all') {
+            $arStatus = $router->getFullStatus();
+        } else if ($result->command_name == 'card') {
+            $arStatus = $router->getCardStatus();
+        } else if ($result->command_name == 'authstatus') {
+            $arStatus = $router->getConnectionStatusAuth();
+        } else {
+            $arStatus = $router->getConnectionStatus();
+        }
+
         foreach ($arStatus as $key => $value) {
-            echo $key . ': ' . $value . "\n";
+            echo $key . ': ';
+            if (is_array($value)) {
+                //session usage
+                echo var_export($value, true) . "\n";
+            } else {
+                echo $value . "\n";
+            }
         }
     }
 } catch (Exception $e) {