#!/usr/bin/env php
<?php
/**
 * @package
 * @subpackage
 * @author      Chance Garcia <chance@garcia.codes>
 * @copyright   (C)Copyright 2013-2021 Chance Garcia, chancegarcia.com
 *
 *    The MIT License (MIT)
 *
 * Copyright (c) 2013-2021 Chance Garcia
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

$currentDir = getcwd();

$configFile = $currentDir . '/config/chancegarcia_git_toolkit.php';
$dirConfigExists = false;
$projectName = null;
$outputFilename = null;
$outputPath = null;

if (file_exists($configFile)) {
    /**
     * @var array $config
     */
    $config = require $configFile;
    $dirConfigExists = array_key_exists('project_root', $config);
    if (array_key_exists('project_name', $config)) {
        $projectName = $config['project_name'];
    }

    if (array_key_exists('filename', $config)) {
        $outputFilename = $config['filename'];
    }

    if (array_key_exists('output_directory', $config)) {
        $outputPath = $config['output_directory'];
    }
}

if ($dirConfigExists) {
    $dir = $config['project_root'];
} else {
    $dir = $currentDir;
    if (false !== strpos($currentDir, 'bin')) {
        $dir = $currentDir . '/..';
    }
}

require $currentDir . '/vendor/autoload.php';

use Symfony\Component\Console\Application;
use Chance\GitToolkit\GitInformation;
use Chance\GitToolkit\Command\ChangeLog;
use Chance\GitToolkit\Service\ChangeLogService;
use Cz\Git\GitRepository;

$application = new Application();

$gitRepo = new GitRepository($dir);
$gitInformation = new GitInformation($gitRepo);
$changeLogService = new ChangeLogService($gitInformation);
$changeLogService->setChangeLogFileName($outputFilename);
$changeLogService->setChangeLogFilePath($outputPath);
$changeLogService->setMainHeaderName($projectName);

$changeLogCommand = new ChangeLog();
$changeLogCommand->setChangeLogService($changeLogService);
$application->add($changeLogCommand);

$application->run();
