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

ModuleName='nginx-tengine';
ModuleSort='server';
ModuleType='WEBServer';
ModuleMainVersion='2.1';
ModuleVersion='2.1.2';
ModuleDescription='Tengine完全兼容Nginx，因此可以参照Nginx的方式来配置Tengine。我们在此只列出Tengine中值得注意的功能。 
关于详细的Tengine的相对于Nginx的差别，可以访问"http://tengine.taobao.org/changelog_cn.html"查看变更列表。';
ModuleInstallPath=/usr/local/${ModuleName};
ModuleDate='2016-06-01';
ModuleWebSite='http://tengine.taobao.org';
ModuleIco='logo.png';
ModuleScriptBy='测试';

ModuleProcessTag="nginx: master process ${ModuleInstallPath}";
ModuleProcessFunctions='cmd:stop,txt:停止|cmd:reload,txt:重载|cmd:restart,txt:重启';
ModuleNotProcessFunctions='cmd:start,txt:启动';
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='';
		ModuleProcessFunctions='cmd:reload,txt:重载';
	fi;
}

#install
function amh_module_install()
{
	if amh_module_status ; then
		return;
	else
		amh_yum_install openssl openssl-devel pcre-devel zlib-devel;
		amh_apt_install openssl libssl-dev libpcre3 libpcre3-dev;

		cd /tmp/;
		wget http://tengine.taobao.org/download/tengine-2.1.2.tar.gz;
		tar -zxvf tengine-2.1.2.tar.gz;
		mv tengine-2.1.2 ${ModuleName};
		cd ${ModuleName};
		
		./configure --prefix=${ModuleInstallPath} --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_concat_module=shared;
		amh_make_install;

		cp ${amh_home}/modules/${ModuleName}/fcgi.conf ${ModuleInstallPath}/conf/fcgi.conf;
		cp ${amh_home}/modules/${ModuleName}/nginx.conf ${ModuleInstallPath}/conf/nginx.conf;

		# 备份
		#\cp ${amh_home}/modules/amh-5.1/AMHScript ${amh_home}/modules/amh-5.1/AMHScript.backup
		# 兼容 tengine
		#sed -i "s/grep nginx/grep ngin/g" ${amh_home}/modules/amh-5.1/AMHScript
		
		cd /tmp/;
		rm -rf /tmp/${ModuleName} /tmp/${ModuleName}.tar.gz openssl-1.0.1g openssl-1.0.1g.tar.gz;
		amh_module_start;
		amh_module_status; 
	fi;
}

#boot
function amh_module_boot()
{
	amh_module_start;
}

#start
function amh_module_start()
{
	if amh_module_status ; then
		nginx_pid=`cat ${ModuleInstallPath}/logs/nginx.pid 2>/dev/null`;
		! amh_pid_status $nginx_pid && ${ModuleInstallPath}/sbin/nginx;

		nginx_pid=`cat ${ModuleInstallPath}/logs/nginx.pid 2>/dev/null`;
		amh_pid_status $nginx_pid && echo "[OK] ${ModuleName} start";
	else
		return 1;
	fi;
}

#stop
function amh_module_stop()
{
	if amh_module_status ; then
		kill -INT `cat ${ModuleInstallPath}/logs/nginx.pid` && echo "[OK] ${ModuleName} stop";
	else
		return 1;
	fi;
}

#restart
function amh_module_restart()
{
	if amh_module_status ; then
		kill -INT `cat ${ModuleInstallPath}/logs/nginx.pid`;
		${ModuleInstallPath}/sbin/nginx;

		nginx_pid=`cat ${ModuleInstallPath}/logs/nginx.pid 2>/dev/null`;
		amh_pid_status $nginx_pid && echo "[OK] ${ModuleName} restart";
	else
		return 1;
	fi;
}

#reload
function amh_module_reload()
{
	if amh_module_status ; then
		kill -HUP `cat ${ModuleInstallPath}/logs/nginx.pid` && echo "[OK] ${ModuleName} reload";
	else
		return 1;
	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;

		amh_module_stop;
		rm -rf ${ModuleInstallPath} && \
		# 还原
		#\cp ${amh_home}/modules/amh-5.1/AMHScript.backup ${amh_home}/modules/amh-5.1/AMHScript
		echo "[OK] ${ModuleName} Uninstall successful." && return 0;
		return 1;
	else
		return;
	fi;
}

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