<?php
/*!
 * Nginx 初始化脚本
**/
anmp_log('EXEC FILE   [AnmpReload.nginx.php]');

// 注入配置变量
$_ENV['ANMP_NGINX_XMLS'] = glob(ANMP_ROOT.'/serv/nginx/winsw/*.xml');
$_ENV['ANMP_NGINX_HOST'] = glob(ANMP_ROOT.'/conf/nginx-vhosts/nginx-*.*.conf');


// print_r( $_ENV['ANMP_NGINX_XMLS'] );


// 解析服务配置文件
foreach ($_ENV['ANMP_NGINX_XMLS'] as $filename) 
{
	$find_serv = '#([A-Z][:][\w\.\-_/]+)(/serv/)([\w\.\-/]+)#u';
	$repl_serv = ANMP_ROOT."/serv/$3";

	$find_data = '#([A-Z][:][\w\.\-_/]+)(/data/)([\w\.\-/]+)#u';
	$repl_data = ANMP_ROOT."/data/$3";

	$content = file_get_contents($filename);
	preg_match_all($find_serv, $content, $match_serv);
	preg_match_all($find_data, $content, $match_data);

	if (ANMP_DEBUG) {
		// print_r($match_serv);
		// print_r($match_data);
		continue;
	}

	$content = preg_replace($find_serv, $repl_serv, $content);
	$content = preg_replace($find_data, $repl_data, $content);
	file_put_contents($filename, $content);
}

// print_r( $_ENV['ANMP_NGINX_HOST'] );
// 解析主机配置文件
foreach ($_ENV['ANMP_NGINX_HOST'] as $filename) 
{
	$find_www = '#([A-Z][:][\w\.\-_/]+)(/www/)([\w\.\-/]+)#u';
	$repl_www = ANMP_ROOT."/www/$3";

	$find_conf = '#([A-Z][:][\w\.\-_/]+)(/conf/)([\w\.\-/]+)#u';
	$repl_conf = ANMP_ROOT."/conf/$3";

	$find_data = '#([A-Z][:][\w\.\-_/]+)(/data/)([\w\.\-/]+)#u';
	$repl_data = ANMP_ROOT."/data/$3";


	$content = file_get_contents($filename);
	$content = preg_replace($find_www,  $repl_www,  $content);
	$content = preg_replace($find_conf, $repl_conf, $content);
	$content = preg_replace($find_data, $repl_data, $content);
	file_put_contents($filename, $content);
}
