#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
public_file=/www/server/panel/install/public.sh
if [ ! -f $public_file ];then
	wget -O $public_file http://download.bt.cn/install/public.sh -T 30;
fi
publicFileMd5=$(md5sum ${public_file}|awk '{print $1}')
md5check="0aa6cc891b1efd074f37feef75ec60bb"
if [ "${publicFileMd5}" != "${md5check}"  ]; then
	wget -O $public_file http://download.bt.cn/install/public.sh -T 30;
fi
. $public_file
if [ -z "${NODE_URL}" ];then
	download_Url="http://download.bt.cn"
else
	download_Url=$NODE_URL
fi
echo ${download_Url}
run_path="/root"
Is_64bit=`getconf LONG_BIT`
Run_User="www"
groupadd ${Run_User}
useradd -s /sbin/nologin -g ${Run_User} ${Run_User}

opensslVersion="1.0.2r"
curlVersion="7.64.0"
freetypeVersion="2.9.1"
pcreVersion="8.42"
Centos8Check=$(cat /etc/redhat-release | grep ' 8.' | grep -iE 'centos|Red Hat')

Repair_Init(){
	serviceName=$1
	wget -O /etc/init.d/${serviceName} ${download_Url}/init/${serviceName}
	chmod +x /etc/init.d/${serviceName}
	/etc/init.d/${serviceName} start
}
Install_OpenSSL111(){
	opensslCheck=$(/usr/local/openssl111/bin/openssl version|grep 1.1.1)
	if [ -z "${opensslCheck}" ]; then
		cd ${run_path}
		wget ${download_Url}/src/openssl-${opensslVersion}.tar.gz -T 20
		tar -zxf openssl-${opensslVersion}.tar.gz
		rm -f openssl-${opensslVersion}.tar.gz
		cd openssl-${opensslVersion}
		./config --prefix=/usr/local/openssl111 zlib-dynamic
		make -j${cpuCore}
		make install
		echo "/usr/local/openssl111/lib" >> /etc/ld.so.conf.d/openssl111.conf
		ldconfig
		cd ..
		rm -rf openssl-${opensslVersion} 
	fi
}
Install_Nghttp2(){
	if [ ! -f "/usr/local/nghttp2/lib/libnghttp2.so" ];then
		wget ${download_Url}/src/nghttp2-${nghttp2Version}.tar.gz
		tar -zxf nghttp2-${nghttp2Version}.tar.gz
		cd nghttp2-${nghttp2Version}
		export CFLAGS='-I/usr/local/openssl111/include' LIBS='-L/usr/local/openssl111/lib'
		./configure --prefix=/usr/local/nghttp2
		make -j${cpuCore}
		make install
		cd ..
		rm -rf nghttp2-${nghttp2Version}*
	fi
}
Install_Sendmail()
{
	if [ "${PM}" = "yum" ]; then
		yum install postfix mysql-libs -y
		if [ "${centos_version}" != '' ];then
			systemctl start postfix
			systemctl enable postfix	
		else
			service postfix start
			chkconfig --level 2345 postfix on
		fi
	elif [ "${PM}" = "apt-get" ]; then
		apt-get install sendmail sendmail-cf -y
	fi
}

Install_Curl()
{
	if [ ! -f "/usr/local/curl/bin/curl" ];then
		wget ${download_Url}/src/curl-${curlVersion}.tar.gz
		tar -zxf curl-${curlVersion}.tar.gz
		cd curl-${curlVersion}
		./configure --prefix=/usr/local/curl --enable-ares --without-nss --with-ssl=/usr/local/openssl
		make -j${cpuCore}
		make install
		cd ..
		rm -f curl-${curlVersion}.tar.gz
		rm -rf curl-${curlVersion}
	fi
}

Install_Openssl()
{
	if [ ! -f "/usr/local/openssl/bin/openssl" ];then
		cd ${run_path}
		wget ${download_Url}/src/openssl-${opensslVersion}.tar.gz
		tar -zxf openssl-${opensslVersion}.tar.gz
		cd openssl-${opensslVersion}
		./config --openssldir=/usr/local/openssl zlib-dynamic shared
		make -j${cpuCore} 
		make install
		echo  "/usr/local/openssl/lib" > /etc/ld.so.conf.d/zopenssl.conf
		ldconfig
		cd ..
		rm -f openssl-${opensslVersion}.tar.gz
		rm -rf openssl-${opensslVersion}
	fi	
}
Install_Pcre(){
	Cur_Pcre_Ver=`pcre-config --version|grep '^8.' 2>&1`
	if [ "$Cur_Pcre_Ver" == "" ];then
		wget -O pcre-${pcreVersion}.tar.gz ${download_Url}/src/pcre-${pcreVersion}.tar.gz -T 5
		tar zxf pcre-${pcreVersion}.tar.gz
		rm -f pcre-${pcreVersion}.tar.gz
		cd pcre-${pcreVersion}
		if [ "$Is_64bit" == "64" ];then
			./configure --prefix=/usr --docdir=/usr/share/doc/pcre-${pcreVersion} --libdir=/usr/lib64 --enable-unicode-properties --enable-pcre16 --enable-pcre32 --enable-pcregrep-libz --enable-pcregrep-libbz2 --enable-pcretest-libreadline --disable-static --enable-utf8  
		else
			./configure --prefix=/usr --docdir=/usr/share/doc/pcre-${pcreVersion} --enable-unicode-properties --enable-pcre16 --enable-pcre32 --enable-pcregrep-libz --enable-pcregrep-libbz2 --enable-pcretest-libreadline --disable-static --enable-utf8
		fi
		make -j${cpuCore}
		make install
		cd ..
		rm -rf pcre-${pcreVersion}
	fi
}
Install_Freetype()
{
	if [ ! -f "/usr/bin/freetype-config" ] && [ ! -f "/usr/local/freetype/bin/freetype-config" ]; then
		cd ${run_path}
		wget -O freetype-${freetypeVersion}.tar.gz ${download_Url}/src/freetype-${freetypeVersion}.tar.gz -T 5
		tar zxf freetype-${freetypeVersion}.tar.gz
		cd freetype-${freetypeVersion}
		./configure --prefix=/usr/local/freetype --enable-freetype-config
		make -j${cpuCore}
		make install
		cd ../
		rm -rf freetype-${freetypeVersion}
		rm -f freetype-${freetypeVersion}.tar.gz
	fi
}
Install_Libiconv()
{
	if [ -d '/usr/local/libiconv' ];then
		return
	fi
	cd ${run_path}
	if [ ! -f "libiconv-1.14.tar.gz" ];then
		wget -O libiconv-1.14.tar.gz ${download_Url}/src/libiconv-1.14.tar.gz -T 5
	fi
	mkdir /patch
	wget -O /patch/libiconv-glibc-2.16.patch ${download_Url}/src/patch/libiconv-glibc-2.16.patch -T 5
	tar zxf libiconv-1.14.tar.gz
	cd libiconv-1.14
    patch -p0 < /patch/libiconv-glibc-2.16.patch
    ./configure --prefix=/usr/local/libiconv --enable-static
    make -j${cpuCore}
    make install
    cd ${run_path}
    rm -rf libiconv-1.14
	rm -f libiconv-1.14.tar.gz
	echo -e "Install_Libiconv" >> /www/server/lib.pl
}
Install_Libmcrypt()
{
	if [ -f '/usr/local/lib/libmcrypt.so' ];then
		return;
	fi
	cd ${run_path}
	if [ ! -f "libmcrypt-2.5.8.tar.gz" ];then
		wget -O libmcrypt-2.5.8.tar.gz ${download_Url}/src/libmcrypt-2.5.8.tar.gz -T 5
	fi
	tar zxf libmcrypt-2.5.8.tar.gz
	cd libmcrypt-2.5.8
	
    ./configure
    make -j${cpuCore}
    make install
    /sbin/ldconfig
    cd libltdl/
    ./configure --enable-ltdl-install
    make && make install
    ln -sf /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
    ln -sf /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
    ln -sf /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
    ln -sf /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
    ldconfig
    cd ${run_path}
    rm -rf libmcrypt-2.5.8
	rm -f libmcrypt-2.5.8.tar.gz
	echo -e "Install_Libmcrypt" >> /www/server/lib.pl
}
Install_Mcrypt()
{
	if [ -f '/usr/bin/mcrypt' ] || [ -f '/usr/local/bin/mcrypt' ];then
		return;
	fi
	cd ${run_path}
	if [ ! -f "mcrypt-2.6.8.tar.gz" ];then
		wget -O mcrypt-2.6.8.tar.gz ${download_Url}/src/mcrypt-2.6.8.tar.gz -T 5
	fi
	tar zxf mcrypt-2.6.8.tar.gz
	cd mcrypt-2.6.8
    ./configure
    make -j${cpuCore}
    make install
    cd ${run_path}
    rm -rf mcrypt-2.6.8
	rm -f mcrypt-2.6.8.tar.gz
	echo -e "Install_Mcrypt" >> /www/server/lib.pl
}
Install_Mhash()
{
	if [ -f '/usr/local/lib/libmhash.so' ];then
		return;
	fi
	cd ${run_path}
	if [ ! -f "mhash-0.9.9.9.tar.gz" ];then
		wget -O mhash-0.9.9.9.tar.gz ${download_Url}/src/mhash-0.9.9.9.tar.gz -T 5
	fi
	tar zxf mhash-0.9.9.9.tar.gz
	cd mhash-0.9.9.9
    ./configure
    make -j${cpuCore}
    make install
    ln -sf /usr/local/lib/libmhash.a /usr/lib/libmhash.a
    ln -sf /usr/local/lib/libmhash.la /usr/lib/libmhash.la
    ln -sf /usr/local/lib/libmhash.so /usr/lib/libmhash.so
    ln -sf /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
    ln -sf /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
    ldconfig
    cd ${run_path}
    rm -rf mhash-0.9.9.9*
	echo -e "Install_Mhash" >> /www/server/lib.pl
}

Install_Yumlib(){
	sed -i "s#SELINUX=enforcing#SELINUX=disabled#" /etc/selinux/config
	rpm -e --nodeps mariadb-libs-*
	mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
	rm -f /var/run/yum.pid
	Packs="make cmake gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel patch wget libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel tar bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel libcurl libcurl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel vim-minimal gettext gettext-devel ncurses-devel gmp-devel pspell-devel libcap diffutils ca-certificates net-tools libc-client-devel psmisc libXpm-devel c-ares-devel libicu-devel libxslt libxslt-devel zip unzip glibc.i686 libstdc++.so.6 cairo-devel bison-devel ncurses-devel libaio-devel perl perl-devel perl-Data-Dumper lsof pcre pcre-devel vixie-cron crontabs expat-devel readline-devel libtermcap-devel cyrus-sasl cyrus-sasl-devel"
	yum install ${Packs} -y
	for yumPack in ${Packs};
	do
		rpmPack=$(rpm -q ${yumPack})
		packCheck=$(echo $rpmPack|grep not)
		if [ "${packCheck}" ]; then
			yum install ${yumPack} -y
		fi
	done
	mv /etc/yum.repos.d/epel.repo.backup /etc/yum.repos.d/epel.repo
	echo "true" > /etc/bt_lib.lock
}
Install_Aptlib(){
	deepinSys=`cat /etc/issue`
	#apt-get autoremove -y
	apt-get -fy install
	export DEBIAN_FRONTEND=noninteractive
	apt-get install -y build-essential gcc g++ make
	if [[ "${deepinSys}" =~ eepin ]]; then
		for aptPack in debian-keyring debian-archive-keyring build-essential gcc g++ make cmake autoconf automake re2c wget cron bzip2 libzip-dev libc6-dev bison file rcconf flex vim bison m4 gawk less cpp binutils diffutils unzip tar bzip2 libbz2-dev libncurses5 libncurses5-dev libtool libevent-dev openssl libssl-dev zlibc libsasl2-dev libltdl3-dev libltdl-dev zlib1g zlib1g-dev libbz2-1.0 libbz2-dev libglib2.0-0 libglib2.0-dev libpng3 libpng12-0 libpng12-dev libkrb5-dev libpq-dev libpq5 gettext libpng12-dev libxml2-dev libcap-dev ca-certificates libc-client2007e-dev psmisc patch git libc-ares-dev libicu-dev e2fsprogs libxslt-dev libc-client-dev xz-utils libgd3 libgd-dev;
		do apt-get -y install $aptPack --force-yes; done
		rm -rf /usr/local/openssl
		Install_OpenSSL
	else
		for aptPack in debian-keyring debian-archive-keyring build-essential gcc g++ make cmake autoconf automake re2c wget cron bzip2 libzip-dev libc6-dev bison file rcconf flex vim bison m4 gawk less cpp binutils diffutils unzip tar bzip2 libbz2-dev libncurses5 libncurses5-dev libtool libevent-dev openssl libssl-dev zlibc libsasl2-dev libltdl3-dev libltdl-dev zlib1g zlib1g-dev libbz2-1.0 libbz2-dev libglib2.0-0 libglib2.0-dev libpng3 libjpeg62 libjpeg62-dev libpng12-0 libpng12-dev libkrb5-dev libpq-dev libpq5 gettext libpng12-dev libxml2-dev libcap-dev ca-certificates libc-client2007e-dev psmisc patch git libc-ares-dev libicu-dev e2fsprogs libxslt-dev libc-client-dev xz-utils libgd3 libgd-dev;
		do apt-get -y install $aptPack --force-yes; done
	fi
	ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so.8 /usr/lib/libjpeg.so
	ln -s /usr/lib/x86_64-linux-gnu/libjpeg.a /usr/lib/libjpeg.a
	ln -s /usr/lib/x86_64-linux-gnu/libpng12.so.0 /usr/lib/libpng.so
	ln -s /usr/lib/x86_64-linux-gnu/libpng.a /usr/lib/libpng.a
	echo "true" > /etc/bt_lib.lock
}

Install_Jemalloc(){
	if [ ! -f '/usr/local/lib/libjemalloc.so' ]; then
		wget -O jemalloc-5.0.1.tar.bz2 ${download_Url}/src/jemalloc-5.0.1.tar.bz2
		tar -xvf jemalloc-5.0.1.tar.bz2
		cd jemalloc-5.0.1
		./configure
		make && make install
		ldconfig
		cd ..
		rm -rf jemalloc*
	fi
}
Install_LuaJIT()
{
	if [ ! -d '/usr/local/include/luajit-2.0' ];then
		wget -c -O LuaJIT-2.0.4.tar.gz ${download_Url}/install/src/LuaJIT-2.0.4.tar.gz -T 10
		tar xvf LuaJIT-2.0.4.tar.gz
		cd LuaJIT-2.0.4
		make linux
		make install
		cd ..
		rm -rf LuaJIT-*
		export LUAJIT_LIB=/usr/local/lib
		export LUAJIT_INC=/usr/local/include/luajit-2.0/
		ln -sf /usr/local/lib/libluajit-5.1.so.2 /usr/local/lib64/libluajit-5.1.so.2
		echo "/usr/local/lib" >> /etc/ld.so.conf
		ldconfig
	fi
}
Install_cjson()
{
	if [ ! -f /usr/local/lib/lua/5.1/cjson.so ];then
		wget -O lua-cjson-2.1.0.tar.gz $download_Url/install/src/lua-cjson-2.1.0.tar.gz -T 20
		tar xvf lua-cjson-2.1.0.tar.gz
		rm -f lua-cjson-2.1.0.tar.gz
		cd lua-cjson-2.1.0
		make
		make install
		cd ..
		rm -rf lua-cjson-2.1.0
	fi
}
My_Cnf(){
	Data_Path="/www/server/data"
	if [ -f "/www/server/mysql/my.cnf.bak" ]; then
		\cp /www/server/mysql/my.cnf.bak /etc/my.cnf
		return
	fi
	if [ "${mysqlV}" == "5.1" ]; then
		defaultEngine="MyISAM"

	else
		defaultEngine="InnoDB"
	fi
	cat > /etc/my.cnf<<EOF
[client]
#password	= your_password
port		= 3306
socket		= /tmp/mysql.sock

[mysqld]
port		= 3306
socket		= /tmp/mysql.sock
datadir = ${Data_Path}
default_storage_engine = ${defaultEngine}
skip-external-locking
key_buffer_size = 8M
max_allowed_packet = 100G
table_open_cache = 32
sort_buffer_size = 256K
net_buffer_length = 4K
read_buffer_size = 128K
read_rnd_buffer_size = 256K
myisam_sort_buffer_size = 4M
thread_cache_size = 4
query_cache_size = 4M
tmp_table_size = 8M
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

#skip-name-resolve
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535

log-bin=mysql-bin
binlog_format=mixed
server-id = 1
slow_query_log=1
slow-query-log-file=${Data_Path}/mysql-slow.log
long_query_time=3
#log_queries_not_using_indexes=on


innodb_data_home_dir = ${Data_Path}
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = ${Data_Path}
innodb_buffer_pool_size = 16M
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50

[mysqldump]
quick
max_allowed_packet = 500M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
EOF

	if [ "${mysqlV}" == "8.0" ]; then
		sed -i '/server-id/a\binlog_expire_logs_seconds = 600000' /etc/my.cnf
		sed -i '/tmp_table_size/a\default_authentication_plugin = mysql_native_password' /etc/my.cnf
		sed -i '/default_authentication_plugin/a\lower_case_table_names = 1' /etc/my.cnf
		sed -i '/query_cache_size/d' /etc/my.cnf
	else
		sed -i '/server-id/a\expire_logs_days = 10' /etc/my.cnf
	fi

	if [ "${mysqlV}" != "5.5" ];then
		if [ "${mysqlV}" != "5.1" ]; then
			sed -i '/skip-external-locking/i\table_definition_cache = 400' /etc/my.cnf
			sed -i '/table_definition_cache/i\performance_schema_max_table_instances = 400' /etc/my.cnf
		fi
	fi

	if [ "${mysqlV}" != "5.1" ]; then
		sed -i '/innodb_lock_wait_timeout/a\innodb_max_dirty_pages_pct = 90' /etc/my.cnf
		sed -i '/innodb_max_dirty_pages_pct/a\innodb_read_io_threads = 4' /etc/my.cnf
		sed -i '/innodb_read_io_threads/a\innodb_write_io_threads = 4' /etc/my.cnf
	fi

	[ "${mysqlV}" == "5.1" ] || [ "${mysqlV}" == "5.5" ] && sed -i '/STRICT_TRANS_TABLES/d' /etc/my.cnf
	[ "${mysqlV}" == "5.7" ] || [ "${mysqlV}" == "8.0" ] && sed -i '/#log_queries_not_using_indexes/a\early-plugin-load = ""' /etc/my.cnf
	[ "${mysqlV}" == "5.6" ] || [ "${mysqlV}" == "5.7" ] || [ "${mysqlV}" == "8.0" ] && sed -i '/#skip-name-resolve/i\explicit_defaults_for_timestamp = true' /etc/my.cnf

	cpuInfo=`cat /proc/cpuinfo |grep "processor"|wc -l`
	sed -i 's/innodb_write_io_threads = 4/innodb_write_io_threads = '${cpuInfo}'/g' /etc/my.cnf
	sed -i 's/innodb_read_io_threads = 4/innodb_read_io_threads = '${cpuInfo}'/g' /etc/my.cnf
	MemTotal=`free -m | grep Mem | awk '{print  $2}'`
	if [[ ${MemTotal} -gt 1024 && ${MemTotal} -lt 2048 ]]; then
		sed -i "s#^key_buffer_size.*#key_buffer_size = 32M#" /etc/my.cnf
		sed -i "s#^table_open_cache.*#table_open_cache = 128#" /etc/my.cnf
		sed -i "s#^sort_buffer_size.*#sort_buffer_size = 768K#" /etc/my.cnf
		sed -i "s#^read_buffer_size.*#read_buffer_size = 768K#" /etc/my.cnf
		sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 8M#" /etc/my.cnf
		sed -i "s#^thread_cache_size.*#thread_cache_size = 16#" /etc/my.cnf
		sed -i "s#^query_cache_size.*#query_cache_size = 16M#" /etc/my.cnf
		sed -i "s#^tmp_table_size.*#tmp_table_size = 32M#" /etc/my.cnf
		sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 128M#" /etc/my.cnf
		sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 64M#" /etc/my.cnf
		sed -i "s#^innodb_log_buffer_size.*#innodb_log_buffer_size = 16M#" /etc/my.cnf
	elif [[ ${MemTotal} -ge 2048 && ${MemTotal} -lt 4096 ]]; then
		sed -i "s#^key_buffer_size.*#key_buffer_size = 64M#" /etc/my.cnf
		sed -i "s#^table_open_cache.*#table_open_cache = 256#" /etc/my.cnf
		sed -i "s#^sort_buffer_size.*#sort_buffer_size = 1M#" /etc/my.cnf
		sed -i "s#^read_buffer_size.*#read_buffer_size = 1M#" /etc/my.cnf
		sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 16M#" /etc/my.cnf
		sed -i "s#^thread_cache_size.*#thread_cache_size = 32#" /etc/my.cnf
		sed -i "s#^query_cache_size.*#query_cache_size = 32M#" /etc/my.cnf
		sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf
		sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 256M#" /etc/my.cnf
		sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 128M#" /etc/my.cnf
		sed -i "s#^innodb_log_buffer_size.*#innodb_log_buffer_size = 32M#" /etc/my.cnf
	elif [[ ${MemTotal} -ge 4096 && ${MemTotal} -lt 8192 ]]; then
		sed -i "s#^key_buffer_size.*#key_buffer_size = 128M#" /etc/my.cnf
		sed -i "s#^table_open_cache.*#table_open_cache = 512#" /etc/my.cnf
		sed -i "s#^sort_buffer_size.*#sort_buffer_size = 2M#" /etc/my.cnf
		sed -i "s#^read_buffer_size.*#read_buffer_size = 2M#" /etc/my.cnf
		sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 32M#" /etc/my.cnf
		sed -i "s#^thread_cache_size.*#thread_cache_size = 64#" /etc/my.cnf
		sed -i "s#^query_cache_size.*#query_cache_size = 64M#" /etc/my.cnf
		sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf
		sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 512M#" /etc/my.cnf
		sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 256M#" /etc/my.cnf
		sed -i "s#^innodb_log_buffer_size.*#innodb_log_buffer_size = 64M#" /etc/my.cnf
	elif [[ ${MemTotal} -ge 8192 && ${MemTotal} -lt 16384 ]]; then
		sed -i "s#^key_buffer_size.*#key_buffer_size = 256M#" /etc/my.cnf
		sed -i "s#^table_open_cache.*#table_open_cache = 1024#" /etc/my.cnf
		sed -i "s#^sort_buffer_size.*#sort_buffer_size = 4M#" /etc/my.cnf
		sed -i "s#^read_buffer_size.*#read_buffer_size = 4M#" /etc/my.cnf
		sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 64M#" /etc/my.cnf
		sed -i "s#^thread_cache_size.*#thread_cache_size = 128#" /etc/my.cnf
		sed -i "s#^query_cache_size.*#query_cache_size = 128M#" /etc/my.cnf
		sed -i "s#^tmp_table_size.*#tmp_table_size = 128M#" /etc/my.cnf
		sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 1024M#" /etc/my.cnf
		sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 512M#" /etc/my.cnf
		sed -i "s#^innodb_log_buffer_size.*#innodb_log_buffer_size = 128M#" /etc/my.cnf
	elif [[ ${MemTotal} -ge 16384 && ${MemTotal} -lt 32768 ]]; then
		sed -i "s#^key_buffer_size.*#key_buffer_size = 512M#" /etc/my.cnf
		sed -i "s#^table_open_cache.*#table_open_cache = 2048#" /etc/my.cnf
		sed -i "s#^sort_buffer_size.*#sort_buffer_size = 8M#" /etc/my.cnf
		sed -i "s#^read_buffer_size.*#read_buffer_size = 8M#" /etc/my.cnf
		sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 128M#" /etc/my.cnf
		sed -i "s#^thread_cache_size.*#thread_cache_size = 256#" /etc/my.cnf
		sed -i "s#^query_cache_size.*#query_cache_size = 256M#" /etc/my.cnf
		sed -i "s#^tmp_table_size.*#tmp_table_size = 256M#" /etc/my.cnf
		sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 2048M#" /etc/my.cnf
		sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 1024M#" /etc/my.cnf
		sed -i "s#^innodb_log_buffer_size.*#innodb_log_buffer_size = 256M#" /etc/my.cnf
	elif [[ ${MemTotal} -ge 32768 ]]; then
		sed -i "s#^key_buffer_size.*#key_buffer_size = 1024M#" /etc/my.cnf
		sed -i "s#^table_open_cache.*#table_open_cache = 4096#" /etc/my.cnf
		sed -i "s#^sort_buffer_size.*#sort_buffer_size = 16M#" /etc/my.cnf
		sed -i "s#^read_buffer_size.*#read_buffer_size = 16M#" /etc/my.cnf
		sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 256M#" /etc/my.cnf
		sed -i "s#^thread_cache_size.*#thread_cache_size = 512#" /etc/my.cnf
		sed -i "s#^query_cache_size.*#query_cache_size = 512M#" /etc/my.cnf
		sed -i "s#^tmp_table_size.*#tmp_table_size = 512M#" /etc/my.cnf
		sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 4096M#" /etc/my.cnf
	if [ "${mysqlV}" == "5.5" ];then
			sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 1024M#" /etc/my.cnf
			sed -i "s#^innodb_log_buffer_size.*#innodb_log_buffer_size = 256M#" /etc/my.cnf
		else
			sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 2048M#" /etc/my.cnf
			sed -i "s#^innodb_log_buffer_size.*#innodb_log_buffer_size = 512M#" /etc/my.cnf
		fi
	fi
}
PPHP_ZEND_Repair(){
	if [ -f "/www/server/php/56/bin/php" ];then
		mkdir -p /usr/local/zend/php56
		if [ "${Is_64bit}" = "64" ] ; then
			wget ${download_Url}/src/zend-loader-php5.6-linux-x86_64.tar.gz -T20
			tar zxf zend-loader-php5.6-linux-x86_64.tar.gz
			\cp zend-loader-php5.6-linux-x86_64/ZendGuardLoader.so /usr/local/zend/php56/
			rm -rf zend-loader-php5.6-linux-x86_64
			rm -f zend-loader-php5.6-linux-x86_64.tar.gz
		else
			wget ${download_Url}/src/zend-loader-php5.6-linux-i386.tar.gz -T20
			tar zxf zend-loader-php5.6-linux-i386.tar.gz
			\cp zend-loader-php5.6-linux-i386/ZendGuardLoader.so /usr/local/zend/php56/
			rm -rf zend-loader-php5.6-linux-i386
			rm -f zend-loader-php5.6-linux-i386.tar.gz
		fi
	fi

	if [ -f "/www/server/php/55/bin/php" ];then
		mkdir -p /usr/local/zend/php55
		if [ "${Is_64bit}" = "64" ] ; then
			wget ${download_Url}/src/zend-loader-php5.5-linux-x86_64.tar.gz -T20
			tar zxf zend-loader-php5.5-linux-x86_64.tar.gz
			mkdir -p /usr/local/zend/
			\cp zend-loader-php5.5-linux-x86_64/ZendGuardLoader.so /usr/local/zend/php55/
			rm -rf zend-loader-php5.5-linux-x86_64
			rm -f zend-loader-php5.5-linux-x86_64.tar.gz
		else
			wget ${download_Url}/src/zend-loader-php5.5-linux-i386.tar.gz
			tar zxf zend-loader-php5.5-linux-i386.tar.gz
			mkdir -p /usr/local/zend/
			\cp zend-loader-php5.5-linux-i386/ZendGuardLoader.so /usr/local/zend/php55/
			rm -rf zend-loader-php5.5-linux-i386
			rm -f zend-loader-php5.5-linux-i386.tar.gz
		fi
	fi

	if [ -f "/www/server/php/54/bin/php" ];then
		mkdir -p /usr/local/zend/php54
		if [ "${Is_64bit}" = "64" ] ; then
			wget ${download_Url}/src/ZendGuardLoader-70429-PHP-5.4-linux-glibc23-x86_64.tar.gz -T20
			tar zxf ZendGuardLoader-70429-PHP-5.4-linux-glibc23-x86_64.tar.gz
			\cp ZendGuardLoader-70429-PHP-5.4-linux-glibc23-x86_64/php-5.4.x/ZendGuardLoader.so /usr/local/zend/php54/
			rm -rf ZendGuardLoader-70429-PHP-5.4-linux-glibc23-x86_64
			rm -f ZendGuardLoader-70429-PHP-5.4-linux-glibc23-x86_64.tar.gz
		else
			wget ${download_Url}/src/ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386.tar.gz -T20
			tar zxf ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386.tar.gz
			\cp ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386/php-5.4.x/ZendGuardLoader.so /usr/local/zend/php54/
			rm -rf ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386
			rm -f ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386.tar.gz
		fi
	fi

	if [ -f "/www/server/php/53/bin/php" ];then
		mkdir -p /usr/local/zend/php53
		if [ "${Is_64bit}" = "64" ] ; then
			wget ${download_Url}/src/ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz
			tar zxf ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz
			\cp ZendGuardLoader-php-5.3-linux-glibc23-x86_64/php-5.3.x/ZendGuardLoader.so /usr/local/zend/php53/
			rm -f ${run_path}/ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz
			rm -rf ${run_path}/ZendGuardLoader-php-5.3-linux-glibc23-x86_64
		else
			wget ${download_Url}/src/ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz
			tar zxf ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz
			\cp ZendGuardLoader-php-5.3-linux-glibc23-i386/php-5.3.x/ZendGuardLoader.so /usr/local/zend/php53/
			rm -rf ZendGuardLoader-php-5.3-linux-glibc23-i386
			rm -f ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz
		fi
	fi

	if [ -f "/www/server/php/52/bin/php" ];then
		mkdir -p /usr/local/zend/php52
		wget -O /usr/local/zend/php52/ZendOptimizer.so ${download_Url}/src/ZendOptimizer-${Is_64bit}.so -T 20
	fi
}
Service_Repair(){
	for phpV in 52 53 54 55 56 70 71 72 73 74
	do
		if [ -f "/www/server/php/${phpV}/bin/php" ];then
			ln -sf /www/server/php/${phpV}/bin/php /usr/bin/php
			wget -O /etc/init.d/php-fpm-${phpV} ${download_Url}/init/php/php-fpm-${phpV}
			chmod +x /etc/init.d/php-fpm-${phpV}
			/etc/init.d/php-fpm-${phpV} start
		fi
	done

	if [ -f "/www/server/nginx/sbin/nginx" ];then
		ln -sf /www/server/nginx/sbin/nginx /usr/bin/nginx
		Repair_Init "nginx"
	fi

	if [ -f "/www/server/apache/bin/httpd" ];then
		if [ -z "${Centos8Check}" ];then
			Install_Openssl111
			Install_Nghttp2
			if [ "${PM}" == "yum" ] || [ "${PM}" == "dnf" ] ; then
				Pack="lua lua-devel"
				${PM} install ${Pack} -y
			elif [ "${PM}" == "apt-get" ]; then
				Pack="lua5.1 lua5.1-dev lua-cjson lua-socket"
				${PM} install ${Pack} -y
			fi
		fi
		ln -sf /www/server/apache/bin/httpd /usr/bin/httpd
		Repair_Init "httpd"
	fi

	if [ "/www/server/pure-ftpd/bin/pure-pw" ];then
		mkdir -p /etc/ssl/private
		openssl req -x509 -nodes -days 3560 -newkey rsa:1024 -keyout /etc/ssl/private/pure-ftpd.pem -out /etc/ssl/private/pure-ftpd.pem<<EOF
Repair_Init "pure-ftpd"
CN
Guangdong
Dongguan
BT-PANEL
BT
$address
admin@bt.cn
EOF
		Repair_Init "pure-ftpd"
	fi

	if [ -f "/www/server/redis/src/redis-server" ];then
		groupadd redis
		useradd -g redis -s /sbin/nologin redis
		chown -R redis.redis /www/server/redis
		ln -sf /www/server/redis/src/redis-cli /usr/bin/redis-cli
		Repair_Init "redis"
	fi

	if [ -f "/usr/local/memcached/bin/memcached" ];then
		groupadd memcached
		useradd -s /sbin/nologin -g memcached memcached
		ln -sf /usr/local/memcached/bin/memcached /usr/bin/memcached
		Repair_Init "memcached"
	fi

	if [ -f "/www/server/mongodb/bin/mongo" ];then
		groupadd mongo
		useradd -s /sbin/nologin -M -g mongo mongo
		chown -R mongo:mongo /www/server/mongodb
		ln -sf /www/server/mongodb/bin/mongo /usr/mongo
		Repair_Init "mongodb"
	fi

	if [ -f "/www/server/mysql/version.pl" ];then
		groupadd mysql
		useradd -s /sbin/nologin -M -g mysql mysql
		chown -R mysql:mysql /www/server/mysql
		chown -R mysql:mysql /www/server/data
		chgrp -R mysql /www/server/mysql/.
		echo "/www/server/mysql/lib" > /etc/ld.so.conf.d/mysql.conf
		ldconfig
		ln -sf /www/server/mysql/lib/mysql /usr/lib/mysql
		ln -sf /www/server/mysql/include/mysql /usr/include/mysql
		ln -sf /www/server/mysql/bin/mysql /usr/bin/mysql
		ln -sf /www/server/mysql/bin/mysqldump /usr/bin/mysqldump
		ln -sf /www/server/mysql/bin/myisamchk /usr/bin/myisamchk
		ln -sf /www/server/mysql/bin/mysqld_safe /usr/bin/mysqld_safe
		ln -sf /www/server/mysql/bin/mysqlcheck /usr/bin/mysqlcheck
		ln -sf /www/server/mysql/bin/mysql_config /usr/bin/mysql_config
		local mysqlVersion=$(cat /www/server/mysql/version.pl)
		local mariadbCheck=$(cat /www/server/mysql/version.pl|grep mariadb)
		local alisqlCheck=$(cat /www/server/mysql/version.pl|grep AliSQL)
		if [ "${mariadbCheck}" ];then
			mariadbV=${mariadbCheck:8:4}
			wget -O /etc/init.d/mysqld ${download_Url}/init/mysql/init/mariadb${mariadbV}
		elif [ "${alisqlCheck}" ]; then
			wget -O /etc/init.d/mysqld ${download_Url}/init/mysql/init/alisql
		else
			mysqlV=${mysqlVersion:0:3}
			wget -O /etc/init.d/mysqld ${download_Url}/init/mysql/init/mysql${mysqlV}
		fi
		chmod +x /etc/init.d/mysqld
		My_Cnf
		/etc/init.d/mysqld start
	fi
}

main(){
	if [ "${PM}" = "yum" ]; then
		Install_Yumlib
	elif [ "${PM}" = "apt-get" ]; then
		Install_Aptlib
	fi
	Install_Sendmail
	Install_Openssl
	Install_Pcre
	Install_Curl
	Install_Mhash
	Install_Libmcrypt
	Install_Mcrypt	
	Install_Libiconv
	Install_Freetype

	Install_Jemalloc
	Install_LuaJIT
	Install_cjson

	PPHP_ZEND_Repair

	Service_Repair
	echo "已修复完成"
}
main

