#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
LANG=en_US.UTF-8

install_pack(){
	#安装依赖
	if [ -f /usr/bin/yum ];then
		yum install gcc lsof unzip libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel wget git -y
	else
		apt install gcc lsof net-tools swig build-essential unzip libffi-dev zlib1g.dev libbz2-dev libssl-dev libncurses-dev libsqlite3-dev libreadline-dev tk-dev libgdbm-dev libdb-dev libdb++-dev libpcap-dev xz-utils wget git -y
	fi
}

env_path=/www/server/coll/pyenv

install_python(){
	#安装指定Py版本
	if [ ! -f $env_path/bin/python ];then
		python_version=3.7.4

		if [ ! -d $env_path ]; then
			mkdir -p $env_path
		fi
		wget -O /tmp/Python-$python_version.tar.xz http://download.bt.cn/src/Python-$python_version.tar.xz -T 5
		if [ ! -f /tmp/Python-$python_version.tar.xz ];then
			echo "Download python tar file failed."
			return
		fi
		tar xvf /tmp/Python-$python_version.tar.xz &> /dev/null
		cd /tmp/Python-$python_version
		./configure --prefix=$env_path &> /dev/null
		cpu_cpunt=$(cat /proc/cpuinfo|grep processor|wc -l)
		make -j$cpu_cpunt &> /dev/null
		make install &> /dev/null

		py_path=$env_path/bin/python3.7
		if [ ! -f $py_path ];then
				echo "==========================================================="
				echo "无法安装Python虚拟环境。"
				echo "==========================================================="
				exit 0
		fi
		\cp -arf $env_path/bin/pip3.7 $env_path/bin/pip
		chmod +x $env_path/bin/pip
		rm -rf /tmp/Python-$python_version /tmp/Python-$python_version.tar.xz
		echo "The python environment installation is complete."
		cd /tmp
	fi
}

install_pypack(){
	#安装PYthon依赖包
	cd /tmp
	curl -Ss --connect-timeout 3 -m 60 http://download.bt.cn/install/pip_select.sh|bash
	wget -O /www/server/coll/requirement.txt http://download.bt.cn/install/coll/requirement.txt
	$env_path/bin/pip install -r /www/server/coll/requirement.txt
	is_install=$($env_path/bin/python -m M2Crypto 2>&1|grep 'package and cannot be directly executed')
	if [ "$is_install" = "" ];then
		echo "==========================================================="
		echo "安装Python依赖包失败!"
		echo "==========================================================="
		exit 0 
	fi
}

install_side(){
	#安装被控端
	side_path=/www/server/coll/client-side
	mkdir -p $side_path
	wget -O /tmp/client-side.zip http://download.bt.cn/install/coll/client-side.zip -T 5
	unzip -o /tmp/client-side.zip -d /www/server/coll/client-side/
	
	if [ ! -f $side_path/init.sh ];then
		echo "==========================================================="
		echo "文件下载失败!"
		echo "==========================================================="
		exit 0 
	fi

	chmod +x $side_path/baota_client
	\cp -arf $side_path/init.sh /etc/init.d/baota_side
	chmod +x /etc/init.d/baota_side
	ln -sf /etc/init.d/baota_side /usr/bin/baota_side
	/etc/init.d/baota_side start
	/etc/init.d/baota_side reload
	sleep 1
}

set_firewall_or_services(){
	#设置服务和防火墙
	mkdir -p $side_path/data
	port=$(cat $side_path/data/port.pl)
	if [ -f /usr/bin/yum ];then
		chkconfig --add baota_side
		chkconfig --level 2345 baota_side on
		if [ -f /etc/init.d/iptables ];then
			iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport $port -j ACCEPT
			service iptables save
		fi

		if [ -f /usr/bin/firewall-cmd ];then
			firewall-cmd --permanent --zone=public --add-port=$port/tcp
			firewall-cmd --reload
		fi
	else
		update-rc.d baota_side defaults
		if [ -f "/usr/sbin/ufw" ];then
			ufw allow $port/tcp
			ufw reload
		fi
	fi
}

install_pack
install_python
install_pypack
install_side
set_firewall_or_services
curl -sS --connect-timeout 10 -m 60 https://www.bt.cn/Api/SetupCount?type=coll_shide > /dev/null 2>&1

echo "==========================================================="
echo "安装成功，请复制以下RSA公钥在主控端添加服务器!"
echo "==========================================================="
echo "端口: $port"
echo "RSA公钥:"
echo "==========================================================="
cat $side_path/data/id_rsa.pub
echo "==========================================================="