<?php

// Sorts images which has first been scanned face up,
// then scanned face down using ADF simplex scanning on double 
// sided pages.


$files = glob("input/*.tif");
$count = count($files);
$split = $count / 2;

$outdir = "./output/";

$counter = 1;
$pagenum = 1;
@mkdir($outdir);

while ($counter <= $split) 
{
	echo $counter . " ";
	print_r($files[$counter-1]);
	copy($files[$counter-1], $outdir . str_pad($pagenum, 4, 0, STR_PAD_LEFT) . '.tif');
	echo " " . $pagenum ."\r\n";
	$counter++;
	$pagenum += 2;
	
}

echo $counter;
$pagenum--;

while ($counter <= $count) 
{
	echo $counter . " ";
	print_r($files[$counter-1]);
	copy($files[$counter-1], $outdir . str_pad($pagenum, 4, 0, STR_PAD_LEFT) . '.tif');
	echo " " . $pagenum ."\r\n";
	$counter++;
	$pagenum -= 2;
}