<?php
// Author Carsten Jensen aka Tomse
// Copyright (c) 2013 Carsten Jensen
// Released under GNU GPL v2

// Corrects page numbers from scanned full-paper pages
// which has been split into 2, and been edited in scan tailor

$ext = '.tif';
$files = glob('*.tif');

// set to 0 if back page lies first, or 1 if frontpage lies first
$up = 0;
$down = count($files);
$odd = 'odd';
$even = 'even';
$pad = strlen($down)+1;
@mkdir ($odd);
@mkdir ($even);

$again = 1;
$back = true;

foreach($files as $k => $v)
{
	if ($up % 2)  // Even pages
	{
		copy($v, $even . DIRECTORY_SEPARATOR . 'page' . padding($up, $pad) . $ext);
		$up++;
		echo "$k => even\\ $up \n";
	}
	else
	{
		copy($v, $odd . DIRECTORY_SEPARATOR . 'page' . padding($down, $pad) . $ext);
		$up++;
		$down--;
		echo "$k => odd\\ $down \n";
	}
}


/**
 * Pads a number with zeroes to make a nice even lenghted result
 * Author Carsten Jensen
 * @param int $int number to be padded
 * @param int $length of padding
 * @return int padded number
*/
function padding($int, $length = 2)
{
	return str_pad($int, $length, 0, STR_PAD_LEFT);
}
?>