<?PHP
/* Copyright 2010, Lime Technology LLC.
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 */
/* Modifications made to GUI, Copyright 2011, Andrew Hamer-Adams. */
/* Adapted by Bergware International (May 2012) */
?>
<?
// Helper functions
function device_info(&$disk) {
  global $root, $path, $var, $sf, $display;
  $name = my_disk($disk['name']);
  $type = strpos($name,'Disk')===false ? $name : "Disk";
  $action = strpos($disk['color'],'blink')===false ? "down" : "up";
  $index = $disk['name']!='cache' ? $disk['idx'] : $disk['device'];
  if ($var['fsState']=="Started") {
    $a_ = "<a href=\"update.php?cmd=hdd&spin={$action}&disk={$index}\" target='progressFrame'>"; $_a = "</a>";
    $title = "Spin ".ucfirst($action);
  } else {
    $a_ = ""; $_a = "";
    $title = "Unavailable";
  }
  if ($display['spin']) {
    $spin_disk =  "{$a_}<img src='{$sf}/icons/{$action}.png' title='{$title}' class='icon gap'>{$_a}";
    $icon = "icon";
  } else {
    $spin_disk = "";
    $icon = "icon gap";
  }
  $blink = str_replace('on','blink',$disk['color']);
  $optional_blinking_indicator = strpos($disk['color'],'grey')===false ? "<img src='{$root}/style/{$blink}.gif' class='icon'>Disk spun-down<br>" : "";
  if ($name!="Flash") {
    $status = "<a href='#' class='info' onClick='return false'>
    <img src=\"{$root}/style/{$disk['color']}.gif\" class='$icon'><span>
    <img src='{$root}/style/green-on.gif' class='icon'>Normal operation<br>
    <img src='{$root}/style/yellow-on.gif' class='icon'>Invalid data content<br>
    <img src='{$root}/style/red-on.gif' class='icon'>Disabled disk<br>
    <img src='{$root}/style/blue-on.gif' class='icon'>New disk, not in array<br>
    <img src='{$root}/style/grey-off.gif' class='icon'>No disk present<br>
    {$optional_blinking_indicator}
    </span></a>{$spin_disk}";
  } else {
    $icon = $display['spin'] ? "icon wide" : "icon gap";
    $status = "<img src=\"{$root}/style/{$disk['color']}.gif\" class='$icon'>";
  }
  $link = strpos($disk['status'], "_NP")===false ? "<a href=\"{$path}/{$type}?name={$disk['name']}\">{$name}</a>" : $name;
  return $status.$link;
}
function device_browse(&$disk) {
  global $path, $sf;
  if ($disk['fsStatus']=="Mounted"):
    $dir = $disk['name']=="flash" ? "/boot" : "/mnt/{$disk['name']}";
    return "<a href='{$path}/Browse?dir={$dir}'><img src='{$sf}/icons/folder_explore.png' class='icon' title='Browse {$dir}'></a>";
  else:
    return "";
  endif;
}
function device_desc(&$disk) {
  global $display;
  return "{$disk['id']} ({$disk['device']})".($display['size'] ? " {$disk['size']}" : "");
}
function assignment(&$disk) {
  global $devs;
  echo "<form method='POST' name=\"{$disk['name']}Form\" action='/update.htm' target='progressFrame'><input type='hidden' name='changeDevice' value='Apply'>";
  echo "<select name=\"slotId.{$disk['idx']}\" onChange=\"{$disk['name']}Form.submit()\">";
  $empty = ($disk['idSb']!="" ? "no device" : "unassigned");
  if ($disk['id']!=""):
    echo "<option value=\"{$disk['id']}\" selected>".device_desc($disk)."</option>";
    echo "<option value=''>{$empty}</option>";
  else:
    echo "<option value='' selected>{$empty}</option>";
  endif;
  foreach ($devs as $dev):
    echo "<option value=\"{$dev['id']}\">".device_desc($dev)."</option>";
  endforeach;
  echo "</select></form>";
}
?>
<table class="disk_status <?=$display['view'].' '.$display['align']?>">
<tr>
<td>Device</td>
<td>Identification</td>
<td>Temp.</td>
<td>Size</td>
<td>Used</td>
<td>Free</td>
<td>Reads</td>
<td>Writes</td>
<td>Errors</td>
<td>View</td>
</tr>
<?
$tr_num=0; $dr=0; $dw=0; $de=0; $dt=0; $dd=0; $ts=0; $tu=0; $tf=0;
switch ($var['fsState']):
case "Stopped":
  foreach ($disks as $disk):
    if (isset($disk['fsFree'])) $disk['fsUsed'] = $disk['size'] - $disk['fsFree'];
    if ($disk['name']=="flash"):
?>    <tr class="tr_row<?=$tr_num++ & 1?>">
      <td><?=device_info($disk)?></td>
      <td><?=device_desc($disk)?></td>
      <td></td>
      <td><?=my_scale($disk['size']*1024, $units).' '.$units?></td>
      <td><?=$disk['fsStatus']=="Mounted" ? my_scale($disk['fsUsed']*1024, $units).' '.$units : $disk['fsStatus']?></td>
      <td><?=$disk['fsStatus']=="Mounted" ? my_scale($disk['fsFree']*1024, $units).' '.$units : ''?></td>
      <td><?=$disk['numReads']?></td>
      <td><?=$disk['numWrites']?></td>
      <td><?=$disk['numErrors']?></td>
      <td><?=device_browse($disk)?></td>
      </tr>
<?    continue;
    endif;
    switch ($disk['status']):
    case "DISK_NP":
?>    <tr class="tr_row<?=$tr_num++ & 1?>">
      <td><?=device_info($disk)?></td>
      <td colspan="9"><?=assignment($disk)?></td>
      </tr>
<?    break;
    case "DISK_OK":
?>    <tr class="tr_row<?=$tr_num++ & 1?>">
      <td><?=device_info($disk)?></td>
      <td><?=assignment($disk)?></td>
      <td><?=my_temp($disk['temp'])?></td>
      <td><?=my_scale($disk['size']*1024, $units).' '.$units?></td>
      <td>-</td>
      <td>-</td>
      <td>-</td>
      <td>-</td>
      <td>-</td>
      <td></td>
      </tr>
<?    break;
    case "DISK_INVALID":
?>    <tr class="tr_row<?=$tr_num++ & 1?>">
      <td><?=device_info($disk)?></td>
      <td><?=assignment($disk)?></td>
      <td><?=my_temp($disk['temp'])?></td>
      <td><?=my_scale($disk['size']*1024, $units).' '.$units?></td>
      <td>-</td>
      <td>-</td>
      <td>-</td>
      <td>-</td>
      <td>-</td>
      <td></td>
      </tr>
<?    break;
    case "DISK_DSBL":
?>    <tr class="tr_row<?=$tr_num++ & 1?>">
      <td><?=device_info($disk)?></td>
      <td><?=assignment($disk)?></td>
      <td><?=my_temp($disk['temp'])?></td>
      <td><?=my_scale($disk['size']*1024, $units).' '.$units?></td>
      <td>-</td>
      <td>-</td>
      <td>-</td>
      <td>-</td>
      <td>-</td>
      <td></td>
      </tr>
<?    break;
    case "DISK_DSBL_NP":
      if ($disk['name']=="parity"):
?>      <tr class="tr_row<?=$tr_num++ & 1?>">
        <td><?=device_info($disk)?></td>
        <td colspan="9"><?=assignment($disk)?></td>
        </tr>
<?    else:?>
        <tr class="tr_row<?=$tr_num++ & 1?>">
        <td><?=device_info($disk)?><br><em>Not installed</em></td>
        <td><?=assignment($disk)?><em><?=$disk['idSb']?></em></td>
        <td>-</td>
        <td><em><?=my_scale($disk['sizeSb']*1024, $units).' '.$units?></em></td>
        <td>-</td>
        <td>-</td>
        <td>-</td>
        <td>-</td>
        <td>-</td>
        <td></td>
        </tr>
<?    endif;
      break;
    case "DISK_DSBL_NEW":
?>    <tr class="tr_row<?=$tr_num++ & 1?>">
      <td><?=device_info($disk)?></td>
      <td><?=assignment($disk)?></td>
      <td><?=my_temp($disk['temp'])?></td>
      <td><?=my_scale($disk['size']*1024, $units).' '.$units?></td>
      <td>-</td>
      <td>-</td>
      <td>-</td>
      <td>-</td>
      <td>-</td>
      <td></td>
      </tr>
<?    break;
    case "DISK_NP_MISSING":
?>    <tr class="tr_row<?=$tr_num++ & 1?>">
      <td><?=device_info($disk)?><br><em>Missing</em></td>
      <td><?=assignment($disk)?><em><?=$disk['idSb']?></em></td>
      <td>-<br>&nbsp;</td>
      <td>-<br><em><?=my_scale($disk['sizeSb']*1024, $units).' '.$units?></em></td>
      <td>-<br>&nbsp;</td>
      <td>-<br>&nbsp;</td>
      <td>-<br>&nbsp;</td>
      <td>-<br>&nbsp;</td>
      <td>-<br>&nbsp;</td>
      <td></td>
      </tr>
<?    break;
    case "DISK_WRONG":
?>    <tr class="tr_row<?=$tr_num++ & 1?>">
      <td><?=device_info($disk)?><br><em>Wrong</em></td>
      <td><?=assignment($disk)?><em><?=$disk['idSb']?></em></td>
      <td><?=my_temp($disk['temp'])?><br>&nbsp;</td>
      <td><?=my_scale($disk['size']*1024, $units).' '.$units?><br><em><?=my_scale($disk['sizeSb']*1024, $units).' '.$units?></em></td>
      <td>-<br>&nbsp;</td>
      <td>-<br>&nbsp;</td>
      <td>-<br>&nbsp;</td>
      <td>-<br>&nbsp;</td>
      <td>-<br>&nbsp;</td>
      <td></td>
      </tr>
<?    break;
    case "DISK_NEW":
?>    <tr class="tr_row<?=$tr_num++ & 1?>">
      <td><?=device_info($disk)?></td>
      <td><?=assignment($disk)?></td>
      <td><?=my_temp($disk['temp'])?></td>
      <td><?=my_scale($disk['size']*1024, $units).' '.$units?></td>
      <td>-</td>
      <td>-</td>
      <td>-</td>
      <td>-</td>
      <td>-</td>
      <td></td>
      </tr>
<?    break;
    endswitch;
  endforeach;
  break;
default:
  foreach ($disks as $disk):
    if (isset($disk['fsFree'])) $disk['fsUsed'] = $disk['size'] - $disk['fsFree'];
    $none = $disk['name']=="parity" ? '-' : '';
    if ($disk['name']=="flash"):
      if ($display['total']):
        $icon = $display['spin'] ? "icon wide" : "icon gap";
?>      <tr class="tr_last">
        <td><img src='<?=$sf?>/icons/task.png' class='<?=$icon?>'>Total</td>
        <td><?=my_count($var['mdNumProtected'])?></td>
        <td><?=$dd>0?my_temp(round($dt/$dd, 1)):"*"?></td>
        <td><?=my_scale($ts*1024, $units).' '.$units?></td>
        <td><?=my_scale($tu*1024, $units).' '.$units?></td>
        <td><?=my_scale($tf*1024, $units).' '.$units?></td>
        <td><?=my_number($dr)?></td>
        <td><?=my_number($dw)?></td>
        <td><?=my_number($de)?></td>
        <td></td>
        </tr>
<?    endif;?>
      <tr class="tr_row<?=$tr_num++ & 1?>">
      <td><?=device_info($disk)?></td>
      <td><?=device_desc($disk)?></td>
      <td></td>
      <td><?=my_scale($disk['size']*1024, $units).' '.$units?></td>
      <td><?=$disk['fsStatus']=="Mounted" ? my_scale($disk['fsUsed']*1024, $units).' '.$units : $disk['fsStatus']?></td>
      <td><?=$disk['fsStatus']=="Mounted" ? my_scale($disk['fsFree']*1024, $units).' '.$units : ''?></td>
      <td><?=my_number($disk['numReads'])?></td>
      <td><?=my_number($disk['numWrites'])?></td>
      <td><?=my_number($disk['numErrors'])?></td>
      <td><?=device_browse($disk)?></td>
      </tr>
<?    continue;
    endif;
    switch ($disk['status']):
    case "DISK_NP":
?>    <tr class="tr_row<?=$tr_num++ & 1?>">
      <td><?=device_info($disk)?></td>
      <td colspan="9">Not installed</td>
      </tr>
<?    break;
    case "DISK_OK":
?>    <tr class="tr_row<?=$tr_num++ & 1?>">
      <td><?=device_info($disk)?></td>
      <td><?=device_desc($disk)?></td>
      <td><?=my_temp($disk['temp'])?></td>
      <td><?=my_scale($disk['size']*1024, $units).' '.$units?></td>
      <td><?=$disk['fsStatus']=="Mounted" ? my_scale($disk['fsUsed']*1024, $units).' '.$units : $disk['fsStatus']?></td>
      <td><?=$disk['fsStatus']=="Mounted" ? my_scale($disk['fsFree']*1024, $units).' '.$units : $none?></td>
      <td><?=my_number($disk['numReads'])?></td>
      <td><?=my_number($disk['numWrites'])?></td>
      <td><?=my_number($disk['numErrors'])?></td>
      <td><?=device_browse($disk)?></td>
      </tr>
<?    break;
    case "DISK_INVALID":
?>    <tr class="tr_row<?=$tr_num++ & 1?>">
      <td><?=device_info($disk)?></td>
      <td><?=device_desc($disk)?></td>
      <td><?=my_temp($disk['temp'])?></td>
      <td><?=my_scale($disk['size']*1024, $units).' '.$units?></td>
      <td><?=$disk['fsStatus']=="Mounted" ? my_scale($disk['fsUsed']*1024, $units).' '.$units : $disk['fsStatus']?></td>
      <td><?=$disk['fsStatus']=="Mounted" ? my_scale($disk['fsFree']*1024, $units).' '.$units : $none?></td>
      <td><?=my_number($disk['numReads'])?></td>
      <td><?=my_number($disk['numWrites'])?></td>
      <td><?=my_number($disk['numErrors'])?></td>
      <td><?=device_browse($disk)?></td>
      </tr>
<?    break;
    case "DISK_DSBL":
?>    <tr class="tr_row<?=$tr_num++ & 1?>">
      <td><?=device_info($disk)?></td>
      <td><?=device_desc($disk)?></td>
      <td><?=my_temp($disk['temp'])?></td>
      <td><?=my_scale($disk['size']*1024, $units).' '.$units?></td>
      <td><?=$disk['fsStatus']=="Mounted" ? my_scale($disk['fsUsed']*1024, $units).' '.$units : $disk['fsStatus']?></td>
      <td><?=$disk['fsStatus']=="Mounted" ? my_scale($disk['fsFree']*1024, $units).' '.$units : $none?></td>
      <td><?=my_number($disk['numReads'])?></td>
      <td><?=my_number($disk['numWrites'])?></td>
      <td><?=my_number($disk['numErrors'])?></td>
      <td><?=device_browse($disk)?></td>
      </tr>
<?    break;
    case "DISK_DSBL_NP":
      if ($disk['name']=="parity"):
?>      <tr class="tr_row<?=$tr_num++ & 1?>">
        <td><?=device_info($disk)?></td>
        <td colspan="9">Not installed</td>
        </tr>
<?    else:?>
        <tr class="tr_row<?=$tr_num++ & 1?>">
        <td><?=device_info($disk)?></td>
        <td><em>Not installed</em></td>
        <td>-</td>
        <td><em><?=my_scale($disk['sizeSb']*1024, $units).' '.$units?></em></td>
        <td><em><?=$disk['fsStatus']=="Mounted" ? my_scale($disk['fsUsed']*1024, $units).' '.$units : $disk['fsStatus']?></em></td>
        <td><em><?=$disk['fsStatus']=="Mounted" ? my_scale($disk['fsFree']*1024, $units).' '.$units : $none?></em></td>
        <td>-</td>
        <td>-</td>
        <td>-</td>
        <td></td>
        </tr>
<?    endif;
      break;
    endswitch;
    if (is_numeric($disk['temp'])):
      $dt += $disk['temp'];
      $dd += 1;
    endif;
    if (strpos($disk['name'],'disk')!==false):
      $ts += $disk['size'];
      $tu += $disk['fsUsed'];
      $tf += $disk['fsFree'];
    endif;
    $dr += $disk['numReads'];
    $dw += $disk['numWrites'];
    $de += $disk['numErrors'];
  endforeach;
endswitch;
?>
</table>