#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin;

#info
function amh_module_info()
{
	echo 'AMH-ModuleName: Xcache-3.0.4';
	echo 'AMH-ModuleDescription: XCache 是一个又快又稳定的 PHP opcode 缓存器. 经过良好的测试并在大流量/高负载的生产机器上稳定运行. 经过(在 linux 上)测试并支持所有现行 PHP 分支的最新发布版本.';
	echo 'AMH-ModuleButton: 安装/卸载';
	echo 'AMH-ModuleDate: 2013-07-31';
	echo 'AMH-ModuleAdmin: ./xcache/index.php';
	echo 'AMH-ModuleWebSite: http://www.mf8.biz';
	echo 'AMH-ModuleIco: http://amysql.com/View/images/amh_module/xcache.png';
	echo 'AMH-ModuleScriptBy: mf8.biz';
}

#install
function amh_module_install()
{
	if amh_module_status ; then
		exit;
	else
		cd /usr/local/;
		wget http://xcache.lighttpd.net/pub/Releases/3.0.4/xcache-3.0.4.tar.gz;
		tar -zxvf xcache-3.0.4.tar.gz;
		cd xcache-3.0.4;
		/usr/local/php/bin/phpize;
		./configure --with-php-config=/usr/local/php/bin/php-config --enable-xcache--enable-xcache-coverager --enable-xcache-optimizer;
		make && make install;
		touch /tmp/xcache;
		chmod 777 /tmp/xcache;
		cpu_count=`cat /proc/cpuinfo |grep -c processor`
cat >xcache.ini<<EOF
[xcache-common]
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/xcache.so
[xcache.admin]
xcache.admin.enable_auth = On
xcache.admin.user = "amh"
xcache.admin.pass = "9befba9b6ff8b20ef851097bf4d2a8cc"
[xcache]
xcache.shm_scheme ="mmap"
xcache.size=64M
xcache.count =$cpu_count
xcache.slots =8K
xcache.ttl=0
xcache.gc_interval =0
xcache.var_size=4M
xcache.var_count =2
xcache.var_slots =8K
xcache.var_ttl=0
xcache.var_maxttl=0
xcache.var_gc_interval =300
xcache.test =Off
xcache.readonly_protection = On
xcache.mmap_path ="/tmp/xcache"
xcache.coredump_directory =""
xcache.cacher =On
xcache.stat=On
xcache.optimizer =Off
[xcache.coverager]
xcache.coverager =On
xcache.coveragedump_directory =""
EOF
sed -i '/;eaccelerator/ {
r xcache.ini
}' /etc/php.ini
    rm -rf xcache.ini
	amh php reload;
	amh_module_status;
	cp -a ./htdocs /home/wwwroot/index/web;
	mv /home/wwwroot/index/web/htdocs /home/wwwroot/index/web/xcache;
	fi;
}

#admin 
function amh_module_admin()
{
	if amh_module_status ; then
		echo '[OK] Xcache-3.0.4  Management: http://ip:8888/xcache/index.php';
	else
		exit;
	fi;
}

#uninstall
function amh_module_uninstall()
{
	if amh_module_status ; then
		cd /usr/local/;
		rm -rf /tmp/xcache;
		rm -rf xcache-3.0.4.tar.gz;
		rm -rf xcache-3.0.4;
		rm -rf /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/xcache.so;
		rm -rf /home/wwwroot/index/web/xcache

		sed -i "/xcache.*/d" /etc/php.ini;
		sed -i "/\[xcache-common\]/d" /etc/php.ini;
		sed -i "/\[xcache.admin\]/d" /etc/php.ini;
		sed -i "/\[xcache.coverager\]/d" /etc/php.ini;
		amh php reload;
		echo '[OK] xcache-3.0.4 Uninstall successful.';
	else
		exit;
	fi;
}

#status
function amh_module_status()
{
	if grep -q '\[xcache\]' /etc/php.ini; then
		echo '[OK] xcache-3.0.4 is already installed.';
		return 0;
	else
		echo '[Notice] xcache-3.0.4 is not installed.';
		return 1;
	fi;
}

