#!/usr/bin/php -q
<?php
if (!file_exists("./include/core.php")) {
	die("Could not start core. Make sure you are running IQ from it's own directory: cd ~/IQ; ./IQ\n");
}
if (!@include("./include/library.php")) {
	die("Failed to load library.php\n");
}
global $argv;
array_shift($argv);
$IQargs=parseLine($argv);
$starttime=time();
if (isset($IQargs["d"])) {
	if (!function_exists("pcntl_fork")) {
		die("PHP does not appear to be compiled with process control support. Recompile with --enable-pcntl in your configure line.\n");
	}
	else {
		// Try to fork into background
		$pid=pcntl_fork();
		if ($pid==-1) {
			die("Could not fork\n");
		}
		elseif ($pid) {
			// parent
			echo "IQ running in daemon mode. Wrapper PID: $pid\n";
			exit;
		} 
		else {
			// child
		}
	}
}
while (1) {
	system("./include/core.php $starttime ".join(" ",$argv),$returncode);
	switch($returncode) {
		case "0":
			// die
			exit;
		case "1":
			// restart
			continue;
		case "9":
			// killed with a signal
			die("Killed\n");
		case "126":
			// bad interpreter error, couldn't find php
			die("Could not find PHP. Make sure the first line of core.php points to the correct location.\n");
		case "255":
			// parse error, die
			exit;
		default:
			// other
			die("Unknown return code: '$returncode'\n");
	}
}
?>
