#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
Install_xdebug()
{
	if [ ! -f "/www/server/php/$version/bin/php-config" ];then
		echo "php-$vphp not install, Plese select other version!"
		exit 0
	fi
	
	isInstall=`cat /www/server/php/$version/etc/php.ini|grep 'xdebug.so'`
	if [ "${isInstall}" != "" ];then
		echo "php-$vphp not install, Plese select other version!"
		exit 0
	fi
	
	case "${version}" in 
		'52')
		extFile='/www/server/php/52/lib/php/extensions/no-debug-non-zts-20060613/xdebug.so'
		ols_ext_file='/usr/local/lsws/lsphp52/lib/php/20060613/xdebug.so'
		;;
		'53')
		extFile='/www/server/php/53/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so'
		ols_ext_file='/usr/local/lsws/lsphp53/lib/php/20090626/xdebug.so'
		;;
		'54')
		extFile='/www/server/php/54/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so'
		ols_ext_file='/usr/local/lsws/lsphp54/lib/php/20100525/xdebug.so'
		;;
		'55')
		extFile='/www/server/php/55/lib/php/extensions/no-debug-non-zts-20121212/xdebug.so'
		ols_ext_file='/usr/local/lsws/lsphp55/lib/php/20121212/xdebug.so'
		;;
		'56')
		extFile='/www/server/php/56/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so'
		ols_ext_file='/usr/local/lsws/lsphp56/lib/php/20131226/xdebug.so'
		;;
		'70')
		extFile='/www/server/php/70/lib/php/extensions/no-debug-non-zts-20151012/xdebug.so'
		ols_ext_file='/usr/local/lsws/lsphp70/lib/php/20151012/xdebug.so'
		;;
		'71')
		extFile='/www/server/php/71/lib/php/extensions/no-debug-non-zts-20160303/xdebug.so'
		ols_ext_file='/usr/local/lsws/lsphp71/lib/php/20160303/xdebug.so'
		;;
		'72')
		extFile='/www/server/php/72/lib/php/extensions/no-debug-non-zts-20170718/xdebug.so'
		ols_ext_file='/usr/local/lsws/lsphp72/lib/php/20170718/xdebug.so'
		;;
		'73')
		extFile='/www/server/php/73/lib/php/extensions/no-debug-non-zts-20180731/xdebug.so'
		ols_ext_file='/usr/local/lsws/lsphp73/lib/php/20180731/xdebug.so'
		;;
		'74')
		extFile='/www/server/php/74/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so'
		ols_ext_file='/usr/local/lsws/lsphp74/lib/php/20190902/xdebug.so'
		;;
		'80')
		extFile='/www/server/php/80/lib/php/extensions/no-debug-non-zts-20200930/xdebug.so'
		ols_ext_file='/usr/local/lsws/lsphp80/lib/php/20200930/xdebug.so'
		;;
	esac
	if [ ! -f "$extFile" ];then
		
		public_file=/www/server/panel/install/public.sh
		if [ ! -f $public_file ];then
			wget -O $public_file http://node.aapanel.com/install/public.sh -T 5;
		fi
		#. $public_file

		download_Url='http://node.aapanel.com'
		
		if [[ "$version" -ge '70' ]] && [[ "$version" -le '72' ]];then
			wget -c -O xdebug-2.8.0.tgz $download_Url/src/xdebug-2.8.0.tgz -T 5
			tar zxvf xdebug-2.8.0.tgz
			rm -f xdebug-2.8.0.tgz
			cd xdebug-2.8.0
		elif [[ "$version" -ge '72' ]];then
			wget -c -O xdebug-3.0.0.tgz $download_Url/src/xdebug-3.0.0.tgz -T 5
			tar zxvf xdebug-3.0.0.tgz
			rm -f xdebug-3.0.0.tgz
			cd xdebug-3.0.0
		else
			wget -c -O xdebug-2.2.7.tgz $download_Url/install/src/xdebug-2.2.7.tgz -T 5
			tar zxvf xdebug-2.2.7.tgz
			rm -f xdebug-2.2.7.tgz
			cd xdebug-2.2.7
		fi
	fi

		/www/server/php/$version/bin/phpize
		./configure  --with-php-config=/www/server/php/$version/bin/php-config
		make && make install
		#ols
		if [ ! -f "/etc/redhat-release" ];then
		if [[ ! -f "${ols_ext_file}" ]] && [[ -f "/usr/local/lsws/lsphp$version/bin/php" ]];then
			apt install -y lsphp$version-dev
			/usr/local/lsws/lsphp$version/bin/phpize
			./configure --with-php-config=/usr/local/lsws/lsphp$version/bin/php-config
			make && make install
			ini_path="/usr/local/lsws/lsphp$version/etc/php/${version:0:1}.${version:1:2}/mods-available/xdebug.ini"
			if [ ! -f "$ini_path" ];then
				echo "extension=xdebug.so" >> $ini_path
			fi
		fi
	else
		yum install -y lsphp$version-pecl-xdebug
		if [[ ! -f "${ols_ext_file}" ]] && [[ -f "/usr/local/lsws/lsphp$version/bin/php" ]];then
			yum install -y lsphp$version-devel
			/usr/local/lsws/lsphp$version/bin/phpize
			./configure --with-php-config=/usr/local/lsws/lsphp$version/bin/php-config
			make && make install
			ini_path="/usr/local/lsws/lsphp$version/etc/php.d/xdebug.ini"
			if [ ! -f "$ini_path" ];then
				echo "extension=xdebug.so" >> $ini_path
			fi
		fi
	fi

	cd ..
	rm -rf xdebug-*
	if [ ! -f "$extFile" ];then
		echo '安装失败!';
		exit 0
	fi
	
	
	echo "zend_extension=$extFile" >> /www/server/php/$version/etc/php.ini

	service php-fpm-$version reload
	/usr/local/lsws/bin/lswsctrl restart
	echo '==========================================================='
	echo 'successful!'
	
	rm -f *.zip
	rm -f *.tar.gz
	rm -f *.tgz
	
}

Uninstall_xdebug()
{
	if [ -f "/usr/local/lsws/lsphp$version/bin/php" ];then
		if [ -f "/etc/redhat-release" ];then
			yum remove -y lsphp$version-pecl-xdebug
			ini_path="/usr/local/lsws/lsphp$version/etc/php.d/xdebug.ini"
			rm -f $ini_path
			rm -f /usr/local/lsws/lsphp$version/lib64/php/modules/xdebug.so
		else
			ini_path="/usr/local/lsws/lsphp$version/etc/php/${version:0:1}.${version:1:2}/mods-available/xdebug.ini"
			rm -f $ini_path
			rm -f $ols_ext_file
		fi
		/usr/local/lsws/bin/lswsctrl restart
	fi
	if [ ! -f "/www/server/php/$version/bin/php-config" ];then
		return
	fi
	isInstall=`cat /www/server/php/$version/etc/php.ini|grep 'xdebug.so'`
	if [ "${isInstall}" == "" ];then
		return
	fi
	
	sed -i '/xdebug/d'  /www/server/php/$version/etc/php.ini
	service php-fpm-$version reload
	echo '==============================================='
	echo 'successful!'
}

actionType=$1
version=$2
vphp=${version:0:1}.${version:1:1}

if [ "$actionType" == 'install' ];then
	Install_xdebug
elif [ "$actionType" == 'uninstall' ];then
	Uninstall_xdebug
fi
