<?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.
 */
?>
<?
function write_cfg_file($file, $array, $mode) {
  if ($mode!=1) return;
  $content = "# Generated settings:\n";
  foreach ($array as $section => $block) {
    foreach ($block as $key => $val) if ($val!=$section) $content .= "$section.$key=\"$val\"\n";
  }
  file_put_contents($file, $content);
}

function write_ini_file($file, $array, $mode) {
  if ($mode==0) return;
  $content = "";
  foreach ($array as $section => $block) {
    $content .= "[$section]\n";
    foreach ($block as $key => $val) if ($val!=$section) $content .= "$key=\"$val\"\n";
  }
  file_put_contents($file, $content);
}

parse_str($argv[1],$_POST);
switch (isset($_POST['cmd']) ? $_POST['cmd'] : 'default') {
case 'hdd':
  $spin = $_POST['spin'];
  $disk = $_POST['disk'];
  echo "<div style='position:fixed;top:0;font-family:monospace;font-size:small;'>Spinning $spin: ";
  if (is_numeric($disk)) {
    echo "disk $disk...</div>";
    shell_exec("/root/mdcmd spin$spin $disk");
  } else {
    echo "cache drive...</div>";
    $cmd = $spin=='down' ? 'hdparm -y' : 'smartctl -H';
    shell_exec("$cmd /dev/$disk");
  }
  break;
case 'export':
// To be implemented
  break;
default:
  $section = $_POST['section'];
  $ini = "/var/local/emhttp/simpleFeatures.ini";
  $cfg = "/boot/config/simpleFeatures.cfg";
  $keys = parse_ini_file($ini, true);
  $save = 0;
  foreach ($_POST as $key => $value) {
    switch ($key) {
    case 'mode':
      $temp = '/tmp/cron.tmp';
      file_put_contents($temp, $argv[1]);
      break;
    case 'parity':
      $crontab = '/tmp/parity-check.tmp'; $cron = '';
      exec("crontab -l | grep -vi 'Scheduled Parity Check' | grep -v '/root/mdcmd check'> $crontab");
      if ($_POST['mode']>0) {
        $hour = isset($_POST['hour']) ? $_POST['hour'] : '* *';
        $dotm = isset($_POST['dotm']) ? $_POST['dotm'] : '*';
        switch ($dotm) {
        case '28-31':
          $term = '[ $(date +%d -d tomorrow) -eq 1 ] && ';
          break;
        case 'W1':
          $dotm = '*';
          $term = '[ $(date +%d) -le 7 ] && ';
          break;
        case 'W2':
          $dotm = '*';
          $term = '[ $(date +%d) -ge 8 -a $(date +%d) -le 14 ] && ';
          break;
        case 'W3':
          $dotm = '*';
          $term = '[ $(date +%d) -ge 15 -a $(date +%d) -le 21 ] && ';
          break;
        case 'W4':
          $dotm = '*';
          $term = '[ $(date +%d) -ge 22 -a $(date +%d) -le 28 ] && ';
          break;
        case 'WL':
          $dotm = '*';
          $term = '[ $(date +%d -d +7days) -le 7 ] && ';
          break;
        default:
          $term = '';
        }
        $month = isset($_POST['month']) ? $_POST['month'] : '*';
        $day = isset($_POST['day']) ? $_POST['day'] : '*';
        $write = $_POST['write'];
        $cron = "'$hour $dotm $month $day $term/root/mdcmd check $write 1>/dev/null 2>&1'";
        exec("echo '# Scheduled Parity Check' >> $crontab");
        exec("echo $cron >> $crontab");
      }
      exec("crontab $crontab");
      exec("rm $crontab");
      exec("rm -f $temp");
      $keys[$section]['cron'] = $cron;
      $save = 1;
      break;
    case 's3_sleep':
      $preRun = "/usr/local/bin/preRun";
      $postRun = "/usr/local/bin/postRun";
      exec("rm -f $preRun");
      exec("rm -f $postRun");
    case 'cache_dirs':
      $go_old = '/boot/config/go';
      $go_new = '/tmp/go.tmp';
      $options = '';
      foreach ($_POST as $option => $val) {
        if ($option==$key || !strlen($val)) continue;
        switch ($option) {
        case 'section':
          break;
        case 'dirs':
        case 's3':
          $enable = $val;
          break;
        case 'script':
          $script = $val;
          break;
        case 'preRun':
          file_put_contents($preRun, "#!/bin/bash\n".str_replace("\r\n","\n",$val));
          exec("chmod u+x $preRun");
          $keys[$section][$option] = urlencode($val);
          $options .= " -b $preRun";
          continue;
        case 'postRun':
          file_put_contents($postRun, "#!/bin/bash\n".str_replace("\r\n","\n",$val));
          exec("chmod u+x $postRun");
          $keys[$section][$option] = urlencode($val);
          $options .= " -p $postRun";
          continue;
        case 'exclude':
        case 'include': 
        case 'pingIP':
        case 'stopDay':
        case 'stopHour':
          $list = explode(',', $val);
          foreach ($list as $insert) $options .= " ".(isset($_POST[$option.'_opt']) ? $_POST[$option.'_opt'] : "").$insert;
          break;
        default:
          if (!strpos($option,'_opt')) $options .= " ".(isset($_POST[$option.'_opt']) ? $_POST[$option.'_opt'] : "").$val;
          break;
        }
      }
      exec("$script -q 1>/dev/null 2>&1");
      exec("grep -v $key $go_old > $go_new");
      if ($enable) {
        exec("echo $script$options >> $go_new");
        exec("$script$options 1>/dev/null 2>&1");
      }
      exec("mv -f $go_new $go_old");
      $save = 1;
      break;
    default:
      if ($value=='Apply') $save = 1;
      elseif ($value=='Done') $save = 2;
      break;
    }
    if ($save==0) $keys[$section][$key] = $value;
  }
  write_ini_file($ini, $keys, $save);
  write_cfg_file($cfg, $keys, $save);
}
?>
<html>
<head><script>var goback=parent.location;</script></head>
<body onLoad="parent.location=goback;"></body>
</html>