<?php
define('uglifyjs','uglifyjs');
exec(uglifyjs.' -V', $output, $return);
if (empty($output)) {
	echo 'Uglifyjs not found in server. Please install uglifyJS/ Please try it using php CLI';exit;
}

include('simple_html_dom.php');
include('functions.php');
$tpl_file_list = array("Content.tpl", 
    "PaymentView.tpl",
    "ContentTopbar.tpl",    
    "ContentSidebarLeft.tpl",   
    "catalog/default/Catalog.tpl",     
    "catalog/default/Product.tpl",
    "catalog/Index.tpl",    
    "Specialoffers.tpl",
    "SpecialofferDetails.tpl",
    "Testdrive.tpl",
    "ContentCommonShare.tpl",    
    "PaymentThanks.tpl",
    "CommonLanguageOptions.tpl",   
    "EmailConfirm.tpl",    
    "ContentSidebarRight.tpl",    
    "Forminclude.tpl",
    "Form.tpl",    
    "CommonJavaScript.tpl",
    "CommonJavaScriptBottom.tpl",
    "Pannellum.tpl"
    );
//$dir_ignore = array("menu","email_templates","theme-config","js_templates");
$version 	= isset($argv[2])?$argv[2]:'v1';
// TO COPY JS FILES FROM ONE VERSION TO ANOTHER
$old_assets_version = isset($argv[2])?$argv[2]:'';
$default_theme 		= 'custom';
$themes = array(
				"custom"				
				);
			
$default_theme_tpl_path = "application/core/views/frontend/".$default_theme;
$default_asset_javascript_path = "assets/".$version."/public/default/frontend/standard/javascript";

//,
foreach($themes as $theme){
    // OLD ASSETS THEME JAVASCRIPT PATH TO COPY FILES FROM
    if($old_assets_version!=''){
        $old_assets_version_javascript_path =  "assets/".$old_assets_version."/public/default/frontend/".$theme."/javascript";
    }
	$asset_destination_path = 	"assets/".$version."/public/default/frontend/".$theme;
	$theme_tpl_path 		= 	"application/core/views/frontend/".$theme;
	$asset_javascript_path 	= 	$asset_destination_path."/javascript";
	
    echo "Uglifying theme  ".$theme." ...\n";	
    if (!file_exists($asset_javascript_path)) {
            mkdir($asset_javascript_path, 0777, true);
    }
	//if($theme != $default_theme) { // styles, images & fonts are not present in the common template
		exec("cp -r public/default/frontend/".$theme."/styles ".$asset_destination_path."");
		exec("cp -r public/default/frontend/".$theme."/images ".$asset_destination_path."");
		exec("cp -r public/default/frontend/".$theme."/fonts ".$asset_destination_path."");
	//}
    // IF old assets version exists copy JS files from old path to new version path, else uglify 
    if($old_assets_version!=''){
        exec("cp -r $old_assets_version_javascript_path $asset_destination_path");
    } else {
        $files = find_all_files($theme_tpl_path,$default_theme_tpl_path,$tpl_file_list,$theme); // Get tpl file list for the theme
        foreach($files as $file){ // loop through all tpl files
			$str 	= 	find_script_tags($file,$theme);
			$html	= 	str_get_html($str);
			$js		=	$theme_js_files	=	array();
			if(!empty($html)){
				foreach($html->find('script') as $script) { // get list of JS file included in the tml
					if(isset($script->attr['src'])){
						if(strpos($script->attr['src'],'default')!=false){
							$js[]=$script->attr['src'];
						}						
					}
				}
				if(!empty($js)){ 
					//Get tpl file name after memoving file path. 
					//Name of the the compiled js file should be same as current tpl file name, but replace '/' with '.'
					$tpl_name 		= 	str_replace($theme_tpl_path."/",'',$file);
					if( !strpos($file,$theme) ){
						$tpl_name 		= 	str_replace($default_theme_tpl_path."/",'',$file); // 
					}
					$tpl_name 		= str_replace('/','.',strtolower($tpl_name)); 
					$output_js 		= $asset_javascript_path."/".$tpl_name;
					$output_js 		= str_replace('.tpl','.js',$output_js);		
					$output_js_default = $default_asset_javascript_path."/".$tpl_name;
					$output_js_default = str_replace('.tpl','.js',$output_js_default);
					if($theme == $default_theme){
						// Create compressed js file which include all js files in the tpl
						exec(uglifyjs.' '.implode(" ",$js).' -m  -o '.$output_js.' -c  warnings=false');
						echo $output_js."\n";
					} else	{
						copy($output_js_default,$output_js);
					}
				}
			}
		}
    }
}
?>
