#!/usr/bin/env php
<?php

/**
 * Finds and requires the Composer autoloader if available.
 */
function loadAutoloader() {
	// Check the vendor autoload paths at different levels
	$autoloadPaths = [
		__DIR__ . '/vendor/autoload.php',
		__DIR__ . '/../vendor/autoload.php',
		__DIR__ . '/../../vendor/autoload.php',
		__DIR__ . '/../../../vendor/autoload.php',
		__DIR__ . '/../../../../vendor/autoload.php'
	];

	foreach ($autoloadPaths as $path) {
		if (file_exists($path)) {
			require_once $path;
			return true;
		}
	}
	return false;
}

// Attempt to load the autoloader
if (!loadAutoloader()) {
	die("\nMigrant must be installed as a Composer package within a project.\n\n");
}

// Proceed with the rest of the script
$workingFolder = getcwd();

try {
	$arguments = $argv;
	array_shift($arguments); // the first argument is always the name of the script
	$migrantWorker = new \Fluxoft\Migrant\Worker();
	$migrantWorker->Work($arguments, $workingFolder);
} catch (\Exception $e) {
	\Fluxoft\Migrant\Printer::PrintException($e);
}
