<?php

if (posix_getuid() == 0){
    echo 'Not allow root user!';
    exit;
}

$rvsitebuilderLicenseFixerObj = new RvsitebuilderLicenseFixer();

if(php_sapi_name() === 'cli'){
    $rvsitebuilderLicenseFixerObj->license_fix_framework();
    $rvsitebuilderLicenseFixerObj->license_fix_all_packages();    
}else{
    echo 'Not allow on web mode!';
    exit;
}

class RvsitebuilderLicenseFixer{    
    protected $app_path;
    protected $package_path;
    protected $rvsitebuilder_package_path;

    protected $fixer_path;
    protected $license_fixer_config;
    
    public function __construct()
    {        
        $this->app_path = $this->get_app_path();        
        $this->package_path = $this->get_package_path();        
        $this->rvsitebuilder_package_path = $this->package_path . '/rvsitebuilder';
        
        $this->license_fixer_path = $this->get_license_fixer_path();
        $this->license_fixer_config = $this->load_license_fixer_config();
    }
    
    private function get_app_path(){
        return realpath(dirname(__FILE__) . '/../../../');
    }
    
    private function get_package_path(){
        return realpath(dirname(__FILE__) . '/../../');
    }
    
    private function get_license_fixer_path(){
        return dirname(__FILE__);
    }
    
    private function load_license_fixer_config(){        
        if(is_file($this->license_fixer_path . '/config/' . 'license-fixer.php')){
            return require $this->license_fixer_path . '/config/' . 'license-fixer.php';
        }else{
            echo 'Not found config file license-fixer.php';
            exit;
        }        
    }
    
    public function license_fix_framework(){
        echo "== License fixer for framework \n";
        $this->composer_json_license_fixer($this->app_path);
        $this->package_json_license_fixer($this->app_path);
        $this->add_package_license($this->app_path);
    }
    
    public function license_fix_all_packages(){        
        echo "== License fixer for all package \n";
        $rvPackageNames = scandir($this->rvsitebuilder_package_path);
        
        foreach ($rvPackageNames as $package){
            if($package === '.' || $package === '..'|| $package === '.gitignore') {continue;}
            if(is_file($this->rvsitebuilder_package_path . '/' . $package)){
                continue;
            }
            echo "==== $package \n";
            $this->composer_json_license_fixer($this->rvsitebuilder_package_path . '/' . $package);
            $this->package_json_license_fixer($this->rvsitebuilder_package_path . '/' . $package);
            $this->add_package_license($this->rvsitebuilder_package_path . '/' . $package);
        }
    }
    
    /* rvsitebuildercms/packages/rvsitebuilder/<package>/composer.json */
    public function composer_json_license_fixer($package_path = ''){
        $composer_json_file =  $package_path . '/composer.json';
        if(!file_exists($composer_json_file)){
            return 0;
        }
        echo "====== License fixer composer.json\n";
        $composer_json_content = file_get_contents($composer_json_file);
        $composer_json = json_decode($composer_json_content,true);

        $composer_json = array_merge($composer_json,$this->license_fixer_config['composer_json']);
        
        $composer_json_content = json_encode($composer_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
        file_put_contents($composer_json_file,$composer_json_content);
        return true;
    }

    /* rvsitebuildercms/packages/rvsitebuilder/<package>/package.json */
    public function package_json_license_fixer($package_path = ''){        
        $package_json_file =  $package_path . '/package.json';
        if(!file_exists($package_json_file)){
            return 0;
        }
        echo "====== License fixer package.json\n";
        $package_json_content = file_get_contents($package_json_file);
        $package_json = json_decode($package_json_content,true);
        
        $package_json = array_merge($package_json,$this->license_fixer_config['package_json']);
        
        $package_json_content = json_encode($package_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
        file_put_contents($package_json_file, $package_json_content);
        return true;
    }
            
    public function add_package_license($package_path = ''){
        echo "====== License fixer add LICENSE\n";
        $license_file =  $package_path . '/LICENSE';
        file_put_contents($license_file, $this->license_fixer_config['LICENSE']);
        return true;
    }    
    
}

?>