<?php

require_once('copytool.php');
$max = floor(execSQL('select max(doc_id) from tbldocument')->fetchColumn()/1000);
$min = @is_numeric($options['n'])? $max - $options['n'] : 0;
$thumbcp = array_key_exists('t', $options);

for ($i = $max; $i >= $min; $i--) {
  $stmt = execSQl('select doc_id, doc_category, doc_filename, doc_type from tbldocument '.
  'where doc_id>=' . ($i*1000) . ' and doc_id<' . (($i+1)*1000));

  while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    list($doc_id, $doc_cat, $doc_file, $doc_type) = array_values($row);
    mycopy("uploads/resources/$doc_cat/$doc_file", "Copy $doc_id");
    $dotpos = strrpos($doc_file, '.');
    if (substr($doc_file, $dotpos) == '.flv' && $doc_type != 'flv') {
      if ($doc_type=='mpe') $doc_type = 'mpeg';
      mycopy("uploads/resources/$doc_cat/".substr($doc_file, 0, $dotpos).'.'.$doc_type, "Copy $doc_id");
    }
    if ($thumbcp) {
      mycopy("uploads/thumbnails/$doc_cat/thumbnails/$doc_file.jpg", "Copy $doc_id");
      mycopy("uploads/thumbnails/$doc_cat/thumbnails2/$doc_file.jpg", "Copy $doc_id");
    }
  }
}
