@echo off
rem Honepage: http://PHPnow.org - YinzCN_at Gmail.com
Call Pn\config.cmd
setlocal
if not "%1"=="" goto %1

set pndir=%CD%

Prompt -$g
Title 初始化 Apache 2.2 + PHP %php_ver% + MySQL %mysql_ver% - PHPnow.org
echo   ____________________________________________________________
echo  ^|                                                            ^|
echo  ^|    PHPnow.org  -  绿色 PHP 环境套件包                      ^|
echo  ^|____________________________________________________________^|
echo.

%Pnvbs% "" || pause && goto end

if not exist %windir%\system32\inetsrv\inetinfo.exe goto chk_apache
echo   ____________________________________________________________
echo  ^|                                                            ^|
echo  ^|    警告:                                                   ^|
echo  ^|    IIS 已安装, 如果同时使用 80 端口, Apache 将不能启动!    ^|
echo  ^|____________________________________________________________^|
set /p stopiis= -^> 尝试暂时停止 IIS(w3svc) 吗? (y/N) 
echo.
if "%stopiis%"=="y" goto stopiis
if "%stopiis%"=="Y" goto stopiis
goto chk_apache


:stopiis
net stop w3svc


:chk_apache
if not exist Apache\logs\httpd.pid goto copy
echo   ____________________________________________________________
echo  ^|                                                            ^|
echo  ^|    检测到 Apache 正在运行. 正在停止 ……                   ^|

cd Apache
bin\httpd.exe -k stop -n %htd_srv_name%
bin\httpd.exe -k uninstall -n %htd_srv_name%
cd ..

echo  ^|____________________________________________________________^|
echo.


:copy
echo   ____________________________________________________________
echo  ^|                                                            ^|
echo  ^|    开始文件处理 ...                                        ^|
echo.
rem 复制 DLL
copy php-%php_ver%-Win32\php5ts.dll Apache\bin /Y
copy php-%php_ver%-Win32\libmhash.dll Apache\bin /Y
copy php-%php_ver%-Win32\libmcrypt.dll Apache\bin /Y
copy php-%php_ver%-Win32\libmysql.dll Apache\bin /Y

copy Pn\eAccelerator%ea_ver%_%php_ver%.dll php-%php_ver%-Win32\ext /Y

rem 建立目录
if not exist Apache\conf\extra md Apache\conf\extra
if not exist Apache\logs md Apache\logs
if not exist Temp\session.save_path md Temp\session.save_path

copy Apache\conf\default\magic Apache\conf /Y
copy Apache\conf\default\mime.types Apache\conf /Y

rem 初始配置 my.ini
if exist MySQL-%mysql_ver%\my.ini copy MySQL-%mysql_ver%\my.ini MySQL-%mysql_ver%\my.ini.bak /Y
copy MySQL-%mysql_ver%\my-medium.ini MySQL-%mysql_ver%\my.ini /Y

rem 初始配置 httpd.conf, php-5.x.x-apache2handler.ini
%Pnvbs% "init `%php_ver%`, `%ea_ver%`, %htd_p%"


%Pnvbs% "conf htdcf, `^#(Include.*vhosts.conf)$`, `$1`"
if exist %vh% goto endvh

echo.>%vh%
echo # PHPnow.org - vhosts>>%vh%
echo.>>%vh%
echo ^<Directory "../vhosts"^>>>%vh%
echo     Options FollowSymLinks>>%vh%
echo     AllowOverride All>>%vh%
echo     Order allow,deny>>%vh%
echo     Allow from all>>%vh%
echo ^</Directory^>>>%vh%
echo.>>%vh%
echo NameVirtualHost *>>%vh%
echo.>>%vh%
echo ^<VirtualHost *^>>>%vh%
echo     ServerAdmin root@localhost>>%vh%
echo     DocumentRoot ../htdocs>>%vh%
echo     ServerName defalut:%htd_p%>>%vh%
echo     ErrorLog logs/defalut-error_log>>%vh%
echo ^</VirtualHost^>>>%vh%
:endvh

copy Pn\pn_default htdocs\index.php /Y

rem 初始配置 phpMyAdmin
if exist htdocs\phpMyAdmin\config.inc.php copy htdocs\phpMyAdmin\config.inc.php htdocs\phpMyAdmin\config.inc.php.bak /Y
copy htdocs\phpMyAdmin\config.sample.inc.php htdocs\phpMyAdmin\config.inc.php /Y

%Pnvbs% "conf `htdocs\phpMyAdmin\config.inc.php`, `\$cfg\['blowfish_secret'\] = \'.*\';`, `$cfg['blowfish_secret'] = 'PHPnow-%COMPUTERNAME%-%RANDOM%';`

echo.
echo  ^|    文件处理完成.                                           ^|
echo  ^|____________________________________________________________^|
echo.


:start_apache
echo   ____________________________________________________________
echo  ^|                                                            ^|
echo  ^|    正在启动 Apache ...                                     ^|
echo.
cd Apache

:start_apache_1
bin\httpd.exe -k install -n %htd_srv_name% && goto start_apache_2
if errorlevel 1 goto start_apa_false

:start_apache_2
bin\httpd.exe -k start -n %htd_srv_name% && goto done_apache

:start_apa_false
cd ..
echo   ____________________________________________________________
echo  ^|                                                            ^|
echo  ^|    Apache 2.2 启动失败! 不能继续.                          ^|
echo  ^|    如果端口占用, 可先通过 PnConf.cmd 更改端口再 Init.cmd   ^|
echo  ^|    * 前面出现的异常信息有助于解决问题.                     ^|
echo  ^|____________________________________________________________^|
echo.
Apache\bin\httpd.exe -k uninstall -n %htd_srv_name%
pause & goto end


:done_apache
cd ..
if exist Apa_Start.cm_ ren *.cm_ *.cmd
echo.
echo  ^|    启动 Apache 完成.                                       ^|
echo  ^|____________________________________________________________^|
echo.


:chk_my_pid
echo   ____________________________________________________________
echo  ^|                                                            ^|
echo  ^|    正在启动 MySQL 5.0 ...                                  ^|
echo.
if not exist MySQL-%mysql_ver%\data\%COMPUTERNAME%.pid goto start_mysql
echo  ^|                                                            ^|
echo  ^|    监测到 MySQL 正在运行. 正在停止 ...                     ^|
echo.

:stop_mysql
net stop %my_srv_name%
MySQL-%mysql_ver%\bin\mysqld-nt --remove %my_srv_name%

:start_mysql
MySQL-%mysql_ver%\bin\mysqld-nt --install %my_srv_name% --defaults-file="%CD%\MySQL-%mysql_ver%\my.ini"
net start %my_srv_name% || pause

echo  ^|    启动 MySQL 5.0 完成.                                    ^|
echo  ^|____________________________________________________________^|
echo.


rem 修改 MySQL root 密码

:setpw_mysql
MySQL-%mysql_ver%\bin\mysqladmin ping -uroot -pphpnow -w

echo   ____________________________________________________________
echo  ^|                                                            ^|
echo  ^|    现在为 MySQL root 设置密码. 重要! 请切记!               ^|
echo  ^|____________________________________________________________^|

set /P my_new_pwd= -^> 设置 root 密码: 
echo.

if "%my_new_pwd%"=="" goto setpw_mysql

MySQL-%mysql_ver%\bin\mysqladmin -uroot -pphpnow password "%my_new_pwd%"

if errorlevel 1 (pause && goto done)

echo   ____________________________________________________________
echo  ^|                                                            ^|
echo        MySQL root 密码已设为 "%my_new_pwd%" , 请切记!
echo  ^|____________________________________________________________^|
echo.

rem /修改 MySQL root 密码


:done
Title 全部完成 - PHPnow.org
echo   ____________________________________________________________
echo  ^|                                                            ^|
echo  ^|    全部完成! 正常情况不会有任何错误提示.                   ^|
echo  ^|____________________________________________________________^|
echo.
pause

start http://127.1:%htd_P%
ren Init.cmd Init.cm_


:end
Prompt
