<?php
	$tmpPath = "/tmp/";
	$killLog = "/Users/shughey/Dropbox/FirefoxKillLog.log";
	
	$output = shell_exec("/usr/bin/pmset -g assertions");
	$lines = explode("\n", $output);
	// print $output;

	$userIsActive = false;
	$x = 0;
	$preventSleep = false;
	$isFirefox = false;
	$firefoxProc = [];
	
	while ($x < count($lines)) {
		$thisLine = $lines[$x];
		if (strpos($thisLine, "UserIsActive                   1") != false) $userIsActive = true;
		if (strpos($thisLine, "PreventUserIdleSystemSleep named") != false) {
			$preventSleep = true;
			echo "Prevent sleep!\n{$thisLine}\n" . $lines[$x+1] . "\n";
			preg_match("/Created for PID: ([0-9]{1,7})/", $lines[$x+1], $gr);
			// print_r($gr);
			if (is_array($gr)) {
				$proc = $gr[1];
				if ($proc) {
					$psout = shell_exec("/bin/ps -p{$proc}" );
					if (strpos($psout, "Firefox.app") != false) {
						echo "Firefox process ID: {$proc}\n";
						$isFirefox = true;
						$firefoxProc[] = $proc;
					}
				}
			}
		} else if (strpos($thisLine, "SystemIsActive named: \"News") != false) {
			$preventSleep = true;
			echo "Prevent sleep!\n{$thisLine}\n" . $lines[$x+1] . "\n";
			preg_match("/Created for PID: ([0-9]{1,7})/", $lines[$x+1], $gr);
			// print_r($gr);
			if (is_array($gr)) {
				$proc = $gr[1];
				if ($proc) {
					$psout = shell_exec("/bin/ps -p{$proc}" );
					if (strpos($psout, "News.app") != false) {
						echo "News process ID: {$proc}\n";
						$isFirefox = true;
						$firefoxProc[] = $proc;
					}
				}
			}
		} else if (strpos($thisLine, "NoIdleSleepAssertion named: \"Playing audio") != false) {
			$preventSleep = true;
			echo "Prevent sleep!\n{$thisLine}\n" . $lines[$x+1] . "\n";
			preg_match("/Created for PID: ([0-9]{1,7})/", $lines[$x+1], $gr);
			// print_r($gr);
			if (is_array($gr)) {
				$proc = $gr[1];
				if ($proc) {
					$psout = shell_exec("/bin/ps -p{$proc}" );
					if (strpos($psout, "Vivaldi.app") != false) {
						echo "Vivaldi process ID: {$proc}\n";
						$isFirefox = true;
						$firefoxProc[] = $proc;
					}
				}
			}
		}
		$x++;
	}

	// TESTING
	// $userIsActive = false;

	$file = "{$tmpPath}sleep-mon-{$firefoxProc}";

	if ($userIsActive) {
		echo "User is active!\n";
		if (file_exists($file)) unlink($file);
	}


	if (!$userIsActive && $preventSleep && $isFirefox) {
		foreach($firefoxProc as $thisProc) {
			$file = "{$tmpPath}sleep-mon-{$thisProc}";
			if (file_exists($file)) {
				if (time() - filemtime($file) > (2*3600)) { // two hours
					unlink($file);
				}
			}
			if (file_exists($file)) {
				$fileage = (time() - filemtime($file));
				if ($fileage > (1.25*3600)) { // half hour
					$killout = shell_exec("kill -9 {$thisProc}");
					$ds = date("D M j G:i:s T Y"); 
					$txt = "Firefox, News or Vivaldi [{$thisProc}] killed at {$ds}\n";
					$fh = fopen($killLog, "a");
					fwrite($fh, $txt);
					echo $txt;
					fclose($fh);
					unlink($file);
				} else {
					echo "File age: {$fileage}\n";
				}
			} else {
				$fh = fopen($file, "w");
				fwrite($fh, "{$thisProc}\n");
				fclose($fh);
			}
		}
	}

?>
