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

ModuleName='pure-ftpd-1.0.36';
ModuleSort='server';
ModuleType='FTPServer';
ModuleMainVersion='1';
ModuleVersion='1.0.36';
ModuleDescription='PureFTPd (Pure FTP Server) 是一个快速、稳定的支持全功能的FTP服务器软件，专门为现在的Linux核心进行设计。支持PAM认证、IPv6、chroot、虚拟域名、内建ls命令、FXP协议、带宽限制、Passive模式、LDAP认证以及XML输出等等。';
ModuleInstallPath=/usr/local/${ModuleName};
ModuleDate='2015-11-09';
ModuleWebSite='http://amh.sh';
ModuleIco='logo.gif';
ModuleScriptBy='amysql';

ModuleProcessTag='pure-ftpd (SERVER)';
ModuleProcessFunctions='cmd:stop,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';

#upgrade
function amh_module_upgrade()
{
	return 0;
}

#install
function amh_module_install()
{
	if amh_module_status ; then
		return;
	else
		cd /tmp;
		amh_wget ${ModuleName}.tar.gz bbcb48e8aa6ec1abff9775b89f84af91;
		tar -zxvf ${ModuleName}.tar.gz;
		cd ${ModuleName};

		rm -rf ${ModuleInstallPath};
		./configure --prefix=${ModuleInstallPath} --with-puredb --with-quotas --with-throttling --with-ratios --with-peruserlimits;
		amh_make_install;

		\cp contrib/redhat.init ${ModuleInstallPath}/sbin/;
		chmod 755 ${ModuleInstallPath}/sbin/redhat.init;
		sed -i "s#/usr/local#${ModuleInstallPath}#" ${ModuleInstallPath}/sbin/redhat.init;
		sed -i "s#/var/run/pure-ftpd.pid#${ModuleInstallPath}/pure-ftpd.pid#" ${ModuleInstallPath}/sbin/redhat.init;
		sed -i "s#/etc/pure-ftpd.conf#${ModuleInstallPath}/pure-ftpd.conf#" ${ModuleInstallPath}/sbin/redhat.init;

		\cp ${amh_home}/modules/${ModuleName}/pure-ftpd.conf ${ModuleInstallPath};
		chmod 744 ${ModuleInstallPath}/pure-ftpd.conf;

		mkdir ${ModuleInstallPath}/etc/;
		touch ${ModuleInstallPath}/etc/pureftpd.passwd;

		\cp configuration-file/pure-config.pl ${ModuleInstallPath}/sbin/pure-config.pl;
		chmod 755 ${ModuleInstallPath}/sbin/pure-config.pl;
		sed -i "s#/usr/local#${ModuleInstallPath}#" ${ModuleInstallPath}/sbin/pure-config.pl;

		groupadd ftpgroup;
		useradd -d /home/wwwroot/ -s /sbin/nologin -g ftpgroup ftpuser;

		/sbin/iptables-save > /etc/amh-iptables;
		sed -i '/--dport 21 -j ACCEPT/d' /etc/amh-iptables;
		sed -i '/--dport 10100:10110 -j ACCEPT/d' /etc/amh-iptables;
		/sbin/iptables-restore < /etc/amh-iptables;
		/sbin/iptables -I INPUT -p tcp --dport 21 -j ACCEPT;
		/sbin/iptables -I INPUT -p tcp --dport 10100:10180 -j ACCEPT;
		/sbin/iptables-save > /etc/amh-iptables;
		echo 'IPTABLES_MODULES="ip_conntrack_ftp"' >>/etc/sysconfig/iptables-config;
		amh_module_status && amh_module_start;
		cd /tmp/;
		rm -rf /tmp/${ModuleName} /tmp/${ModuleName}.tar.gz;
	fi;
}

#boot
function amh_module_boot()
{
	amh_module_start;
}

#start
function amh_module_start()
{
	if amh_module_status ; then
		${ModuleInstallPath}/sbin/redhat.init start;
	else
		return 1;
	fi;
}

#stop
function amh_module_stop()
{
	if amh_module_status ; then
		${ModuleInstallPath}/sbin/redhat.init stop;
	else
		return 1;
	fi;
}

#restart
function amh_module_restart()
{
	if amh_module_status ; then
		${ModuleInstallPath}/sbin/redhat.init restart;
	else
		return 1;
	fi;
}

#uninstall
function amh_module_uninstall()
{
	if amh_module_status ; then
		amh_module_stop;
		rm -rf ${ModuleInstallPath} && \
		echo "[OK] ${ModuleName} Uninstall successful." && return 0;
		return 1;
	else
		exit;
	fi;
}

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