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

date_default_timezone_set("UTC");
ini_set ('gd.jpeg_ignore_warning', 1);

$files = array(
  __DIR__ . '/../../vendor/autoload.php',
  __DIR__ . '/../../../../autoload.php',
  __DIR__ . '/../../../autoload.php',
  '../vendor/autoload.php',
  'vendor/autoload.php',  
);

foreach ($files as $file) {
    if (file_exists($file)) {
        require $file;

        define('SCALP_COMPOSER_INSTALL', $file);

        break;
    }
}

if (!defined('SCALP_COMPOSER_INSTALL')) {
    die(
      'You need to set up the project dependencies using the following commands:' . PHP_EOL .
      'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
      'php composer.phar install' . PHP_EOL
    );
}



use Buonzz\Scalp\Commands\Metadata\ExtractCommand;
use Buonzz\Scalp\Commands\B2\UploadCommand;
use Buonzz\Scalp\Commands\Maver\GenerateCommand;
use Buonzz\Scalp\Commands\Thumb\CreateThumbnailCommand;
use Buonzz\Scalp\Commands\Thumb\SaveThumbnailsCommand;
use Dotenv\Dotenv;

use Symfony\Component\Console\Application;

// defaults
$config_items = array(
                        'INPUT_FOLDER' => './',
                        'OUTPUT_FOLDER' => './dist',
                        'LOG_FOLDER' => './',
                        'THUMB_PERCENT_RESIZE' => 10,
                        'TIMEZONE' => 'UTC'
                        );

try{

  $dotenv = new Dotenv(getcwd());
  $dotenv->load();

  foreach($config_items as $k=>$v){
    if(getenv($k) === FALSE)
        putenv($k . '=' . $v);
  }

}catch(\Dotenv\Exception\InvalidPathException $e)
{
  foreach($config_items as $k=>$v)
        putenv($k . '=' . $v);
}

  // set the timezone after reading the one in .env file
  date_default_timezone_set(getenv('TIMEZONE'));

  $command = new ExtractCommand();

  $application = new Application("Scalp Metadata Extraction Tool by Darwin Biler", "v2.1.2");

  $application->add(new CreateThumbnailCommand);
  $application->add(new GenerateCommand);
  $application->add(new UploadCommand);
  $application->add($command);


  $application->run();
