#!/bin/sh
public_file=/www/server/panel/install/public.sh
if [ ! -f $public_file ];then
	wget -O $public_file https://download.bt.cn/install/public.sh -T 5;
fi
. $public_file
download_Url=$NODE_URL
action=$1
v=$2
centos(){
	JSON=
	if [ "$v" = "70" ] || [ "$v" = "71" ] || [ "$v" = "72" ] || [ "$v" = "73" ] || [ "$v" = "74" ] ; then
		JSON=lsphp$v-json
	fi
	for i in lsphp$v lsphp$v-common lsphp$v-gd lsphp$v-process lsphp$v-mbstring lsphp$v-xml lsphp$v-mcrypt lsphp$v-pdo lsphp$v-imap lsphp$v-mysqlnd $JSON lsphp$v-sqlite3 lsphp$v-zip lsphp$v-curl
	do
	echo "begin to $action $i"
	yum -y $action $i
	done
}


ubuntu(){
	for i in lsphp$v lsphp$v-mysql lsphp$v-imap lsphp$v-curl lsphp$v-sqlite3 lsphp$v-zip
	do
	echo "begin to $action $i"
	apt-get $action $i -y
	done
	if [ "$v" != "70" ] && [ "$v" != "71" ] && [ "$v" != "72" ]  && [ "$v" != "73" ] || [ "$v" = "74" ] ; then
		apt-get -y $action lsphp$v-gd lsphp$v-mcrypt
	else
	   apt-get -y $action lsphp$v-common lsphp$v-json
	fi
}

#centos安装
if [ -f /etc/redhat-release ];then
	centos
	if [ ! -f /usr/local/lsws/lsphp${v}/etc/php.ini ];then
		centos
	fi
	if [ ! -f /usr/local/lsws/lsphp${v}/bin/php ];then
		centos_install_ols
	fi
#debian安装
else
	ubuntu
	if [ ! -f /usr/local/lsws/lsphp$v/etc/php/${v:0:1}.${v:1:2}/litespeed/php.ini ];then
		ubuntu
	fi
	if [ ! -f /usr/local/lsws/lsphp${v}/bin/php ];then
		ubuntu
	fi
fi
if [ -f "/etc/redhat-release" ];then
#	rm -f /usr/local/lsws/lsphp$v/etc/php.ini_old
#	mv /usr/local/lsws/lsphp$v/etc/php.ini /usr/local/lsws/lsphp$v/etc/php.ini_old
#	wget -O /usr/local/lsws/lsphp$v/etc/php.ini $download_Url/install/ols/php/centos/php$v.ini
	if [ ! -f /usr/local/lsws/lsphp$v/etc/php.ini ];then
	  wget -O /usr/local/lsws/lsphp$v/etc/php.ini $download_Url/install/ols/php/centos/php$v.ini
#		mv /usr/local/lsws/lsphp$v/etc/php.ini_old /usr/local/lsws/lsphp$v/etc/php.ini
#		sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv/g' /usr/local/lsws/lsphp$v/etc/php.ini
#		sed -i 's/mysqli.default_socket.*/mysqli.default_socket = \/tmp\/mysql.sock/g' /usr/local/lsws/lsphp$v/etc/php.ini
#		sed -i 's/mysql.default_socket.*/mysql.default_socket = \/tmp\/mysql.sock/g' /usr/local/lsws/lsphp$v/etc/php.ini
#		sed -i 's/session.save_path.*/session.save_path = "\/tmp"/g' /usr/local/lsws/lsphp$v/etc/php.ini
	fi
	grep "/tmp/mysql.sock" /usr/local/lsws/lsphp$v/etc/php.ini > /dev/null
	if [ "$?" -ne 0 ];then
	  wget -O /usr/local/lsws/lsphp$v/etc/php.ini $download_Url/install/ols/php/centos/php$v.ini
	fi
else
#	rm -f /usr/local/lsws/lsphp$v/etc/php/${v:0:1}.${v:1:2}/litespeed/php.ini_old
#	mv /usr/local/lsws/lsphp$v/etc/php/${v:0:1}.${v:1:2}/litespeed/php.ini /usr/local/lsws/lsphp$v/etc/php/${v:0:1}.${v:1:2}/litespeed/php.ini_old
#	wget -O /usr/local/lsws/lsphp$v/etc/php/${v:0:1}.${v:1:2}/litespeed/php.ini $download_Url/install/ols/php/ubuntu/php$v.ini
	if [ ! -f "/usr/local/lsws/lsphp$v/etc/php/${v:0:1}.${v:1:2}/litespeed/php.ini" ];then
	  wget -O /usr/local/lsws/lsphp$v/etc/php/${v:0:1}.${v:1:2}/litespeed/php.ini $download_Url/install/ols/php/ubuntu/php$v.ini
#		mv /usr/local/lsws/lsphp$v/etc/php/${v:0:1}.${v:1:2}/litespeed/php.ini_old /usr/local/lsws/lsphp$v/etc/php/${v:0:1}.${v:1:2}/litespeed/php.ini
#		sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv/g' /usr/local/lsws/lsphp$v/etc/php/${v:0:1}.${v:1:2}/litespeed/php.ini
#		sed -i 's/mysqli.default_socket.*/mysqli.default_socket = \/tmp\/mysql.sock/g' /usr/local/lsws/lsphp$v/etc/php/${v:0:1}.${v:1:2}/litespeed/php.ini
#		sed -i 's/mysql.default_socket.*/mysql.default_socket = \/tmp\/mysql.sock/g' /usr/local/lsws/lsphp$v/etc/php/${v:0:1}.${v:1:2}/litespeed/php.ini
#		sed -i 's/session.save_path.*/session.save_path = "\/tmp"/g' /usr/local/lsws/lsphp$v/etc/php/${v:0:1}.${v:1:2}/litespeed/php.ini
	fi
	grep "/tmp/mysql.sock" /usr/local/lsws/lsphp$v/etc/php/${v:0:1}.${v:1:2}/litespeed/php.ini > /dev/null
    if [ "$?" -ne 0 ];then
      wget -O /usr/local/lsws/lsphp$v/etc/php/${v:0:1}.${v:1:2}/litespeed/php.ini $download_Url/install/ols/php/ubuntu/php$v.ini
    fi
fi
/usr/local/lsws/bin/lswsctrl restart