<!DOCTYPE html>
<!--
<?php
function get_command_list($command_file_name){
  echo "Getting commands from file $command_file_name...\n";
  $command_file = fopen($command_file_name, "r") or die("Could not read file $command_file_name. Aborting.\n");
  $commands = explode(PHP_EOL, fread($command_file, filesize($command_file_name)));
  fclose($command_file);
  echo "Done.\n";
  return $commands;
}

$status_commands = get_command_list("commands_status.conf");
$ondemand_commands = get_command_list("commands_ondemand.conf");
$git_commands = get_command_list("commands_git.conf");
?>
-->
<html>
  <head>
    <title> Strassenkind Server Status Page </title>
    <link rel="stylesheet" type="text/css" href="style.css" />
    <?php
    if (!isset($_POST['ondemand']) && !isset($_POST['git'])) {
      echo "<meta http-equiv=\"refresh\" content=\"20\" />";
    }
    ?>
  </head>
  <body class="status_page">
    <h1> Strassenkind Server Status </h1>
    <hr />
    <?php
    function add_button($button_text, $button_action){
      if (!isset($_POST[$button_action])) {
        echo "<form action=\"\" method=\"post\"><input type=\"submit\" value=\"$button_text\" name=\"$button_action\" /></form>";
      } else {
        echo "<a href=\"\"><button> Return to regular status commands </button></a>";
      }
    }

    add_button("Run ondemand commands", "ondemand");
    add_button("Force ThreadR update", "git");
    ?>
    <hr />
    <?php
    function execute_command_list($commands){
      foreach ($commands as $command) {
        if ($command != ""){
          // === is here on purpose.
          if (strpos($command, '>') === false) {
            $command = $command . " 2>&1";
          }
          $output = shell_exec($command);
          echo "<pre class=\"commandLine\"> " . htmlspecialchars($command, $flags=ENT_DISALLOWED | ENT_HTML5 | ENT_QUOTES) . " </pre>\n<pre class=\"commandLine\"> " . htmlspecialchars($output, $flags=ENT_DISALLOWED | ENT_HTML5 | ENT_QUOTES) . " </pre>\n<hr />";
        }
      }

    }

    // run ondemand command list if demanded
    if (isset($_POST['ondemand'])) {
      execute_command_list($ondemand_commands);
    } elseif (isset($_POST['git'])) {
      execute_command_list($git_commands);
    } else {
      //run status command list otherwise
      execute_command_list($status_commands);
    }
    ?>
  </body>
</html>