#!/Applications/xampp/xamppfiles/bin/php
<?php
set_hosts();
set_phpini();
set_httpd_conf();
set_vhosts_conf();
$HOME = $_ENV['HOME'];
exec("/bin/chmod -R a+w $HOME/QT");
exit;

function set_hosts() {
	$filename = "/etc/hosts";
	$pattern = "/^\s*127\.0\.0\.1\s*quicty\.local/";
	$add_line = "127.0.0.1\tquicty.local\n";
	
	if(!is_line_exists($filename,$pattern)) {
		//echo "add line: $add_line to $filename\n";
		file_put_contents($filename, $add_line, FILE_APPEND);
	}
}

function set_phpini() {
	$filename = "/Applications/xampp/etc/php.ini";
	$pattern = "/^\s*memory_limit\s*?=\s*?(\d+)M/";
	$replace_line = "memory_limit = 32M      ; Maximum amount of memory a script may consume (8MB)";
	
	if($value = get_value($filename,$pattern)) {
		if($value < 32) {
			$result = set_value($filename,$pattern,$replace_line);
			//echo "rewrite value $value M -> 32M\n";
		} else {
			//echo "don't rewrite value $value M is over 32M\n";
		}
	} else {
		file_put_contents($filename, $replace_line, FILE_APPEND);
		//echo "add line: $replace_line to $filename\n";
	}
	
        $pattern = "/^\s*upload_tmp_dir\s*?=/";
	$add_line = "upload_tmp_dir = /tmp";

	if(!is_line_exists($filename,$pattern)) {
		file_put_contents($filename, $add_line, FILE_APPEND);
	}
						

	$patterns = array(
		1=>'mbstring.language = Japanese',
		2=>'mbstring.internal_encoding = EUC-JP',
		3=>'mbstring.http_input = auto',
		4=>'mbstring.detect_order = auto',
	);
	$needs = array(
		1=>true, 2=>true, 3=>true, 4=>true, 
	);
	$needCount = 4;
	$file = file($filename);
	foreach($file as $line) {
		foreach($patterns as $key=>$pattern) {
			if(preg_match("/^\s*$pattern/", $line)) {
				//echo "match : $line";
				$needs[$key] = false;
				$needCount--;
			}
		}
	}
	
	if($needCount) {
		foreach($file as $line) {
			$new_file .= $line;
			foreach($patterns as $key=>$pattern) {
				if($needs[$key] AND preg_match("/^;$pattern/", $line)) {
					$new_file .= $pattern."\n";
					$needs[$key] = false;
				}
			}
		}
		//echo $new_file;
		file_put_contents($filename, $new_file);
	}
}

function set_httpd_conf() {
	$filename = "/Applications/xampp/etc/httpd.conf";
	$pattern = "/^\s*Include \/Applications\/xampp\/etc\/extra\/httpd-vhosts.conf/";
	$add_line = "Include /Applications/xampp/etc/extra/httpd-vhosts.conf\n";
	
	if(!is_line_exists($filename,$pattern)) {
		//echo "add line: $add_line to $filename\n";
		file_put_contents($filename, $add_line, FILE_APPEND);
	}
}

function set_vhosts_conf() {
	$HOME = $_ENV['HOME'];
	$filename = "/Applications/xampp/etc/extra/httpd-vhosts.conf";
	$patterns = array(
		1=>'NameVirtualHost \*:80',
		2=>'DocumentRoot.+/Applications/xampp/xamppfiles/htdocs',
		3=>"DocumentRoot.*$HOME/QT/qtbuilder/htdocs",
	);
	$name_virtual = "NameVirtualHost *:80";
	$xampp_vhost = get_xampp_vhost();
	$qtbuilder_vhost = get_qtbuilder_vhost($HOME);
	$add_lines = array(
		1=>$name_virtual,
		2=>$xampp_vhost,
		3=>$qtbuilder_vhost,
	);

	$needs = array(
		1=>true, 2=>true, 3=>true,
	);
	$needCount = 3;
	
	$file = file($filename);
	foreach($file as $line) {
		foreach($patterns as $key=>$pattern) {
			if(preg_match("@^\s*$pattern@", $line)) {
				//echo "match : $line";
				$needs[$key] = false;
				$needCount--;
			}
		}
	}
	//print_r($needCount);
	if($needCount) {
		foreach($patterns as $key=>$pattern) {
			if($needs[$key]) {
				$new_file .= $add_lines[$key]."\n";
			}
		}
		////echo $new_file;
		file_put_contents($filename, $new_file, FILE_APPEND);
	}
}

function get_xampp_vhost() {
	$text = <<<EOS
<VirtualHost *:80>
    DocumentRoot /Applications/xampp/xamppfiles/htdocs
    ServerName localhost
    Options FollowSymLinks
</VirtualHost>
EOS;
	return $text;
}


function get_qtbuilder_vhost($HOME) {
	$text = <<<EOS
<VirtualHost *:80>
    DocumentRoot $HOME/QT/qtbuilder/htdocs
    ServerName quicty.local
    Options FollowSymLinks
    RewriteEngine On
    RewriteRule ^/(.*)/(css|errors|img|js)/(.*) $HOME/QT/$1/htdocs/$2/$3 [L]
    RewriteRule ^/(.*)/(.*)/(.*)/(.*) $HOME/QT/$1/htdocs/index.php [L,E=QtRootShift:Yes]
    RewriteRule ^/(.*)/(.*)/(.*) $HOME/QT/$1/htdocs/index.php [L,E=QtRootShift:Yes]
    RewriteRule ^/(.*)/(.*) $HOME/QT/$1/htdocs/index.php [L,E=QtRootShift:Yes]
</VirtualHost>

<DirectoryMatch "$HOME/QT">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride FileInfo
    Order allow,deny
    Allow from all
</DirectoryMatch>
EOS;
	return $text;
}

//-----------------------------------------

function is_line_exists($filename,$pattern) {
	$file = file($filename);
	foreach($file as $line) {
		if(preg_match($pattern, $line)) {
			//echo "match : $line";
			$result = true;
		}
	}
	return $result;
}

function get_value($filename,$pattern) {
	$result = 0;
	$file = file($filename);
	foreach($file as $line) {
		if(preg_match($pattern, $line, $matches)) {
			//echo "match : $line";
			$result = $matches[1];
		}
	}
	return $result;
}

function set_value($filename,$pattern,$replace_line) {
	$result = false;
	$file = file($filename);
	foreach($file as $line) {
		if(preg_match($pattern, $line, $matches)) {
			//echo "match : $line";
			$new_file .= $replace_line;
			$result = true;
		} else {
			$new_file .= $line;
		}
	}
	//echo $new_file;
	file_put_contents($filename, $new_file);
	return $result;
}


?>
