#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

CN='125.88.182.172'
HK='103.224.251.79'
HK2='103.224.251.67'
US='128.1.164.196'
sleep 0.5;
CN_PING=`ping -c 1 -w 1 $CN|grep time=|awk '{print $7}'|sed "s/time=//"`
HK_PING=`ping -c 1 -w 1 $HK|grep time=|awk '{print $7}'|sed "s/time=//"`
HK2_PING=`ping -c 1 -w 1 $HK2|grep time=|awk '{print $7}'|sed "s/time=//"`
US_PING=`ping -c 1 -w 1 $US|grep time=|awk '{print $7}'|sed "s/time=//"`

echo "$HK_PING $HK" > ping.pl
echo "$HK2_PING $HK2" >> ping.pl
echo "$US_PING $US" >> ping.pl
echo "$CN_PING $CN" >> ping.pl
nodeAddr=`sort -n -b ping.pl|sed -n '1p'|awk '{print $2}'`
if [ "$nodeAddr" == "" ];then
	nodeAddr=$HK
fi

Download_Url=http://$nodeAddr:5880
mkdir -p /www/server
run_path="/root"
Is_64bit=`getconf LONG_BIT`

centos_version=`cat /etc/redhat-release | grep ' 7.' | grep -i centos`
if [ "${centos_version}" != '' ]; then
	rpm_path="centos7"
else
	rpm_path="centos6"
fi

rm -f /www/server/rpm.pl

Install_Curl()
{	
	curl_status=`cat /www/server/rpm.pl | grep curl`
	if [ "${curl_status}" != 'curl_installed' ]; then
		cd ${run_path}
		if [ ! -f "curl-7.50.3.rpm" ];then
			wget ${Download_Url}/rpm/${rpm_path}/${Is_64bit}/curl-7.50.3.rpm
		fi
		rpm -ivh curl-7.50.3.rpm --force --nodeps
		rm -f curl-7.50.3.rpm
		echo -e "curl_installed" >> /www/server/rpm.pl
	fi
}
Install_Libiconv()
{
	libiconv_status=`cat /www/server/rpm.pl | grep libiconv`
	if [ "${libiconv_status}" != 'libiconv_installed' ]; then
		cd ${run_path}
		if [ ! -f "libiconv-1.14.rpm" ];then
			wget ${Download_Url}/rpm/${rpm_path}/${Is_64bit}/libiconv-1.14.rpm
		fi
		rpm -ivh libiconv-1.14.rpm --force --nodeps
		rm -f libiconv-1.14.rpm
		echo -e "libiconv_installed" >> /www/server/rpm.pl
	fi
}

Install_Libmcrypt()
{	
	libmcrypt_status=`cat /www/server/rpm.pl | grep libmcrypt`
	if [ "${libmcrypt_status}" != 'libmcrypt_installed' ]; then
		cd ${run_path}
		if [ ! -f "libmcrypt-2.5.8.rpm" ];then
			wget ${Download_Url}/rpm/${rpm_path}/${Is_64bit}/libmcrypt-2.5.8.rpm
		fi
		rpm -ivh libmcrypt-2.5.8.rpm --force --nodeps
		rm -f libmcrypt-2.5.8.rpm
		ln -s /usr/local/lib/libltdl.so.3 /usr/lib/libltdl.so.3
		echo -e "libmcrypt_installed" >> /www/server/rpm.pl
	fi
}

Install_Mcrypt()
{   
	mcrypt_status=`cat /www/server/rpm.pl | grep mcrypty`
	if [ "${mcrypt_status}" != 'mcrypty_installed' ]; then
		cd ${run_path}
		if [ ! -f "mcrypt-2.6.8.rpm" ];then
			wget ${Download_Url}/rpm/${rpm_path}/${Is_64bit}/mcrypt-2.6.8.rpm 
		fi
		
		rpm -ivh mcrypt-2.6.8.rpm --force --nodeps 
		rm -f mcrypt-2.6.8.rpm
		echo -e "mcrypty_installed" >> /www/server/rpm.pl
	fi
}

Install_Mhash()
{
	mhash_status=`cat /www/server/rpm.pl | grep mhash`
	if [ "${mhash_status}" != 'mhash_installed' ]; then
		cd ${run_path}
		if [ ! -f "mhash-0.9.9.9.rpm" ];then
			wget ${Download_Url}/rpm/${rpm_path}/${Is_64bit}/mhash-0.9.9.9.rpm
		fi
		rpm -ivh mhash-0.9.9.9.rpm --force --nodeps
		rm -f mhash-0.9.9.9.rpm
		echo -e "mhash_installed" >> /www/server/rpm.pl
	fi
}

Install_Pcre()
{	
	pcre_status=`cat /www/server/rpm.pl | grep pcre`
	if [ "${pcre_status}" != 'pcre_installed' ]; then
	    Cur_Pcre_Ver=`pcre-config --version`
	    if echo "${Cur_Pcre_Ver}" | grep -vEqi '^8.';then
			cd ${run_path}
			if [ ! -f "pcre-8.36.rpm" ];then
				wget ${Download_Url}/rpm/${rpm_path}/${Is_64bit}/pcre-8.36.rpm
			fi
			rpm -ivh pcre-8.36.rpm --force --nodeps
			rm -f pcre-8.36.rpm
	    fi
	    echo -e "pcre_installed" >> /www/server/rpm.pl
	fi
}

Install_Pcre
Install_Curl
Install_Mhash
Install_Libmcrypt
Install_Mcrypt	
Install_Libiconv