@echo off
setlocal enabledelayedexpansion
title 安装先锋云盘windows服务器环境
echo.
echo ****************************************
echo.
echo 开始安装apache2.4，请不要关闭窗口
echo.
echo 请以管理员身份运行
echo.
echo ****************************************
pause
echo.
echo 设置安装路径
echo.
set cds=%~dp0
set cpf=%~d0
set cpdpath=%~p0
set apachepath=%cds%Server\Apache24\
echo.
rem 安装APACHE的VC2017库支持
set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\14.0\VC\Runtimes\x64"
set VALUE_NAME=VC_redis.x64
FOR /F "usebackq skip=4 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% 2^>nul`) DO (
    set ValueName=%%A
    set ValueType=%%B
    set ValueValue=%%C
)
if defined ValueName (
    @echo 已经安装过%VALUE_NAME%组件 跳过安装
) else (
    %cds%include\VC_redist.x64.exe /q
)
pause
echo 安装路径：%apachepath%
::pause
set conf_file=%cds%include\httpd-default.conf
set default_root=C:\XfplayWebRoot\
rem 读取httpd.conf文件所有内容
for /f "delims=" %%i in (%conf_file%) do (
rem 设置变量a为每行内容
set str=%%i
rem 如果该行有  则写多个set
set "str=!str:%default_root%=%cds%!"
rem 把修改后的全部行存入$
echo !str!>>$ )
rem 用$的内容替换原来root.cfg内容
move $ %apachepath%conf\httpd.conf
::pause
echo.
echo 设置vhost相关信息
set vhost_file=%cds%include\www.xfclouds.conf
set dcpf=C:
set dcpd=XfplayWebRoot
set "cpd=%cpdpath:\=/%"
set "cpd=%cpd:~1,-1%"
rem 读取 www.xfclouds.conf 文件所有内容
for /f "delims=" %%i in (%vhost_file%) do (
rem 设置变量a为每行内容
set str=%%i
rem 如果该行有 则写多个set
set "str=!str:%default_root%=%cds%!"
set "str=!str:%dcpf%=%cpf%!"
set "str=!str:%dcpd%=%cpd%!"
rem 把修改后的全部行存入$
echo !str!>>$ )
rem 用$的内容替换原来root.cfg内容
move $ %apachepath%conf\vhost\www.xfclouds.conf
::pause
echo.
echo 配置php.ini文件
set phpini_file=%cds%include\php-default.ini
rem 读取 www.xfclouds.conf 文件所有内容
for /f "delims=" %%i in (%phpini_file%) do (
rem 设置变量a为每行内容
set str=%%i
set "str=!str:%default_root%=%cds%!"
rem 把修改后的全部行存入$
echo !str!>>$ )
move $ %cds%Server\php\php7.2.31nts\php.ini
::pause
echo.
echo 配置mysql 的.ini文件
set myini_file=%cds%include\my-default.ini
for /f "delims=" %%i in (%myini_file%) do (
rem 设置变量a为每行内容
set str=%%i
set "str=!str:%dcpf%=%cpf%!"
set "str=!str:%dcpd%=%cpd%!"
rem 把修改后的全部行存入$
echo !str!>>$ )
move $ %cds%Server\mysql\MySQL5.6\my.ini
::pause
echo 正在启动apache服务
NET stop XfcloudApache
%apachepath%\bin\httpd.exe -k uninstall -n "XfcloudApache"
%apachepath%\bin\httpd.exe -k install -n "XfcloudApache"
echo 启动apache服务成功 服务名为  XfcloudApache
NET start XfcloudApache
::pause
rem 生成mysql的随机密码
set mysqlPassword=%cds%include\my-pass.ini
set /P myqlnp=<%mysqlPassword%
for %%i in ("0=A" "1=B" "2=C" "3=D" "4=E" "5=F")do set "x1%%~i"
setlocal enabledelayedexpansion
set "guid="
for /l %%i in (1,1,32)do (
set /a "n=!random!&15"
if !n! gtr 9 call set "n=%%x!n!%%"
set "guid=!guid!!n!")
set "guid=%guid:~,8%%guid:~8,4%%guid:~12,4%"
echo %guid%>%cds%www.xfclouds.com\config\mysql-password.ini
echo %guid%>%cds%include\my-pass.ini
echo ----------------------
set service_name=XfcloudMysql56
NET stop %service_name%
rem rd /s /q %cds%Server\mysql\MySQL5.6\data
%cds%Server\mysql\MySQL5.6\bin\mysqld.exe --remove %service_name%
rem md %cds%Server\mysql\MySQL5.6\data
rem %cds%Server\mysql\MySQL5.6\bin\mysqld.exe --initialize --user=mysql --console
echo.
%cds%Server\mysql\MySQL5.6\bin\mysqld.exe --install %service_name% --defaults-file=%cds%Server/mysql/MySQL5.6/my.ini
NET start %service_name%
rem 这两条不需要
rem %cds%Server\mysql\MySQL5.6\bin\mysql.exe -P23306 -uroot -p%myqlnp% -e "DROP database xfcloudwin;"
rem %cds%Server\mysql\MySQL5.6\bin\mysql.exe -P23306 -uroot -p%myqlnp% -e "CREATE database xfcloudwin;"
%cds%Server\mysql\MySQL5.6\bin\mysqladmin.exe -P23306 -uroot -p%myqlnp% password %guid%
%cds%Server\mysql\MySQL5.6\bin\mysqladmin.exe -P23306 -uxfcloudwin -p%myqlnp% password %guid%
echo.
del %cds%www.xfclouds.com\config\config.php
rem 生成 自动安装配置文件
set xfcloudData=%cds%www.xfclouds.com/data
set "xfcloudDataPath=%xfcloudData:\=/%"
>%cds%www.xfclouds.com\config\autoconfig.php (
	echo ^<^?php 
	echo ^$AUTOCONFIG = array(
	echo  "dbtype"        =^> "mysql",
	echo  "dbname"        =^> "xfcloudwin",
	echo  "dbuser"        =^> "xfcloudwin",
	echo  "dbpass"        =^> "%guid%",
	echo  "dbhost"        =^> "localhost:23306",
	echo  "dbtableprefix" =^> "oc_",
	echo  "directory"     =^> "%xfcloudDataPath%",
	echo ^);
)
::pause
echo 安装 redis服务
echo.
net stop xfcloudredis
%cds%Server\redis\redis-server.exe --service-install %cds%Server\redis\redis.windows.conf --service-name xfcloudredis --port 26237 --loglevel verbose
net start xfcloudredis
rem 创建openssl.cnf的文件夹位置
md C:\usr\local\ssl
copy %cds%include\openssl.cnf  C:\usr\local\ssl\openssl.cnf
echo ****************************************
echo ===========================
echo ===先锋云盘环境安装成功！======
echo  http://localhost:20081
echo  访问以上网址进行云盘安装
echo  MySQL56监听端口号23306
echo  Redis监听端口号26237
echo  请保存好mysql的root账号密码
echo %guid%
echo ==========================
echo ****************************************
path=%path%; C:\Program Files\Internet Explorer\iexplore.exe
start iexplore http://localhost:20081
cmd /k echo.
