<?
	function recurse_copy($src, $dst){
		$dir = opendir($src);
		@mkdir($dst);
		while(false !==($file = readdir($dir))){
			if(($file != '.') && ($file != '..') && ($file != '.svn')){
				if(is_dir($src.'/'.$file)){
					recurse_copy($src.'/'.$file, $dst.'/'.$file);
				}else{
					copy($src.'/'.$file, $dst.'/'.$file);
				}
			}
		}
		closedir($dir);
	}
	
		
	header("Content-Type: text/html;charset=utf-8");
	
	$g_path = 'my/';
	
	include $g_path."u_globals.php";
	include $g_path."html/f.php";
	include $g_path."u_spoj_na_bazu.php";
	
	$can_live_alone = 1;
	include $g_path."u_headers.php";
	
	
	$l_db_prefix = '';
	
	$l_tip = get_item('select value from settings where name = "tipprog" ');
	
	if($l_tip == 'u'){
		$l_prefix = 'ugo';
		$l_db_prefix = 'ugo_';
	}
	if($l_tip == 'm'){
		$l_prefix = 'malo';
		$l_db_prefix = 'malo_';
	}
	
	if($l_db_prefix){
		$l_path = get_item('select value from settings where name = "putanja.http" ');
		$l_path_abs = get_item('select value from settings where name = "putanja.abs" ');
		
		$l_path_rev = strrev($l_path);
		
		if($l_path_rev[0] == '\\' || $l_path_rev[0] == '/'){
			$l_path_rev[0] = '';
		}
		$l_pos = strpos($l_path_rev, '\\');
		if($l_pos !== false){
			$l_path_rev = substr($l_path_rev, $l_pos);
		}else{
			$l_pos = strpos($l_path_rev, '/');
			if($l_pos !== false){
				$l_path_rev = substr($l_path_rev, $l_pos);
			}
		}
		$l_path_new = strrev($l_path_rev);
		
		$l_razlika = str_replace($l_path_new, '', $l_path);
		
		$l_path_new_abs = str_replace($l_razlika, '', $l_path_abs);
		
		
		$l_path_new .= $l_db_prefix.$_GET['year'];
		$l_path_new_abs .= $l_db_prefix.$_GET['year'];
		
		if(is_dir($l_path_new_abs)){
		}else{
			recurse_copy($l_path_abs, $l_path_new_abs);
			
			
			/*  ************************************************ */
			$l_db_file = $l_path_new_abs.'/app/config/database.php';
			
			$handle = @fopen($l_db_file, 'r');
			$contents = '';
			if($handle){
				while (!feof($handle)) {
					$l_line = fgets($handle, 4096);
					if(strstr($l_line, '//') && strstr($l_line, '://') === false){
						continue;
					}
					
					if(strstr($l_line, "g_baza = ")){
						$l_line = "$"."g_baza = '".$l_db_prefix.$_GET['year']."';\n";
					}
					if(strstr($l_line, "'database' =>")){
						$l_line = "'database' => '".$l_db_prefix.$_GET['year']."',\n";
					}
					
					$contents .= $l_line;
				}
				fclose($handle);
			}
			
			
			$handle2 = @fopen($l_db_file, 'w');
			fwrite($handle2, $contents);
			fclose($handle2);
			
			/*  ************************************************ */
			
			
			$l_db_file = $l_path_new_abs.'/app/views/layouts/default.ctp';
			
			$handle = @fopen($l_db_file, 'r');
			$contents = '';
			if($handle){
				while (!feof($handle)) {
					$l_line = fgets($handle, 4096);
					
					if(strstr($l_line, '<div id="mailer-message" style="display:none">')){
						$l_line = '<div id="old-year" style="width:100%; height:61px; padding:0; margin:0; background:#aaffaa; overflow:hidden; ">'."\n".
							'<div id="old-year-inner" style="color:#ff2222; font-size:18pt; height:35px; line-height:160%; padding:8px 20px 12px 20px; border-style:solid; border-color:#ff0000; border-width:3px; ">'.
							'Stara godina <b>'.intval($_GET['year']).'</b>'."\n".
							'</div>'."\n".
							'</div>'."\n".
							'<div id="mailer-message" style="display:none">'."\n";
					}
					
					$contents .= $l_line;
				}
				fclose($handle);
			}
			
			
			$handle2 = @fopen($l_db_file, 'w');
			fwrite($handle2, $contents);
			fclose($handle2);
			
			
			
			
			/*  ************************************************ */
			$l_db_file = $l_path_new_abs.'/app/webroot/iso/my/html/c_time.php';
			
			$handle = @fopen($l_db_file, 'r');
			$contents = '';
			if($handle){
				while (!feof($handle)) {
					$l_line = fgets($handle, 4096);
					
					if(strstr($l_line, 'return u_godina($date_time);/*REPLACE YEAR INTO OLD YEAR*/')){
						$l_line = 'return u_godina("'.intval($_GET['year']).'-01-01");'."\n";
					}
					
					$contents .= $l_line;
				}
				fclose($handle);
			}
			
			
			$handle2 = @fopen($l_db_file, 'w');
			fwrite($handle2, $contents);
			fclose($handle2);
			/*  ************************************************ */
			
			
			
			
			/*  ************************************************ */
			$l_db_file = $l_path_new_abs.'/app/webroot/izvjestaji/c_time.php';
			
			$handle = @fopen($l_db_file, 'r');
			$contents = '';
			if($handle){
				while (!feof($handle)) {
					$l_line = fgets($handle, 4096);
					
					if(strstr($l_line, 'return u_godina($date_time);/*REPLACE YEAR INTO OLD YEAR*/')){
						$l_line = 'return u_godina("'.intval($_GET['year']).'-01-01");'."\n";
					}
					
					$contents .= $l_line;
				}
				fclose($handle);
			}
			
			
			$handle2 = @fopen($l_db_file, 'w');
			fwrite($handle2, $contents);
			fclose($handle2);
			/*  ************************************************ */
			
			
			
			
			/*  ************************************************ */
			unlink($l_path_new_abs.'/app/webroot/iso/extra-info-'.$l_tip.'.txt');
			/*  ************************************************ */
			
			
			mysql_select_db($l_db_prefix.$_GET['year']);
			
			$l_sql = 'update settings set value = "'.str_replace('\\', '\\\\', $l_path_new_abs).'/" where name = "putanja.abs" ';
			sql_exe($l_sql);
			
			$l_sql = 'update settings set value = "'.$l_path_new.'/" where name = "putanja.http" ';
			sql_exe($l_sql);
		}
		
		header('location: '.$l_path_new.'/home');
		exit;
	}
?>
