<?php
include_once 'Text/Diff.php';
include_once 'Text/Diff/Renderer.php';
include_once 'Text/Diff/Renderer/unified.php';

class package_file extends myQuicty {

function add($package_root,$app_root,$folder_name) {
	$copy_dir = true;
	if(is_dir($package_root.'/bin')) copy_files($package_root.'/bin',$app_root.'/bin',false,0777);
	//if(is_dir($package_root.'/etc/conf')) copy_files($package_root.'/etc/conf',$app_root.'/etc/conf',false,0666);
	if(file_exists($package_root.'/etc/conf/userauth.on')) copy_files($package_root.'/etc/conf/userauth.on',$app_root.'/etc/conf',false,0666);
	if(file_exists($package_root.'/etc/conf/word_values_addition.conf'))  $this->add_includes($package_root.'/etc/conf/word_values_addition.conf',$app_root.'/etc/conf/word_values_addition.conf');
	if(is_dir($package_root.'/etc/data_def')) copy_files($package_root.'/etc/data_def',$app_root.'/etc/data_def',false,0666);
	//if(is_dir($package_root.'/etc/data_set')) copy_files($package_root.'/etc/data_set',$app_root.'/etc/data_set',false,0666);
	if(is_dir($package_root.'/etc/data_set')) $this->set_templete_files($package_root.'/etc/data_set',$app_root.'/etc/data_set');
	if(is_dir($package_root.'/etc/data_view')) copy_files($package_root.'/etc/data_view',$app_root.'/etc/data_view',false,0666);
	if(is_dir($package_root.'/etc/search_view')) copy_files($package_root.'/etc/search_view',$app_root.'/etc/search_view',false,0666);
	if(is_dir($package_root.'/etc/sql')) copy_files($package_root.'/etc/sql',$app_root.'/etc/sql',$copy_dir,0666);
	if(is_dir($package_root.'/htdocs')) copy_files($package_root.'/htdocs',$app_root.'/htdocs',false,0666);
	if(is_dir($package_root.'/htdocs/css')) copy_files($package_root.'/htdocs/css',$app_root.'/htdocs/css',false,0666);
	if(is_dir($package_root.'/htdocs/img')) copy_files($package_root.'/htdocs/img',$app_root.'/htdocs/img',false,0666);
	if(is_dir($package_root.'/htdocs/js')) copy_files($package_root.'/htdocs/js',$app_root.'/htdocs/js',false,0666);
	if(is_dir($package_root.'/lib')) copy_files($package_root.'/lib',$app_root.'/lib',false,0666);
	if(is_dir($package_root.'/lib/Pages')) $this->set_templete_files($package_root.'/lib/Pages',$app_root.'/lib/Pages'.$folder_name);
	if(is_dir($package_root.'/lib/includes')) $this->set_templete_files($package_root.'/lib/includes',$app_root.'/lib/includes');
	if(is_dir($package_root.'/lib/Plugin')) copy_files($package_root.'/lib/Plugin',$app_root.'/lib/Plugin',false,0666);
	if(is_dir($package_root.'/lib/smarty_plugins')) copy_files($package_root.'/lib/smarty_plugins',$app_root.'/lib/smarty_plugins',false,0666);
	if(is_dir($package_root.'/view')) $this->set_templete_files($package_root.'/view',$app_root.'/view'.$folder_name,$without='includes');
	if(is_dir($package_root.'/view')) $this->set_templete_files($package_root.'/view/includes',$app_root.'/view/includes','',$merge=false);
	if(is_dir($package_root.'/etc/add_includes')) $this->add_includes($package_root.'/etc/add_includes',$app_root.'/view/includes');
}

function set_templete_files($package_root,$app_root,$without='',$merge=true) {
	$this->template_dir ='';
	$this->left_delimiter = '<!--{';
	$this->right_delimiter = '}-->';

	$files = list_dir($package_root);
	foreach($files as $file_name) {
		if($file_name==$without) continue;
		$source_file = $package_root.'/'.$file_name;
		$target_file = $app_root.'/'.$file_name;
		if(is_dir($source_file)) {
			if(!file_exists($target_file)) make_dir($target_file,0777);
			$this-> set_templete_files($source_file,$target_file);
		} else {
			if(file_exists($target_file) and $merge) {
				$fpath = pathinfo($file_name);
				//$backup_file = $app_root.'/'.$file_name.'_old';
				$backup_file = "$app_root/".$fpath['dirname'].'/'.$fpath['basename'].'_old.'.$fpath['extension'];
				copy($target_file,$backup_file);
			}
			$contents = $this->fetch($source_file);
			file_write_contents($target_file,$contents);
			chmod($target_file,0666);
			if(file_exists($backup_file)) {
				$new_lines = file($target_file);
				$old_lines = file($backup_file);
				$diff = new Text_Diff($new_lines, $old_lines);
				$renderer = new Text_Diff_Renderer_unified();
				if($merge_lines = $renderer->render($diff)) {
					//$diff_file = $app_root.'/'.$file_name.'_diff';
					$diff_file =  "$app_root/".$fpath['dirname'].'/'.$fpath['basename'].'_diff.'.$fpath['extension'];
					file_write_contents($diff_file,$merge_lines);
					chmod($target_file,0666);
				} else {
					unlink($backup_file);
				}
			}
		}
	}
}

function add_includes($package_root,$app_root) {
	umask(0);
	$this->template_dir ='';
	$this->left_delimiter = '<!--{';
	$this->right_delimiter = '}-->';

	if(is_dir($package_root)) {
		$files = list_dir($package_root);
		foreach($files as $file_name) {
			$source_file = $package_root.'/'.$file_name;
			$target_file = $app_root.'/'.$file_name;
			if(is_dir($source_file)) {
				if(!file_exists($target_file)) make_dir($target_file,0777);
				$this->add_includes($source_file,$target_file);
			} else {
				$contents = $this->fetch($source_file);
				file_write_contents($target_file,$contents,$mode='a');
			}
		}
	} else {
		$contents = $this->fetch($package_root);
		file_write_contents($app_root,$contents,$mode='a');
	}
}

} // end of class add_package

?>
