<?php

if ($argv[1] == 'all') {
  $path = '/srv/www/violet/web/uploads/previews/upinfo';
  $date = date('Ymd', strtotime('-1 day'));
  BackupList("$path/resources_$date.log");
  BackupList("$path/blogs_$date.log");
  BackupList("$path/thumbnails_$date.log");
} else {
  BackupList($argv[1]);
}

function BackupList($listFile) {
  if (!file_exists($listFile)) return;
  $list = file_get_contents($listFile);
  $list = explode("\n", $list);
  umask(0);
  foreach ($list as $item) {
    $pos = strpos($item, '/srv/www/violet/web');
    if ($pos === false) continue;
    $file1 = substr($item, $pos);
    $file2 = '/data/violet'.substr($file1, 19);
    if (file_exists($file1)) {
      $path2 = substr($file2, 0, strrpos($file2, "/"));
      if (!file_exists($path2)) mkdir($path2, 0777, true);
      $status = file_exists($file2)? "overwrite": "new";
      echo "Copy $status to $file2\n";
      copy($file1, $file2);
    }
  }
}
