<?php
// slideshow_cleanup.php
// RJM Programming
// May, 2022

  $foundone=false;
  $doall=false;
  $dotxt="";
  $cdis=dirname(__FILE__) . DIRECTORY_SEPARATOR;
  $prevrun="";
  if (file_exists('/tmp/slideshow_cleanup.now')) {
    $prevrun=file_get_contents('/tmp/slideshow_cleanup.now');
    if ($prevrun != $cdis) {
      $prevrun="";
    }
  }
  if ($prevrun == "") {
  file_put_contents('/tmp/slideshow_cleanup.now', $cdis);
  if (file_exists('/tmp/slideshow_cleanup.txt')) {
    unlink('/tmp/slideshow_cleanup.txt');
  }
  exec("cd " . $cdis . "; find . -name 'slideshow_0.zip' 2> /dev/null > /tmp/slideshow_cleanup.txt; chmod 777 /tmp/slideshow_cleanup.txt;  chmod 777 /tmp/slideshow_cleanup.now;  chgrp rjmprogr /tmp/slideshow_cleanup.txt; chown rjmprogr /tmp/slideshow_cleanup.txt;  chgrp rjmprogr /tmp/slideshow_cleanup.now; chown rjmprogr /tmp/slideshow_cleanup.now; ");
  
  $lines = file('/tmp/slideshow_cleanup.txt');
  date_default_timezone_set('Australia/Perth');
  file_put_contents('/tmp/slideshow_cleanup.txt', 'Inhouse Slideshow tidy up at ' . date('l jS \of F Y h:i:s A') . ' Australia/Perth report ... ' . "\n");

  if (isset($argv)) {
        if ($argc >= 3) {
          if (strtolower($argv[2]) == 'all') {
            $doall=true;
          }
        } 
  } else if (isset($_GET['all'])) {
            $doall=true;
  } 

  foreach ($lines as $line_num => $line) {
    $ji=0;
    if (substr($line,0,1) == '.') { $ji=2; }
    $thisdir=explode('slideshow_0.zip', $line)[0];
    $uzc=shell_exec("unzip -l " . $cdis . substr($line, $ji));
    foreach (glob($thisdir . '*-[0-9]*of.[jJpPgG][pPnNiI]*') as $zipmember) {
      $ij=0;
      if (substr($zipmember,0,1) == '.') { $ij=2; }
      $thisfile=$cdis . substr($zipmember, $ij);
      if (strpos($uzc, (' ' . explode(DIRECTORY_SEPARATOR,   substr($zipmember, $ij))[-1 + sizeof(explode(DIRECTORY_SEPARATOR,   substr($zipmember, $ij)))])    ) !== false) {
        if ((time() - filectime($thisfile)) > 600) {   // more than 10 minutes old
          $foundone=true;
          file_put_contents('/tmp/slideshow_cleanup.txt', file_get_contents('/tmp/slideshow_cleanup.txt') . 'Deleted ' . $thisfile . ' which was ' . (time() - filectime($thisfile)) . ' seconds old.' . "\n");
          unlink($thisfile); //echo "analyze " . $thisfile . " " . (time() - filectime($thisfile)) . ' seconds ago ' . "\n";
        } else if ($doall) {
          $foundone=true;
          $dotxt.=$thisfile . ' is kept because only ' . (time() - filectime($thisfile)) . ' seconds old.' . "\n";
        } 
      }
    }
    foreach (glob($thisdir . '*-[0-9]{[0-9]*}.[jJpPgG][pPnNiI]*') as $zipmember) {
      $ij=0;
      if (substr($zipmember,0,1) == '.') { $ij=2; }
      $thisfile=$cdis . substr($zipmember, $ij);
      if (strpos($uzc, (' ' . explode(DIRECTORY_SEPARATOR,   substr($zipmember, $ij))[-1 + sizeof(explode(DIRECTORY_SEPARATOR,   substr($zipmember, $ij)))])    ) !== false) {
        if ((time() - filectime($thisfile)) > 600) {
          $foundone=true;
          file_put_contents('/tmp/slideshow_cleanup.txt', file_get_contents('/tmp/slideshow_cleanup.txt') . 'Deleted ' . $thisfile . ' which was ' . (time() - filectime($thisfile)) . ' seconds old.' . "\n");
          unlink($thisfile); //echo "Analyze " . $thisfile . " " . (time() - filectime($thisfile)) . ' seconds ago ' . "\n";
        } else if ($doall) {
          $foundone=true;
          $dotxt.=$thisfile . ' is kept because only ' . (time() - filectime($thisfile)) . ' seconds old.' . "\n"; 
        } 
      }
    }
    
    }
    
    if ($foundone) {
      if (isset($argv)) {
        if (1 == 5) {
        if ($argc >= 2) {
          if (strtolower($argv[1]) == 'rmetcalfe15@gmail.com') {
            if ($doall) {  file_put_contents('/tmp/slideshow_cleanup.txt', file_get_contents('/tmp/slideshow_cleanup.txt') . "\n\n" . $dotxt); }
            passthru("uuencode /tmp/slideshow_cleanup.txt " . str_replace('/','_',str_replace(':','_',str_replace(' ','_','Inhouse Slideshow tidy up at ' . date('l jS \of F Y h:i:s A') . ' report.txt'))) . " | mailx -s '" . str_replace('/','_',str_replace(':','_',str_replace(' ','_','Inhouse Slideshow tidy up at ' . date('l jS \of F Y h:i:s A') . ' report.txt'))) . "' rmetcalfe15@gmail.com");
          } else if (strtolower($argv[1]) == 'rmetcalfe@rjmprogramming.com.au') {
            if ($doall) {  file_put_contents('/tmp/slideshow_cleanup.txt', file_get_contents('/tmp/slideshow_cleanup.txt') . "\n\n" . $dotxt); }
            passthru("uuencode /tmp/slideshow_cleanup.txt " . str_replace('/','_',str_replace(':','_',str_replace(' ','_','Inhouse Slideshow tidy up at ' . date('l jS \of F Y h:i:s A') . ' report.txt'))) . " | mailx -s '" . str_replace('/','_',str_replace(':','_',str_replace(' ','_','Inhouse Slideshow tidy up at ' . date('l jS \of F Y h:i:s A') . ' report.txt'))) . "' rmetcalfe@rjmprogramming.com.au");
          }
        } 
        }
      } else if (isset($_GET['to'])) {
        if (1 == 5) {
          if (strtolower(str_replace('+',' ',urldecode($_GET['to']))) == 'rmetcalfe15@gmail.com') {
            if ($doall) {  file_put_contents('/tmp/slideshow_cleanup.txt', file_get_contents('/tmp/slideshow_cleanup.txt') . "\n\n" . $dotxt); }
            passthru("uuencode /tmp/slideshow_cleanup.txt " . str_replace('/','_',str_replace(':','_',str_replace(' ','_','Inhouse Slideshow tidy up at ' . date('l jS \of F Y h:i:s A') . ' report.txt'))) . " | mailx -s '" . str_replace('/','_',str_replace(':','_',str_replace(' ','_','Inhouse Slideshow tidy up at ' . date('l jS \of F Y h:i:s A') . ' report.txt'))) . "' rmetcalfe15@gmail.com");
          } else if (strtolower(str_replace('+',' ',urldecode($_GET['to']))) == 'rmetcalfe@rjmprogramming.com.au') {
            if ($doall) {  file_put_contents('/tmp/slideshow_cleanup.txt', file_get_contents('/tmp/slideshow_cleanup.txt') . "\n\n" . $dotxt); }
            //exec("cat /tmp/slideshow_cleanup.txt | uuencode '" . str_replace('/','_',str_replace(':','_',str_replace(' ','_','Inhouse Slideshow tidy up at ' . date('l jS \of F Y h:i:s A') . ' report.txt'))) . "' | mailx -s '" . str_replace('/','_',str_replace(':','_',str_replace(' ','_','Inhouse Slideshow tidy up at ' . date('l jS \of F Y h:i:s A') . ' report.txt'))) . "' rmetcalfe@rjmprogramming.com.au");
            passthru("uuencode /tmp/slideshow_cleanup.txt " . str_replace('/','_',str_replace(':','_',str_replace(' ','_','Inhouse Slideshow tidy up at ' . date('l jS \of F Y h:i:s A') . ' report.txt'))) . " | mailx -s '" . str_replace('/','_',str_replace(':','_',str_replace(' ','_','Inhouse Slideshow tidy up at ' . date('l jS \of F Y h:i:s A') . ' report.txt'))) . "' rmetcalfe@rjmprogramming.com.au");
          }
        }
      } 
    } else {
      echo "Nothing to tidy up.\n";
    }

    if (file_exists('/tmp/slideshow_cleanup.now')) {
      unlink('/tmp/slideshow_cleanup.now');
    }
      
    } else {
      echo "Another instance has not finished running.";
    }

?>
