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

ModuleName='php-5.5.13';
ModuleSort='server';
ModuleType='script';
ModuleMainVersion='5.5';
ModuleVersion='5.5.13';
ModuleDescription='php5.5版本，GD扩展多方面做了改进与新增更多函数支持、语法增加很多新特性等。PHP是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点，入门门槛较低，易于学习，使用广泛，主要适用于Web开发领域。';
ModuleInstallPath=/usr/local/${ModuleName};
ModuleSbin=/usr/local/${ModuleName}/sbin/php-fpm;
ModuleDate='2014-04-01';
ModuleWebSite='http://amh.sh';
ModuleIco='logo.gif';
ModuleScriptBy='amysql';

ModuleInstallFunctions='cmd:uninstall,txt:卸载,class:red';
ModuleNotInstallFunctions='cmd:install,txt:安装|cmd:delete,txt:删除,class:red';



#init
function amh_module_init()
{
	if grep ${ModuleName} ${amh_www}/etc/amh-base.conf 2>/dev/null; then
		ModuleInstallFunctions='';
	fi;
}

#install
function amh_module_install()
{
	if amh_module_status ; then
		return;
	else
		amh_yum_install openssl openssl-devel libxml2-devel autoconf zlib-devel curl-devel pcre-devel libtool-libs libjpeg-devel libpng-devel freetype-devel ;
		amh_apt_install openssl libxml2-dev autoconf libcurl4-gnutls-dev autoconf2.13 zlib1g-dev libpcre3-dev libtool libjpeg-dev libpng12-dev libfreetype6-dev libssl-dev;
		if [ -f "/usr/include/freetype2/freetype.h" ]; then
			mkdir -p /usr/include/freetype2/freetype;
			cp -a /usr/include/freetype2/freetype.h /usr/include/freetype2/freetype;
		fi;

		cd /tmp/;
		amh_wget ${ModuleName}.tar.gz 32d0fc26fccdb249a918c0e01ffb7b82;
		tar -zxvf ${ModuleName}.tar.gz;
		cd ${ModuleName};

		rm -rf /tmp/ext;
		cp -a ext /tmp/;

		groupadd www;
		useradd -m -s /sbin/nologin -g www www;
		./configure --prefix=${ModuleInstallPath} --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-config-file-path=${ModuleInstallPath}/etc --with-openssl --with-zlib  --with-curl --enable-ftp --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-exif --enable-mbstring --enable-zip --with-iconv=/usr/local/libiconv-1.14  --without-pear --disable-fileinfo;
		amh_make_install;
		mkdir ${ModuleInstallPath}/var/run/pid;
		
		rm -rf /tmp/${ModuleName}.tar.gz /tmp/${ModuleName};
		mv /tmp/ext ${ModuleInstallPath}/extcode;
		amh_module_status;
	fi;
}


#uninstall
function amh_module_uninstall()
{
	if amh_module_status ; then
		if grep ${ModuleName} ${amh_www}/etc/amh-base.conf >/dev/null; then
			echo "[Notice] ${ModuleName} Uninstall not allowed.";
			return 1;
		fi;

		rm -rf ${ModuleInstallPath} && \
		echo "[OK] ${ModuleName} Uninstall successful." && return 0;
		return 1;
	else
		return;
	fi;
}

#status
function amh_module_status()
{
	if [ -f "${ModuleInstallPath}/sbin/php-fpm" ]; then
		echo "[OK] ${ModuleName} is already installed.";
		return 0;
	else
		echo "[Notice] ${ModuleName} is not installed.";
		return 1;
	fi;
}
