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


// 注入配置变量
$_ENV['ANMP_MYSQL_DIRS'] = glob(ANMP_ROOT.'/serv/mysql/mysql-*.*.*-win*', GLOB_ONLYDIR);
$_ENV['ANMP_MYSQL_VERS'] = array();


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


// 解析配置文件
foreach ($_ENV['ANMP_MYSQL_DIRS'] as $dir)
{
	$filename  = "{$dir}/my.ini";
	if (is_file($filename)) {
		$_ENV['ANMP_MYSQL_VERS'][basename($dir)] = $dir;
	} else {
		continue;
	}

	$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);
}


// 生成菜单项
$mysqlVerTextA = $mysqlVerTextB = '';
foreach ($_ENV['ANMP_MYSQL_VERS'] as $mysql_ver => $mysql_dir)
{
	$version = explode('-', $mysql_ver);
	$mysql_bin   = "%WRoot%/serv/mysql/{$mysql_ver}/bin/mysqld.exe";
	$mysql_desc  = "AnmpMySQL/{$version[1]} (". ucfirst($version[2]) .")";
	$mysql_glyph = ($mysql_ver === $_ENV['ANMP_CONF']['mysql_vers']) ? " Glyph: 13;\n" : "\n";

	if ($mysql_ver === $_ENV['ANMP_CONF']['mysql_vers']) {
		$_ANMP_MENU_TMPL_STR = str_replace('-MySQL-Server-Description-', $mysql_desc, $_ANMP_MENU_TMPL_STR);
	}

	$mysqlVerTextA .= "    Type: item; Caption: \"{$mysql_ver}\"; Action: multi; Actions: MultiAction.Switch_{$mysql_ver};{$mysql_glyph}";
	$mysqlVerTextB .= "\n[MultiAction.Switch_{$mysql_ver}]\n"
		// 卸载旧服务
		.'    Action: service; Service: AnmpMysqldServer; ServiceAction: stop; Flags: waituntilterminated ignoreerrors;'."\n"
		.'    Action: closeservices; Flags: ignoreerrors;'."\n"
		.'    Action: run; FileName: "SC"; Parameters: "DELETE AnmpMysqldServer"; ShowCmd: hidden; Flags: waituntilterminated ignoreerrors;'."\n"
		.'    Action: resetservices;'."\n"

		// 安装新服务
		.'    Action: run; FileName: "'. $mysql_bin .'"; Parameters: "--install-manual AnmpMysqldServer"; ShowCmd: hidden; Flags: waituntilterminated;'."\n"
		.'    Action: run; FileName: "SC"; Parameters: "CONFIG AnmpMysqldServer start= '. $_ENV['ANMP_CONF']['mysql_type'] .'"; ShowCmd: hidden; Flags: waituntilterminated;'."\n"
		.'    Action: run; FileName: "SC"; Parameters: "DESCRIPTION AnmpMysqldServer ""'. $mysql_desc .'"""; ShowCmd: hidden; Flags: waituntilterminated;'."\n"
		.'    Action: resetservices;'."\n"

		// 切换新服务
		.'    Action: run; FileName: "%WBPHPExe%"; Parameters: "AnmpSwitch.php MySQL '. $_ENV['ANMP_CONF']['mysql_vers'] .' '. $mysql_ver .'"; WorkingDir: "%WRoot%/bin/AeTrayMenu/"; Flags: waituntilterminated;'."\n"
		.'    Action: run; FileName: "%WBPHPExe%"; Parameters: "AnmpReload.php"; WorkingDir: "%WRoot%/bin/AeTrayMenu/"; Flags: waituntilterminated;'."\n"
		.'    Action: service; Service: AnmpMysqldServer; ServiceAction: startresume; Flags: ignoreerrors;'."\n"
		.'    Action: readconfig;'."\n";
}

// 替换菜单项
$mysqlVerTextA .= "    Type: separator;\n".'    Type: item; Caption: "Get more..."; Action: run; FileName: "explorer.exe"; Parameters: "http://dev.osdu.net/p/anmpserver/?anmp_addons_mysql";' . "\n";
$_ANMP_MENU_TMPL_STR = str_replace(';ANMP_MYSQL_VERSION_MENU',   $mysqlVerTextA, $_ANMP_MENU_TMPL_STR);
$_ANMP_MENU_TMPL_STR = str_replace(';ANMP_MYSQL_VERSION_SWITCH', $mysqlVerTextB, $_ANMP_MENU_TMPL_STR);


// print_r($mysqlVerTextA);
// print_r($mysqlVerTextB);
// print_r($_ANMP_MENU_TMPL_STR);

