<?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');

$version 	= 'v1';//isset($argv[1])?$argv[1]:'v1';
$theme 		= 'custom';//isset($argv[2])?$argv[2]:'standard';

$asset_path 			= 	"assets/".$version."/public/default/frontend/".$theme;
$theme_path 			= 	"application/core/views/frontend/".$theme;
$asset_javascript_path 	= 	$asset_path."/javascript";

if (!file_exists($asset_javascript_path)) {
	mkdir($asset_javascript_path, 0777, true);
}

exec("cp -r public/default/frontend/".$theme."/styles ".$asset_path."");
exec("cp -r public/default/frontend/".$theme."/images ".$asset_path."");
exec("cp -r public/default/frontend/".$theme."/fonts ".$asset_path."");

$files = find_all_files($theme_path); 

foreach($files as $file){
	//if($file=='application/core/views/frontend/'.$theme.'/fbstore/Index.tpl')
	{
	$str 	= 	find_script_tags($file,$theme);
	$html	= 	str_get_html($str);
	$js		=	array();
	if(!empty($html)){
		foreach($html->find('script') as $script) {
			//check whether the file found in server
			if(isset($script->attr['src'])){
				if(strpos($script->attr['src'],'default')!=false){
					$js[]=$script->attr['src'];
				}
			}
		}
		if(!empty($js)){
			$tpl_name 		= 	str_replace($theme_path."/",'',$file);
			$tpl_name 		= 	str_replace('/','.',strtolower($tpl_name));
			$output_js 		= 	$asset_javascript_path."/".$tpl_name;
			$output_js 		= 	str_replace('.tpl','.js',$output_js);
			exec(uglifyjs.' '.implode(" ",$js).' -m  -o '.$output_js.' -c  warnings=false');
		}
		
	}
	}
}


?>