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

ModuleName='nginx-1.18';
ModuleSort='server';
ModuleType='WEBServer';
ModuleMainVersion='1.18';
ModuleVersion='1.18.0';
ModuleDescription='Nginx-1.18稳定版本。Nginx是一款轻量级的Web 服务器，并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发，供俄国大型的入口网站及搜索引擎Rambler使用。其特点是占有内存少，并发能力强。';
ModuleInstallPath=/usr/local/${ModuleName};
ModuleDate='2020-10-01';
ModuleWebSite='http://amh.sh';
ModuleIco='logo.gif';
ModuleScriptBy='amysql';

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';
ModuleVid='1';


#init
function amh_module_init()
{
	if grep ${ModuleName} ${amh_www}/etc/amh-base.conf 2>/dev/null; then
		ModuleInstallFunctions='';
		ModuleProcessFunctions='cmd:reload,txt:重载';
	fi;

	if [ -f /usr/local/${ModuleName}/sbin/nginx ] && ! /usr/local/${ModuleName}/sbin/nginx -V 2>&1 | grep -q "$ModuleVersion" >/dev/null; then
		ModuleInstallFunctions='cmd:upgrade,txt:安装升级';
	fi;
}

#upgrade
function amh_module_upgrade()
{
	if ! amh_module_status ; then
		amh_module_install;
	else
		/usr/local/${ModuleName}/sbin/nginx -V 2>&1 | grep -q "$ModuleVersion " && return;
		nginx_v=$ModuleName;
		! /usr/local/$nginx_v/sbin/nginx -t && return;
		[ -d /usr/local/${ModuleName} ] && \cp -a /usr/local/${ModuleName} /usr/local/${ModuleName}-p1;

		amh_yum_install openssl openssl-devel pcre-devel zlib-devel;
		amh_apt_install openssl libssl-dev libpcre3 libpcre3-dev;

		mkdir -p /home/usrdata/${ModuleName};
		cd /home/usrdata/${ModuleName};
		amh_wget openssl-1.0.2u.tar.gz cdc2638f789ecc2db2c91488265686c1;
		tar -zxvf openssl-1.0.2u.tar.gz;
		rm -f openssl-1.0.2u.tar.gz;

		cd /tmp/;
		amh_wget nginx-${ModuleVersion}.tar.gz b2d33d24d89b8b1f87ff5d251aa27eb8;
		tar -zxvf nginx-${ModuleVersion}.tar.gz;
		cd nginx-${ModuleVersion};
		
		nginx_configure=`/usr/local/${nginx_v}/sbin/nginx -V 2>&1 | grep 'configure arguments' | cut -d: -f2`;
		nginx_configure=${nginx_configure//$nginx_v/$ModuleName};
		nginx_configure=${nginx_configure//openssl-1.0.2h/openssl-1.0.2u};
		./configure $nginx_configure;
		amh_make_install;


		cd /usr/local/;
		if /usr/local/${ModuleName}/sbin/nginx -V 2>&1 | grep -q "$ModuleVersion"; then
			[ -d /usr/local/${ModuleName}-p1 ] && \cp -a /usr/local/${ModuleName}-p1/conf /usr/local/${ModuleName};
			kill -INT `cat /usr/local/${nginx_v}/logs/nginx.pid`;
			${ModuleInstallPath}/sbin/nginx;
			rm -rf /usr/local/${ModuleName}-p1;
			rm -rf /tmp/nginx-${ModuleVersion} /tmp/nginx-${ModuleVersion}.tar.gz ;
			return 0;
		else
			rm -rf /usr/local/${ModuleName};
			[ -d /usr/local/${ModuleName}-p1 ] && mv /usr/local/${ModuleName}-p1 /usr/local/${ModuleName};
			rm -rf /tmp/nginx-${ModuleVersion} /tmp/nginx-${ModuleVersion}.tar.gz ;
			return 1;
		fi;
	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;

		mkdir -p /home/usrdata/${ModuleName};
		cd /home/usrdata/${ModuleName};
		amh_wget openssl-1.0.2u.tar.gz cdc2638f789ecc2db2c91488265686c1;
		tar -zxvf openssl-1.0.2u.tar.gz;
		rm -f openssl-1.0.2u.tar.gz;

		cd /tmp/;
		amh_wget nginx-${ModuleVersion}.tar.gz b2d33d24d89b8b1f87ff5d251aa27eb8;
		tar -zxvf nginx-${ModuleVersion}.tar.gz;
		cd nginx-${ModuleVersion};
		
		./configure --prefix=${ModuleInstallPath} --user=www --group=www --with-http_ssl_module --with-openssl=/home/usrdata/${ModuleName}/openssl-1.0.2u --with-http_gzip_static_module --with-http_v2_module --with-threads --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module ;
		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;
		
		cd /tmp/;
		rm -rf /tmp/nginx-${ModuleVersion} /tmp/nginx-${ModuleVersion}.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} && \
		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;
}
