#!/usr/bin/php -q
<?PHP
/* Copyright 2005-2021, Lime Technology
 * Copyright 2012-2021, Bergware International.
 * Copyright 2015, Dan Landon.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
?>
<?
session_start();
$docroot  = '/usr/local/emhttp';

require_once "$docroot/webGui/include/Wrappers.php";
require_once "$docroot/webGui/include/publish.php";
extract(parse_plugin_cfg('dynamix',true));

// add translations
$_SERVER['REQUEST_URI'] = 'settings';
$_SESSION['locale'] = $display['locale'];
require_once "$docroot/webGui/include/Translations.php";
// remember current language
$locale_init = $locale;

// close session, it is not needed anymore
session_unset();
session_destroy();

$state = [
  'TRIM ONLINE'  => 'Online (trim)',
  'BOOST ONLINE' => 'Online (boost)',
  'ONLINE'       => 'Online',
  'ONBATT'       => 'On battery',
  'COMMLOST'     => 'Lost communication',
  'NOBATT'       => 'No battery detected'
];

$red    = "class='red-text'";
$green  = "class='green-text'";
$orange = "class='orange-text'";

function update_translation($locale) {
  global $docroot,$language;
  $language = [];
  if ($locale) {
    $text = "$docroot/languages/$locale/translations.txt";
    if (file_exists($text)) {
      $store = "$docroot/languages/$locale/translations.dot";
      if (!file_exists($store)) file_put_contents($store,serialize(parse_lang_file($text)));
      $language = unserialize(file_get_contents($store));
    }
    $text = "$docroot/languages/$locale/settings.txt";
    if (file_exists($text)) {
      $store = "$docroot/languages/$locale/settings.dot";
      if (!file_exists($store)) file_put_contents($store,serialize(parse_lang_file($text)));
      $language = array_merge($language,unserialize(file_get_contents($store)));
    }
  }
}
while (true) {
  $status = array_fill(0,6,"<td>-</td>");
  $power = $load = false;
  // check for language changes
  extract(parse_plugin_cfg('dynamix',true));
  if ($display['locale'] != $locale_init) {
    $locale_init = $display['locale'];
    update_translation($locale_init);
  }
  if (file_exists("/var/run/apcupsd.pid")) {
    exec("/sbin/apcaccess 2>/dev/null", $rows);
    for ($i=0; $i<count($rows); $i++) {
      $row = array_map('trim', explode(':', $rows[$i], 2));
      $key = $row[0];
      $val = strtr($row[1], $state);
      switch ($key) {
      case 'STATUS':
        $status[0] = $val ? (stripos($val,'online')===false ? "<td $red>$val</td>" : "<td $green>$val</td>") : "<td $orange>"._('Refreshing')."...</td>";
        break;
      case 'BCHARGE':
        $status[1] = strtok($val,' ')<=10 ? "<td $red>$val</td>" : "<td $green>$val</td>";
        break;
      case 'TIMELEFT':
        $status[2] = strtok($val,' ')<=5 ? "<td $red>$val</td>" : "<td $green>$val</td>";
        break;
      case 'NOMPOWER':
        $power = strtok($val,' ');
        $status[3] = $power==0 ? "<td $red>$val</td>" : "<td $green>$val</td>";
        break;
      case 'LOADPCT':
        $load = strtok($val,' ');
        $status[5] = $load>=90 ? "<td $red>$val</td>" : "<td $green>$val</td>";
        break;
      }
    }
    if ($power && $load) $status[4] = ($load>=90 ? "<td $red>" : "<td $green>").intval($power*$load/100)." "._('Watts')."</td>";
  }

  publish('apcups',"<tr>".implode('', $status)."</tr>");
  sleep(5);
}
?>
