@echo off
color 0b

echo 该批处理将帮助您控制Wamp下的Apache web服务器。

rem 设置路径
set wamp_home=%~dp0
set apache_bin=%wamp_home%apache2.2\bin\

:select
echo =====请选择要进行的操作=====
echo [0]退出 [1]启动 [2]重启 [3]停止 [4]安装并启动 [5]停止并卸载
set /p type=:

if %type% == 0 goto exit
if %type% == 1 goto start
if %type% == 2 goto restart
if %type% == 3 goto stop
if %type% == 4 goto install
if %type% == 5 goto uninstall
if %type% == 6 goto inital

if %type% GTR 6 goto end
if %type% LSS 1 goto end

:start
echo 启动Apache2.2...
net start | find /i "Apache2.2">NUL
if %errorlevel%==0 (
echo 服务已经启动，终止当前启动操作。
goto end)
net start Apache2.2
goto end

:restart
echo 重新启动Apache2.2...
net start | find /i "Apache2.2">NUL
if %errorlevel%==0 net stop Apache2.2
net start Apache2.2
goto end

:stop
echo 停止Apache2.2...
net start | find /i "Apache2.2">NUL
if %errorlevel%==0 (
net stop Apache2.2
)ELSE (
echo 未检测到服务在运行。)
goto end

:install
echo 安装Apache2.2作为服务...
%apache_bin%httpd -k install
net start Apache2.2
goto end

:uninstall
echo 卸载Apache2.2服务...
net start | find /i "Apache2.2">NUL
if %errorlevel%==0 net stop Apache2.2
%apache_bin%httpd -k uninstall

goto end

:inital
echo 初始化Apache配置文件，当第一次安装或者Wamp目录结构变动时需要使用。

echo -----生成apache配置文件到%wamp_home%apache2.2/conf/httpd.conf...
if exist %wamp_home%apache2.2/conf/httpd.conf (
	set /p confirm=确认覆盖原配置文件么[y/n]:
) else (
	set confirm=y
)

if %confirm% == y (
%wamp_home%tools\sed\sed.exe s/@@ServerRoot@@/\/Wamp\/apache2.2/g;s/@@Port@@/80/g;s/@@ServerAdmin@@/wamp@loaclhost/g;s/@@DocumentRoot@@/\/Wamp\/www/g  %wamp_home%apache2.2/conf/original/httpd.conf > %wamp_home%apache2.2/conf/httpd.conf
)

if not exist  %wamp_home%/www/index.html (
 echo -----生成一个默认页面到 %wamp_home%/www/index.html...
 echo Wamp apache web server works! > %wamp_home%/www/index.html
)

echo 完成初始化。
goto end

:end
goto select

:exit
echo 脚本结束。
pause