<?php

namespace CiextInstaller;

class Install
{

    public static function run()
    {
        if (file_exists('application')) {
            echo 'Already installed' . PHP_EOL;
            return false;
        }

        // Copy application
        self::recursiveCopy('vendor/codeigniter/framework/application', 'application');
        echo 'application folder copied' . PHP_EOL;

        // Copy application files
        self::recursiveCopy('vendor/ciext/ciext_installer/data/application', 'application');
        echo 'Extended application files copied' . PHP_EOL;

        // Copy assets files
        self::recursiveCopy('vendor/ciext/ciext_installer/data/assets', 'assets');
        echo 'assets folder copied' . PHP_EOL;

        // Edit autoload file
        self::edit_file('application/config/autoload.php', [
            '$autoload[\'packages\'] = array()',
            '$autoload[\'helper\'] = array()',
            '$autoload[\'config\'] = array()'
                ], [
            '$autoload[\'packages\'] = array(\'vendor/codeigniter/codeigniter3-translations\', \'vendor/ciext/ciext/third_party/application\')',
            '$autoload[\'helper\'] = array(\'url\')',
            '$autoload[\'config\'] = array(\'config_assets\')'
        ]);
        echo 'application/config/autoload.php changed' . PHP_EOL;

        // Edit config file
        self::edit_file('application/config/config.php', [
            '$config[\'base_url\'] = \'\';',
            '$config[\'index_page\'] = \'index.php\';',
            '$config[\'language\']	= \'english\';',
            'MY_',
            '$config[\'log_threshold\'] = 0;',
            '$config[\'sess_cookie_name\'] = \'ci_session\';',
            '$config[\'sess_save_path\'] = NULL;',
            '$config[\'sess_match_ip\'] = FALSE;',
            '$config[\'cookie_secure\'] = FALSE;',
            '$config[\'cookie_httponly\'] = FALSE;',
            '$config[\'composer_autoload\'] = FALSE;',
            '$config[\'proxy_ips\'] = \'\';',
                ], [
            '$config[\'base_url\'] = (ENVIRONMENT == \'development\') ? \'/\' : \'/\';',
            '$config[\'index_page\'] = \'\';',
            '$config[\'language\']	= \'english\';' . PHP_EOL . '$config[\'language_short\'] = \'en\';',
            'RT_',
            '$config[\'log_threshold\'] = 1;',
            '$config[\'sess_cookie_name\'] = \'ciext_session\';',
            '$config[\'sess_save_path\'] = sys_get_temp_dir();',
            '$config[\'sess_match_ip\'] = TRUE;',
            '$config[\'cookie_secure\'] = TRUE;',
            '$config[\'cookie_httponly\'] = TRUE;',
            '$config[\'composer_autoload\'] = realpath(APPPATH . \'../vendor/autoload.php\');',
            '$config[\'proxy_ips\'] = \'\';' . PHP_EOL . PHP_EOL . 'require_once \'ciext.php\';',
        ]);
        echo 'application/config/config.php changed' . PHP_EOL;

        // Edit index.php file
        copy('vendor/codeigniter/framework/index.php', 'index.php');
        self::edit_file('index.php', [
            '$system_path = \'system\';',
                ], [
            '$system_path = \'vendor/codeigniter/framework/system\';',
        ]);
        echo 'index.php copied' . PHP_EOL;

        // Remove welcome
        if (file_exists('application/controllers/Welcome.php')) {
            unlink('application/controllers/Welcome.php');
            unlink('application/views/welcome_message.php');
        }
        echo 'Welcome page deleted' . PHP_EOL;

        // Update composer.json
        copy('vendor/ciext/ciext_installer/data/composer.json.dist', 'composer.json');

        // Run composer update
        passthru('composer update');

        echo 'CIEXT3 successfully installed.' . PHP_EOL;
    }

    protected static function edit_file($file, $search = [], $replace = [])
    {
        return file_put_contents($file, str_replace($search, $replace, file_get_contents($file)));
    }

    /**
     * Recursive Copy
     *
     * @param string $src
     * @param string $dst
     */
    protected static function recursiveCopy($src, $dst)
    {
        if (!file_exists($dst)) {
            mkdir($dst, 0755);
        }

        $iterator = new \RecursiveIteratorIterator(
                new \RecursiveDirectoryIterator($src, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST
        );

        foreach ($iterator as $file) {
            if ($file->isDir()) {
                if (!file_exists($dst . '/' . $iterator->getSubPathName())) {
                    mkdir($dst . '/' . $iterator->getSubPathName());
                }
            } else {
                copy($file, $dst . '/' . $iterator->getSubPathName());
            }
        }
    }

}
