sleep before reconnecting
[auerswald-callnotifier.git] / src / callnotifier / CLI.php
index 55f7f71847aafd812809f3d70dec5a0f1da858fb..0b8ae60773c1cac7a48ca2dfa51776aa4b9bb7b2 100644 (file)
@@ -5,7 +5,6 @@ namespace callnotifier;
 class CLI
 {
     protected $cliParser;
-    protected $config;
 
     public function __construct()
     {
@@ -14,31 +13,49 @@ class CLI
 
     public function run()
     {
-        $this->config = new Config();
+        $config = new Config();
         try {
             $result = $this->cliParser->parse();
         } catch (\Exception $exc) {
             $this->cliParser->displayError($exc->getMessage());
         }
 
-        $this->fillConfig($this->config, $result);
+        $this->fillConfig($config, $result);
 
-        $handler = new MessageHandler($this->config);
+        $log = new Log();
         if ($result->options['debug'] || $result->options['debugEdss1']) {
             $debugLogger = new Logger_Debug();
-            $handler->addLogger($debugLogger, '*');
+            $log->addLogger($debugLogger, '*');
             if ($result->options['debugEdss1']) {
                 $debugLogger->edss1MsgOnly = true;
             }
         }
 
-        if ($this->config->replayFile !== null) {
+        $callMonitor = new CallMonitor($config, $log);
+
+        $configFile = $this->getConfigFile();
+        if ($configFile !== null) {
+            include $configFile;
+        }
+
+        $handler = new MessageHandler($config, $log, $callMonitor);
+
+        if ($config->replayFile !== null) {
             $sourceClass = 'callnotifier\Source_File';
         } else {
             $sourceClass = 'callnotifier\Source_Remote';
         }
-        $source = new $sourceClass($this->config, $handler);
-        $source->run();
+
+        try {
+            $source = new $sourceClass($config, $handler);
+            $source->run();
+        } catch (\Exception $e) {
+            $msg = 'Callnotifier error!' . "\n"
+                . 'Code: ' . $e->getCode() . "\n"
+                . 'Message: ' . $e->getMessage() . "\n";
+            file_put_contents('php://stderr', $msg);
+            exit(1);
+        }
     }
 
     public function setupCli()
@@ -62,7 +79,7 @@ class CLI
             'dumpFile',
             array(
                 'long_name'   => '--dump',
-                'description' => 'dump messages into file for later replay',
+                'description' => 'Dump messages into file for later replay',
                 'help_name'   => 'FILE',
                 'action'      => 'StoreString'
             )
@@ -105,6 +122,39 @@ class CLI
         $config->setIfNotEmpty('dumpFile', $result->options['dumpFile']);
         $config->setIfNotEmpty('replayFile', $result->options['replayFile']);
     }
+
+    /**
+     * Finds the path to the configuration file.
+     *
+     * The following locations are tried:
+     * - Git checkout: data/callnotifier.config.php
+     * - ~/.config/callnotifier.config.php
+     * - /etc/callnotifier.config.php
+     *
+     * @return string Full path of config file or NULL if no file found
+     */
+    protected function getConfigFile()
+    {
+        if (basename(dirname(__DIR__)) == 'src'
+            && file_exists(__DIR__ . '/../../data/callnotifier.config.php')
+        ) {
+            return __DIR__ . '/../../data/callnotifier.config.php';
+        }
+
+        if (isset($_ENV['HOME'])) {
+            $file = $_ENV['HOME'] . '/.config/callnotifier.config.php';
+            if (file_exists($file)) {
+                return $file;
+            }
+        }
+
+        $file = '/etc/callnotifier.config.php';
+        if (file_exists($file)) {
+            return $file;
+        }
+
+        return null;
+    }
 }
 
 ?>
\ No newline at end of file