<?php

date_default_timezone_set('Europe/Berlin');
set_time_limit(0);
error_reporting(E_ALL & ~E_NOTICE);
$log_file_name = "/root/votian_admin/do_bzip2_" . date("Ym") . ".log";
$srv_root = "/home/mps2";

doIt("/home/htdata/mysql_hha");
doIt("/home/htdata/mysql_stadtbote1");
//doItEdifact("$srv_root/sb/temp/edifact");
doItLogs("$srv_root/stadtbote/log", false);
delete_files("$srv_root/stadtbote/temp/signs");
delete_files("$srv_root/stadtbote/temp/captchas");
delete_files("$srv_root/stadtbote/temp/download");
//delete_files("$srv_root/stadtbote/temp/photos");

function doIt($source) {
  $filestobezipped = array();
  $max_filemtime = 0;
  
  if ($handle = opendir($source)):
    while (false !== ($file = readdir($handle))):
      if (!is_dir("$source/$file")):
        list($dirname, $basename, $extension, $filename) = array_values(pathinfo($file));
        if  ($filename == "mps2-bin" && substr($extension, 0, 1) == "0"):
          $filestobezipped[] = array("$source/$file", filemtime("$source/$file"));
          $max_filemtime = (filemtime("$source/$file") > $max_filemtime ? filemtime("$source/$file") : $max_filemtime);
        endif;
      endif;
    endwhile;
    closedir($handle);
  endif;

  foreach ($filestobezipped as $filetobezipped) {
    if ($filetobezipped[1] != $max_filemtime):
  	  exec_cmd("bzip2 -9 " . $filetobezipped[0]);
    endif;
  } 
}

//function doItEdifact($source) {
//  exec_cmd("cd $source;tar -cvf \"()_edifact_" . date("Ym") . ".tar\" --exclude=*.bz2 *;bzip2 -9f \"()_edifact_" . date("Ym") . ".tar\"");
//  if (is_file("\"()_edifact_" . date("Ym", mktime(0, 0, 0, date("m") - 2, 1, date("Y"))) . ".tar\""))
//    exec_cmd("rm \"()_edifact_" . date("Ym", mktime(0, 0, 0, date("m") - 2, 1, date("Y"))) . ".tar\"");
//  
//  $filestoberemoved = array();
//  if ($handle = opendir($source)):
//    while (false !== ($file = readdir($handle))):
//      if (!is_dir("$source/$file")):
//        list($dirname, $basename, $extension, $filename) = array_values(pathinfo($file));
//        if (filemtime("$source/$file") < mktime(0, 0, 0, date("m"), 1, date("Y")) && $extension != "bz2"):
//          $filestoberemoved[] = "$source/$file";
//        endif;
//      endif;
//    endwhile;
//    closedir($handle);
//  endif;
//
//  foreach ($filestoberemoved as $filetoberemoved) {
//  	exec_cmd("rm \"" . $filetoberemoved . "\"");
//  } 
//}

function doItLogs($source, $do_delete = false) {
  $filestoberemoved = array();
  $filestobezipped = array();
  
  if ($handle = opendir($source)):
    while (false !== ($file = readdir($handle))):
      if (!is_dir("$source/$file")):
        list($dirname, $basename, $extension, $filename) = array_values(pathinfo($file));
        if (!(strpos($basename, "_" . date("Ym", mktime(0, 0, 0, date("m") - 1, 1, date("Y")))) === false) && $extension == "log"):
          $filestobezipped[] = "$source/$file";
        elseif (!(strpos($basename, "_" . date("Ym", mktime(0, 0, 0, date("m") - 2, 1, date("Y")))) === false) && $extension == "bz2"):
          $filestoberemoved[] = "$source/$file";
        endif;
      endif;
    endwhile;
    closedir($handle);
  endif;

  if ($do_delete)
    foreach ($filestoberemoved as $filetoberemoved) {
  	  exec_cmd("rm " . $filetoberemoved);
    } 
  foreach ($filestobezipped as $filetobezipped) {
  	exec_cmd("bzip2 -9 " . $filetobezipped);
  } 
}

function delete_files($filespec){
//  exec_cmd("ls $filespec | xargs rm");
  exec_cmd("find $filespec -type f -delete");
}

function exec_cmd($cmd){
  global $log_file_name;

  $cmd .= " >> $log_file_name 2>&1";
  writeLog("\"$cmd\"");
  exec($cmd);
}

function writeLog($log_text)
{
  global $log_file_name;
  
  $fileHandle = @fopen($log_file_name, 'a');
  @fwrite($fileHandle, "[" . date("Y-m-d H:i:s") . "] " . $log_text .  "\n");
  @fclose($fileHandle);
  return;
}

?>
