<?php
session_start();
session_write_close();
ignore_user_abort(false);
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

$filename = "index.html";
while(True){
  if ( connection_aborted() ){
    exit();
  }
  else{
    try{
        $string = file_get_contents("https://golem.fjfi.cvut.cz/current_status.json");
        $json = json_decode($string, true);
        $pom1 = $json["tokamak_state"];

        $t1 = shell_exec("date -r index.html");
        sleep(3);
        $pom2 = $json["tokamak_state"];
        if($t1 != shell_exec("date -r index.html"))
        {
            sleep(2);
            echo "data: file changed \n\n";
        }
        else if($pom2=="GetReadyTheDischarge@Everywhere") // and $pom1!=$pom2
        {
            sleep(2);
            echo "data: new shot available \n\n";
        }
        else{
            echo ": heartbeat\n\n";
        }
        ob_flush();
        flush();
    }
    catch (\Error $e){
        echo "data: error, json not available \n\n";
    }
  }
}
ob_end_flush()
?>
