<?PHP
/* Copyright 2012, Bergware International.
 * 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.
 */
?>
<?
$sleep = &$sf_ini['sleep'];
$sName = "s3_sleep";
$script = "/usr/local/sbin/$sName";
$sFind = exec("find /boot -maxdepth 3 -name $sName");
if (strlen($sFind)) {
  $sCurrent = exec("$script -V | cut -f3 -d' '");
  $sVersion = exec("$sFind -V | cut -f3 -d' '");
  if ($sVersion > $sCurrent) $script = $sFind;
}
unset($sPorts);
exec("ifconfig -s | grep eth | cut -d' ' -f1", &$sPorts);
$sTimeout = 30;
$sInterval = 5;
$bwm_ng = is_file("/usr/local/sbin/bwm-ng") ? '0' : '1';
$days = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
?>
<form name="sleep_settings" method="POST" action="/update.php" target="progressFrame" onsubmit="prepareSleep(this)">
<input type="hidden" name="section" value="sleep">
<input type="hidden" name="script" value="<?=$script?>">
<input type="hidden" name="timeout_opt" value="-m">
<input type="hidden" name="interval_opt" value="-t">
<input type="hidden" name="eth_opt" value="-e">
<input type="hidden" name="pingIP_opt" value="-i">
<input type="hidden" name="stopHour_opt" value="-h">
<input type="hidden" name="stopDay_opt" value="-d">
<input type="hidden" name="setWol_opt" value="-w">
<table class="settings">
  <tr>
  <td>S3 sleep function:</td>
  <td><select name="s3" size="1" onchange="presetSleep(this.form);">
  <?=mk_option($sleep['s3'], "0", "Disabled")?>
  <?=mk_option($sleep['s3'], "1", "Enabled")?>
  </select>(<?=exec("$script -V")?>)</td>
  <td><span class="status">Status:
<?if (exec("ps -e | grep -m 1 '$sName' | awk '{print $4}'")):?>
  <span class="green">Running</span>
<?else:?>
  <span class="orange">Stopped</span>
<?endif;?>
  </span></td>
  </tr>
  <tr>
  <td>Wait for array inactivity:</td>
  <td><select name="checkHDD" size="1">
  <?=mk_option($sleep['checkHDD'], "", "No")?>
  <?=mk_option($sleep['checkHDD'], "-a", "Yes")?>
  </select></td>
  </tr>
  <tr>
  <td>Extra delay after array inactivity (minutes):</td>
  <td><input type="text" name="timeout" maxlength="2" value="<?=$sleep['timeout']?>"><?=$sleep['timeout']==$sTimeout?'default':'user-set'?></td>
  </tr>
  <tr>
  <td>Interval of network / device inactivity (minutes):</td>
  <td><input type="text" name="interval" maxlength="2" value="<?=$sleep['interval']?>"><?=$sleep['interval']==$sInterval?'default':'user-set'?></td>
  </tr>
  <tr>
  <td>Ethernet interface:</td>
  <td><select name="eth" size="1">
<?foreach ($sPorts as $eth):?>
  <?=mk_option_check($sleep['eth'], $eth, $eth)?>
<?endforeach;?>
  </select></td>
  </tr>
  <tr>
  <td>Wait for network inactivity:</td>
  <td><select name="checkTCP" size="1">
  <?=mk_option($sleep['checkTCP'], "", "No")?>
  <?=mk_option($sleep['checkTCP'], "-n", "Yes")?>
  </select><? if ($bwm_ng=='1') echo "BWM-NG is not installed"; ?></td>
  </tr>
  <tr>
  <td>Wait for device inactivity (address):</td>
  <td><input type="text" name="pingIP" maxlength="200" value="<?=$sleep['pingIP']?>">(e.g. 192.168.1.2)</td>
  </tr>
  <td>Excluded hours:</td>
  <td><select id="s2" name="stopHour" size="1" multiple="multiple" style="display:none">
  <option value='' selected></option>
<?for ($h=0; $h<24; $h++):?>
  <?=mk_option_check($sleep['stopHour'], sprintf("%02d", $h), sprintf("%02d:00 - %02d:59", $h, $h))?>
<?endfor;?>
  </select></td>
  </tr>
  <tr>
  <td>Excluded days:</td>
  <td><select id="s1" name="stopDay" size="1" multiple="multiple" style="display:none">
  <option value='' selected></option>
<?for ($d=0; $d<count($days); $d++):?>
  <?=mk_option_check($sleep['stopDay'], strval($d), $days[$d])?>
<?endfor;?>
  </select></td>
  </tr>
  <tr>
  <td>DHCP renewal after wake-up:</td>
  <td><select name="dhcpRenew" size="1">
  <?=mk_option($sleep['dhcpRenew'], "", "No")?>
  <?=mk_option($sleep['dhcpRenew'], "-R", "Yes")?>
  </select></td>
  </tr>
  <tr>
  <td>Force gigabit speed after wake-up:</td>
  <td><select name="forceGb" size="1">
  <?=mk_option($sleep['forceGb'], "", "No")?>
  <?=mk_option($sleep['forceGb'], "-F", "Yes")?>
  </select></td>
  </tr>
  <tr>
  <td>Set WOL options before sleep:</td>
  <?unset($wakeon); exec("ethtool {$sleep['eth']} | grep 'Wake-on' | cut -d':' -f2", &$wakeon)?>
  <td><input type="text" name="setWol" maxlength="8" value="<?=$sleep['setWol']?>"> Wake-on: <strong><?=$wakeon[1]?></strong> (available options: <strong><?=$wakeon[0]?></strong>)</td>
  </tr>
  <tr>
  <td>Custom commands before sleep:</td>
  <td><textarea name="preRun" rows="3" columns="120" wrap="off"><?=urldecode($sleep['preRun'])?></textarea></td>
  </tr>
  <tr>
  <td>Custom commands after wake-up:</td>
  <td><textarea name="postRun" rows="3" columns="120" wrap="off"><?=urldecode($sleep['postRun'])?></textarea></td>
  </tr>
  <tr>
  <td><button type="button" onclick="resetSleep(this.form);">Default</button></td>
  <td><input type="submit" name="s3_sleep" value="Apply"><button type="button" onclick="done();">Done</button></td>
  </tr>
</table>
</form>

<script>
$(document).ready(function() {
  $("#s1").dropdownchecklist({emptyText:'None', width:300, explicitClose:'...close'});
  $("#s2").dropdownchecklist({emptyText:'None', width:300, explicitClose:'...close'});
  presetSleep(document.sleep_settings);
});

function prepareSleep(form) {
  var days = '';
  for (var i=0,item; item=form.stopDay.options[i]; i++) {
    if (item.selected) {
      if (days.length) days += ',';
      days += item.value;
      item.selected = false;
    }
  }
  item = form.stopDay.options[0];
  item.value = days;
  item.selected = true;
  var hours = '';
  for (var i=0,item; item=form.stopHour.options[i]; i++) {
    if (item.selected) {
      if (hours.length) hours += ',';
      hours += item.value;
      item.selected = false;
    }
  }
  item = form.stopHour.options[0];
  item.value = hours;
  item.selected = true;
  form.pingIP.value = form.pingIP.value.replace(/,/g,' ').replace(/\s+/g,',');
  if (!form.timeout.value || isNaN(form.timeout.value)) form.timeout.value = <?=$sTimeout?>;
  if (!form.interval.value || isNaN(form.interval.value)) form.interval.value = <?=$sInterval?>;
}

function presetSleep(form) {
  var disabled = form.s3.value==0;
  var onOff = disabled ? 'disable' : 'enable';
  form.checkHDD.disabled = disabled;
  form.checkTCP.disabled = disabled || <?=$bwm_ng?>;
  form.timeout.disabled = disabled;
  form.eth.disabled = disabled;
  form.interval.disabled = disabled;
  form.pingIP.disabled = disabled;
  form.stopHour.disabled = disabled;
  form.dhcpRenew.disabled = disabled;
  form.forceGb.disabled = disabled;
  form.setWol.disabled = disabled;
  form.preRun.disabled = disabled;
  form.postRun.disabled = disabled;
  $("#s1").dropdownchecklist(onOff);
  $("#s2").dropdownchecklist(onOff);
  if (!form.timeout.value) form.timeout.value = <?=$sTimeout?>;
  if (!form.interval.value) form.interval.value = <?=$sInterval?>;
}
function resetSleep(form) {
  var s3 = form.s3.selectedIndex;
  form.innerHTML = form.innerHTML.replace(/user-set/g,'default');
  form.s3.selectedIndex = s3;
  form.checkHDD.selectedIndex = 1;
  form.timeout.value = <?=$sTimeout?>;
  form.interval.value = <?=$sInterval?>;
  form.eth.selectedIndex = 0;
  form.checkTCP.selectedIndex = 0;
  form.pingIP.value = "";
  form.stopHour.value = "";
  form.stopDay.value = "";
  form.dhcpRenew.selectedIndex = 0;
  form.forceGb.selectedIndex = 0;
  form.setWol.value = "";
  form.preRun.value = "";
  form.postRun.value = "";
}
</script>