#!/bin/bash
panel_path=/www/server/panel
log_file=$panel_path/logs/error.log
pidfile=$panel_path/logs/panel.pid
bt_files="/etc/init.d/bt"

if [ $(whoami) != "root" ];then
    echo -e "Non-root install, please try the following solutions: \n   1.Please switch to root user \n   2.Try executing the following commands: \n     sudo bash $0 $@"
    exit 1
fi

if [[ ! -s "$bt_files" ]];then
	rm -f $bt_files
	wget --no-check-certificate -O $bt_files https://node.aapanel.com/install/src/bt6_en.init -T 20
	chmod +x $bt_files
	if [[ ! -s "$bt_files" ]];then
		echo -e "\033[31mDownload failed, please check your network.\033[0m"
		echo -e "\033[31m$bt_files\033[0m file content is 0kb "
	fi
fi

Install_telnet(){
if ! command -v telnet &> /dev/null; then

	if [ -f "/usr/bin/yum" ] && [ -d "/etc/yum.repos.d" ]; then
                PM="yum"
        elif [ -f "/usr/bin/apt-get" ] && [ -f "/usr/bin/dpkg" ]; then
                PM="apt-get"
	fi

	if [ "${PM}" = "yum" ];then
		Pack="telnet"
	elif [ "${PM}" = "apt-get" ];then
		Pack="telnet"
	fi

	${PM} install ${Pack} -y
fi
}

check_bt_status(){
while true
do
	pids=$(ps -ef | grep -E 'BT-Panel|BT-Task' | grep -v grep | awk '{print $2}')
	if [ "$pids" == "" ];then
		sleep 1;
		echo -e "Starting Bt-Panel..."
		echo -e "------------------------------------------------------------------"
		bt restart;
		break;
	else
		bt status;
		echo -e "------------------------------------------------------------------"
		break;
	fi
done
}

check_ssl() {
ssl_file=/www/server/panel/data/ssl.pl
if [ -f "$ssl_file" ];then
	echo -e "SSL is enabled, please use \033[31m HTTPS access \033[0m"
	vive_access=$(bt 14 |grep "https://")
	echo "$vive_access"
	echo -e "\033[31m When sharing, please be careful to hide your IP address\033[0m"
	echo -e "------------------------------------------------------------------"
else
	echo -e "SSL is not enabled, please use \033[31m HTTP access\033[0m"
	vive_access=$(bt 14 |grep "http://")
	echo "$vive_access"
	echo -e "\033[31m When sharing, please be careful to hide your IP address\033[0m"
	echo -e "------------------------------------------------------------------"
fi
}

check_port(){ 
port=/www/server/panel/data/port.pl
for line in `cat $port`
do
	echo -e "\033[31m aaPanel port:$line\033[0m"
done
}

check_url(){
port=$(cat $panel_path/data/port.pl)
	pool=http
	if [ -f $ssl_file ];then
		pool=https
	fi

	url=$(curl --insecure "$pool://127.0.0.1:$port/login" 2>&1 |grep -i html)
	if [ $? -eq 0 ];then
		echo -e "\033[32m Server internal 127.0.0.1 test access is normal \033[0m"
	else
		echo -e "\033[31m Server internal 127.0.0.1 test access failed \033[0m"
		echo -e "------------------------------------------------------------------"
	fi
}

check_port_status(){
	red=$(cat /www/server/panel/data/port.pl)
	pid=$(lsof -i:${red} | sed -n '2p')
	if [ "$pid" != "" ];then
		echo -e "$pid"
		echo -e "------------------------------------------------------------------"
	fi
	echo "Getting server ip, Please wait..."
	ip=$(curl -sSk --connect-timeout 10 -m 60 https://www.aapanel.com/api/common/getClientIP)
	port_status=$(echo "" | telnet $ip $red 2> /dev/null)
	if [[ $port_status =~ "Escape" ]];then
		echo -e "\033[32m aaPanel port external network "$ip" "$red" test access is normal \033[0m"
		#echo -e "------------------------------------------------------------------"
		echo ""
	else
		echo -e "\033[31m aaPanel port external network test access failed \033[0m"
		echo -e "\033[31m For reference only, please test manually! \033[0m"
		#echo -e "------------------------------------------------------------------"
		echo ""
	fi
		
}

check_disk(){
	disk=$(df -H)
	inode=$(df -i)
	if [ "$disk" ];then
		echo "-------------------------Disk space usage--------------------------"
		echo "$disk"
	fi
	if [ "$inode" ];then
		echo "--------------------------Inode usage------------------------------"
		echo "$inode"
		echo -e "------------------------------------------------------------------"
	fi
}

error_logs(){
	red=$(cat /www/server/panel/data/port.pl)
    pid=$(lsof -i:$red)
	if [ $? -eq 0 ];then
		echo $? &>/dev/null
	else
		echo -e "------------------------------------------------------------------"
	 	tail -n 20 $log_file
	fi
}

check_dir(){
	pyenv="/www/server/panel/pyenv"
	pyenv_files="/www/server/panel/pyenv/bin/python"
	version_file="/www/server/panel/class/common.py"
	version_file_v2="/www/server/panel/class_v2/common_v2.py"
	if [[ ! -d "$pyenv" ]];then
		echo -e "\033[31m$pyenv\033[0m directory does not exist "
		ls /www/server/panel/
	# else
	# 	echo -e "\033[32m$pyenv\033[0m directory exists "
	fi	
	if [[ ! -x "$pyenv_files" ]];then
		chmod -R 700 $pyenv/bin
		if [[ ! -x "$pyenv_files" ]];then
			echo -e "\033[31m$pyenv_files\033[0m no execute permission"
		fi
	fi
	if [[ ! -f "$pyenv_files" ]];then
		echo -e "\033[31m$pyenv_files\033[0m file does not exist "
	else
		py_version=$($pyenv_files -V)
		# echo -e "\033[32m$pyenv_files\033[0m file exists. Version: $py_version"
		echo -e "Python version: $py_version"
	fi

	# if [[ -f "$version_file_v2" ]];then
	# 	if [[ -f "$version_file_v2" ]];then
	# 		get_panel_version=$(awk -F"[=']+" '/g.version =/{print $3}' $version_file_v2)
	# 		echo "aaPanal version: $get_panel_version"
	# 	fi
	# else
		if [[ -f "$version_file" ]];then
			get_panel_version=$(awk -F"[=']+" '/g.version =/{print $3}' $version_file)
			echo "aaPanal version_o: $get_panel_version"
		fi
	# fi

	if [[ ! -s "$bt_files" ]];then
		rm -f $bt_files
		wget --no-check-certificate -O $bt_files https://node.aapanel.com/install/src/bt6_en.init -T 20
        chmod +x $bt_files
		if [[ ! -s "$bt_files" ]];then
			echo -e "\033[31m$bt_files\033[0m file content is 0kb "
		fi
	fi
}


Install_telnet
check_bt_status

check_port_status

check_ssl

check_url

check_disk

check_dir

error_logs
