<?php
/*
Officity - Web application platform - Version 6.0 - 2011-07-05

François Dispaux, Boris Verdeyen, Thomas Hermant,
Jérémie Roy, Grégory Meurice, Abdelila Harbi, 
Marc Mignonsin, Jonathan Sanchez, Julien Gonzalez, Pierre Fouchez

Sushee and Officity is © Copyright 2011 Nectil SA.

`/sushee-source/sushee/private/recron.php` is part of Officity.

Sushee 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 3 of the License, or
(at your option) any later version.

Officity and Sushee are distributed in the hope that they will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Sushee. If not, see <http://www.gnu.org/licenses/>.
*/
require_once(dirname(__FILE__)."/../common/common_functions.inc.php");
error_reporting(E_ERROR | E_WARNING | E_PARSE);

function generateCronFile(){
	
	$OS = getServerOS();
	
	if($OS=='windows'){
		makeDir(dirname(__FILE__)."/../../Files/tasks/");
		$mailingQueue_bat = dirname(__FILE__)."/../../Files/tasks/mailingQueue.bat";
		debug_log($mailingQueue_bat);
		$fd = fopen($mailingQueue_bat, "w+");
		fwrite($fd,'"'.$GLOBALS["phpExecutable"].'" "'.$GLOBALS["nectil_dir"].'\Kernel\private\mailingQueue.php"');
		fwrite($fd,"\n");
		fclose($fd);
		if(file_exists($mailingQueue_bat)){
			$mailingQueue_clean = 'schtasks /delete /tn mailingQueue /f 2>&1';
			debug_log(shell_exec($mailingQueue_clean));
			$mailingQueue_command = 'schtasks /create /sc minute /mo 1 /tn mailingQueue /tr \""'.realpath($mailingQueue_bat).'"\" 2>&1';
			debug_log($mailingQueue_command);
			debug_log(shell_exec($mailingQueue_command));
		}
		$dailyTask_bat = dirname(__FILE__)."/../../Files/tasks/dailyTask.bat";
		$fd = fopen($dailyTask_bat, "w+");
		if(file_exists($dailyTask_bat)){
			fwrite($fd,'"'.$GLOBALS["phpExecutable"].'" "'.$GLOBALS["nectil_dir"].'\Kernel\private\tasks.php"');
			fwrite($fd,"\n");
			fclose($fd);
			$dailyTask_clean = 'schtasks /delete /tn dailyTask /f 2>&1';
			debug_log(shell_exec($dailyTask_clean));
			$dailyTask_command = 'schtasks /create /sc daily /mo 1 /st 03:30:00 /tn dailyTask /tr \""'.realpath($dailyTask_bat).'"\" 2>&1';
			debug_log($dailyTask_command);
			debug_log(shell_exec($dailyTask_command));
		}
	}else{
		makeDir(dirname(__FILE__)."/../../Files/tasks/");
		$fd = fopen(dirname(__FILE__)."/../../Files/tasks/my_task", "w+");
		// for the primary Kernel
		fwrite($fd,"* * * * * ".$GLOBALS["phpExecutable"]." ".$GLOBALS["nectil_dir"]."/".Sushee_dirname."/private/mailingQueue.php");
		fwrite($fd,"\n30 3 * * * ".$GLOBALS["phpExecutable"]." ".$GLOBALS["nectil_dir"]."/".Sushee_dirname."/private/tasks.php");
		fwrite($fd,"\n");
		fclose($fd);
		debug_log(shell_exec('crontab '.dirname(__FILE__)."/../../Files/tasks/my_task 2>&1"));
	}
}

generateCronFile();

?>
