#!/bin/bash
#filename: memcached.sh
# Description: Install Memcache Script.
# Author: shuhui@vqiu.cn
# Create Date: 2014/07/25
# Last Modify: 2015/11/27 (Modify memcached version)

# Define Variables

# Install directory
PREFIX='/usr/local'

# The direcoty of tarballs 
PKG_DIR='/usr/local/src'

# The full path of php.ini
PHP_INI=$(php -i | awk '/Loaded Configuration File/{print $5}')

# The full path of php-config 
PHP_CONFIG=$(which php-config)

# CPU Core
cpu_threads=$(expr $(grep processor /proc/cpuinfo|wc -l) + 1)

# Define Package Version
LIBEVENT_VER='2.0.22'
RE2C_VER='0.13.5'
MEMCACHED_VER='1.4.25'
LIBMEMCACHED_VER='1.0.18'
PHP_MEMCACHED_VER='2.2.0'
PHP_MEMCACHE_VER='3.0.8'

# Memcache Memory Size
MEM='512M'

function isRoot(){
	if [ $(id -u) -ne 0 ];
	then
		echo "ERROR,Please run as root user!";exit 1;
	fi
}

function yum_depend() {
	for i in gcc gcc-c++ automake autoconf
	do
		yum -y install $i>/dev/null 2>&1
	done
}

function libevent() {
	cd ${PKG_DIR};
	wget -c http://down.vqiu.cn/package/tarball/memcached/libevent-${LIBEVENT_VER}-stable.tar.gz;
	tar axvf libevent-${LIBEVENT_VER}-stable.tar.gz
	cd libevent-${LIBEVENT_VER}-stable
	./configure --prefix=${PREFIX}/libevent;make -j${cpu_threads};make install
}

function re2c() {
	wget -c http://down.vqiu.cn/package/tarball/memcached/re2c-${RE2C_VER}.tar.gz;
	tar axvf re2c-${RE2C_VER}.tar.gz
	cd re2c-${RE2C_VER}
	./configure;make -j${cpu_threads};make install
}

function memcached() {
	cd ${PKG_DIR};
	wget -c http://down.vqiu.cn/package/tarball/memcached/memcached-${MEMCACHED_VER}.tar.gz;
	tar axvf memcached-${MEMCACHED_VER}.tar.gz
	cd memcached-${MEMCACHED_VER}
	./configure \
	--prefix=${PREFIX}/memcached \
	--with-libevent=${PREFIX}/libevent \
	--with-64bit \
	--disable-docs
	make -j${cpu_threads};make install
	
	# Env Variables
	echo 'export PATH="$PATH:${PREFIX}/memcached/bin"' > /etc/profile.d/memcached.sh
	source /etc/profile.d/memcached.sh
	
	# Start memcached Service
	${PREFIX}/memcached/bin/memcached -m ${MEM} -u root -p 11211 -d
	echo "${PREFIX}/memcached/bin/memcached -m ${MEM} -u root -p 11211 -d" >> /etc/rc.local
}

function libmemcached() {
	cd ${PKG_DIR};
	wget -c http://down.vqiu.cn/package/tarball/memcached/libmemcached-${LIBMEMCACHED_VER}.tar.gz
	tar zxvf libmemcached-${LIBMEMCACHED_VER}.tar.gz
	cd libmemcached-${LIBMEMCACHED_VER}
	./configure \
	--prefix=${PREFIX}/libmemcached \
	--with-memcached=${PREFIX}/memcached/bin/memcached
	make -j${cpu_threads};make install
}

function php_memcached() {
	# Website http://pecl.php.net/package/memcached/2.2.0
	cd ${PKG_DIR};
	wget -c http://down.vqiu.cn/package/tarball/memcached/memcached-${PHP_MEMCACHED_VER}.tgz;
	tar axvf memcached-${PHP_MEMCACHED_VER}.tgz;
	cd memcached-${PHP_MEMCACHED_VER}
	phpize
	./configure \
	--with-php-config=${PHP_CONFIG} \
	--with-libmemcached-dir=${PREFIX}/libmemcached \
	--disable-memcached-sasl
	make -j${cpu_threads};make install
	if [[  $? -eq 0 ]]
	then 
		echo -en '\n\n[memcached]\nextension = memcached.so' >> ${PHP_INI}
	fi
}


function php_memcache() {
	cd ${PKG_DIR};
	wget -c http://down.vqiu.cn/package/tarball/memcached/memcache-${PHP_MEMCACHE_VER}.tgz;
	tar zxvf memcache-${PHP_MEMCACHE_VER}.tgz;
	cd memcache-${PHP_MEMCACHE_VER}
	phpize
	./configure \
	--with-php-config=${PHP_CONFIG}
	make -j${cpu_threads};make install
	if [[ $? -eq 0 ]]
	then 
		echo -en '\n\n[memcache]\nextension = memcache.so' >> ${PHP_INI}
	fi
}

isRoot
yum_depend

case $1 in 
	all)
		libevent
		re2c
		memcached
		libmemcached
		php_memcached
		#php_memcache
		;;
		
	server)
		yum_depend
		libevent
		re2c
		memcached
		;;
		
	php-memcached)
		libmemcached
		php_memcached
		;;
		
	php-memcache)

		php_memcache
		;;
		
	*)
		echo 'Please Usage all | server | php-memcached | php-memcache' 

esac
