<?php
/*=======================================================================
 *
 * automailer_main.php
 *
 * Autor: Marc Vollmann
 *
  =======================================================================*/

include_once ("../include/mcglobal.inc.php");


// if (isRunning(1)):
//   exit();
// endif;

// Execution-Time for script
set_time_limit(0);


function call_automailer() {
  global $logFile, $path;

  $result = "";
  $cmd = "php " . $path . "/include/automailer.php >" . $path . "/log/automailer.stdout+err 2>&1 &";
  $ausgabe = system($cmd, $result);
}

function call_automailer2() {
  global $logFile, $path;

  $result = "";
  $cmd = "php " . $path . "/include/automailer2.php >" . $path . "/log/automailer.stdout+err 2>&1 &";
  $ausgabe = system($cmd, $result);
}

function call_automailer3() {
  global $logFile, $path;

  $result = "";
  $cmd = "php " . $path . "/include/automailer3.php >" . $path . "/log/automailer.stdout+err 2>&1 &";
  $ausgabe = system($cmd, $result);
}

function call_automailer4() {
  global $logFile, $path;

  $result = "";
  $cmd = "php " . $path . "/include/automailer4.php >" . $path . "/log/automailer.stdout+err 2>&1 &";
  $ausgabe = system($cmd, $result);
}

function call_automailer5() {
  global $logFile, $path;

  $result = "";
  $cmd = "php " . $path . "/include/automailer5.php >" . $path . "/log/automailer.stdout+err 2>&1 &";
  $ausgabe = system($cmd, $result);
}

function call_automailer6() {
  global $logFile, $path;

  $result = "";
  $cmd = "php " . $path . "/include/automailer6.php >" . $path . "/log/automailer.stdout+err 2>&1 &";
  $ausgabe = system($cmd, $result);
}

function call_automailer8() {
  global $logFile, $path;

  $result = "";
  $cmd = "php " . $path . "/include/automailer8.php >" . $path . "/log/automailer.stdout+err 2>&1 &";
  $ausgabe = system($cmd, $result);
}

// START SCRIPT

// $path = getParameterValue("0", "PATH_DOCROOT", "0");
$dirName = dirname(__FILE__);
$dirName = str_replace("\\", "/", $dirName);
$lastSlashPos = strrpos($dirName,"/");
$path = substr($dirName, 0, $lastSlashPos);

$logFile = getParameterValue("0", "AUTOMAILER_LOGFILE", "0");

$constAutomailerSleeptime = getParameterValue("0", "AUTOMAILER_SLEEP_TIME", "0");
if ($constAutomailerSleeptime == "" || !is_numeric($constAutomailerSleeptime) || $constAutomailerSleeptime < 2 || $constAutomailerSleeptime > 9) :
   $constAutomailerSleeptime = 4;
endif;

// Endless loop
while (TRUE):

  $constAutomailerEnabled = getParameterValue("0", "AUTOMAILER_ENABLED", "0");

  // Set execution time for keepalive
  $currentTime = getDateTime("0");
  updateStmt("keepalive", "ka_process", "automailer", array("ka_lastexecutiontime", $currentTime),"");

  if ($constAutomailerEnabled == '1') :
     call_automailer();
  endif;

  sleep($constAutomailerSleeptime);

  // Set execution time for keepalive
  $currentTime = getDateTime("0");
  updateStmt("keepalive", "ka_process", "automailer", array("ka_lastexecutiontime", $currentTime),"");

  if ($constAutomailerEnabled == '1') :
     call_automailer2();
  endif;

  sleep($constAutomailerSleeptime);

  // Set execution time for keepalive
  $currentTime = getDateTime("0");
  updateStmt("keepalive", "ka_process", "automailer", array("ka_lastexecutiontime", $currentTime),"");

  if ($constAutomailerEnabled == '1') :
     call_automailer3();
  endif;

  sleep($constAutomailerSleeptime);

  // Set execution time for keepalive
  $currentTime = getDateTime("0");
  updateStmt("keepalive", "ka_process", "automailer", array("ka_lastexecutiontime", $currentTime),"");

  if ($constAutomailerEnabled == '1') :
     call_automailer4();
  endif;

  sleep(1);

  // Set execution time for keepalive
  // $currentTime = getDateTime("0");
  // updateStmt("keepalive", "ka_process", "automailer", array("ka_lastexecutiontime", $currentTime),"");

  if ($constAutomailerEnabled == '1') :
     call_automailer5();
  endif;

  sleep(1);

  if ($constAutomailerEnabled == '1') :
     call_automailer6();
  endif;

  sleep(1);

  if ($constAutomailerEnabled == '1') :
     call_automailer8();
  endif;

  sleep(1);

endwhile;  // Endless loop
?>
