PortSystem      1.0

name				qmail-spamcontrol
version				2.5.19
revision			0
categories			mail
maintainers			nomaintainer
platforms			darwin

description			Qmail, enhanced Spamcontrol version

long_description	        Robust, popular mail system.

extract.mkdir			yes

universal_variant		no

variant relaymailfrom description {Relay based on sender email, not a good idea} {}
variant noreqbrackets description {Do not require brackets in SMTP addresses} {}
variant noverp description {Do not alow VERP addresses for recipients} {}
variant moreipme description {Patch to account for certain NAT or load balance situations} {}
variant bigtodo description {May make very large installations more efficient} {}

homepage			https://www.fehcom.de/qmail/spamcontrol.html

master_sites		        https://www.fehcom.de/qmail/spamcontrol/:spamcontrol \
				https://cr.yp.to/software/:qmail \
				https://qmail-mirror.jms1.net/:qmail \
				http://www.qmail.org/:qmail

distfiles			qmail-1.03.tar.gz:qmail \
				spamcontrol-2519_tgz.bin:spamcontrol

checksums			qmail-1.03.tar.gz \
					md5 622f65f982e380dbe86e6574f3abcb7c \
					sha1 18fb960481291a0503e93a94df3f6094edb7f27a \
					rmd160 b851f273f1d365d38efd949b1efcf35768ffa30f \
				spamcontrol-2519_tgz.bin \
					md5 acca46cc48d8b5cfa9eb3de4d3fc90ad \
					sha1 b259183cb9afd7a574ad598cb2399af5002226ec \
					rmd160 8374835ade0656145ba15eded23c6f8ce0a1b936

worksrcdir		qmail-1.03

patchfiles		patch-dns.c.diff \
			patch-strerr_sys.c.diff \
			patch-qmail-lspawn.c.diff \
			patch-qmail-rspawn.c.diff \
			patch-qmail.c.diff \
			patch-Makefile.diff

configure.cflags 	-O2 -include /usr/include/errno.h -c
build.target		setup-patch
destroot.cmd		./install-destroot

post-extract {

	# Merge Spamcontrol source with Qmail source
	foreach file [glob ${worksrcpath}/qmail-1.03/*] {
		file rename ${file} ${worksrcpath}/
	}
	file delete ${worksrcpath}/qmail-1.03

	# Create an install file to install into destroot since
	# destroot is not supported by qmail
	file copy ${worksrcpath}/conf-qmail ${worksrcpath}/conf-destroot
        reinplace "s|/var/qmail|${destroot}${prefix}/var/qmail|g" \
		${worksrcpath}/conf-destroot
	file copy ${worksrcpath}/install.c ${worksrcpath}/install-destroot.c

	# Change live install dir to reflect the prefix
        reinplace "s|/var/qmail|${prefix}/var/qmail|g" \
		${worksrcpath}/conf-qmail
}

pre-patch {
		# Apply Spamcontrol patches
		foreach file [glob ${worksrcpath}/*.patch] {
			system "patch -d ${worksrcpath} -p2 <${file}"
		}
}

post-patch {

		# Handle the variants, disable via source code define change
		foreach file [glob ${worksrcpath}/*c] {
			if {![variant_isset relaymailfrom]} {
         			reinplace "s|^#define RELAYMAILFROM|\/* #define RELAYMAILFROM *\/|g" \
					${file}
			}
			if {[variant_isset noreqbrackets]} {
         			reinplace "s|^#define REQBRACKETS|\/* #define REQBRACKETS *\/|g" \
					${file}
			}
			if {[variant_isset noverp]} {
         			reinplace "s|^#define VERP|\/* #define VERP *\/|g" \
					${file}
			}
			if {![variant_isset moreipme]} {
         			reinplace "s|^#define MOREIPME|\/* #define MOREIPME *\/|g" \
					${file}
			}
			if {![variant_isset bigtodo]} {
         			reinplace "s|^#define BIGTODO|\/* #define BIGTODO *\/|g" \
					${file}
			}
		}
}

configure {

	# The qmail users and groups are required before compilation
	addgroup qmail gid=2107
	addgroup nofiles gid=2108
	adduser alias uid=7790 gid=[existsgroup nofiles] realname=Qmail-alias-user home=${prefix}/var/qmail shell=/usr/bin/true
	adduser qmaild uid=7791 gid=[existsgroup nofiles] realname=Qmail-SMTP-user home=${prefix}/var/qmail shell=/usr/bin/true
	adduser qmaill uid=7792 gid=[existsgroup nofiles] realname=Qmail-log-user home=${prefix}/var/qmail shell=/usr/bin/true
	adduser qmailp uid=7793 gid=[existsgroup nofiles] realname=Qmail-password-user home=${prefix}/var/qmail shell=/usr/bin/true
	adduser qmailq uid=7794 gid=[existsgroup qmail] realname=Qmail-queue-user home=${prefix}/var/qmail shell=/usr/bin/true
	adduser qmailr uid=7795 gid=[existsgroup qmail] realname=Qmail-remote-user home=${prefix}/var/qmail shell=/usr/bin/true
	adduser qmails uid=7796 gid=[existsgroup qmail] realname=Qmail-send-user home=${prefix}/var/qmail shell=/usr/bin/true
}

pre-destroot {
	# A whole lot of keepdirs, Qmail creates lots of required, empty directories
	destroot.keepdirs \
		${destroot}${prefix}/var/log/qmail/smtpd \
		${destroot}${prefix}/var/qmail/control \
		${destroot}${prefix}/var/qmail/users \
		${destroot}${prefix}/var/qmail/queue/pid \
		${destroot}${prefix}/var/qmail/queue/bounce
	if {![variant_isset bigtodo]} {
		destroot.keepdirs-append \
			${destroot}${prefix}/var/qmail/queue/todo \
			${destroot}${prefix}/var/qmail/queue/intd
	}
	
	for {set i 0} {$i <= 22} {incr i} {
		destroot.keepdirs-append \
			${destroot}${prefix}/var/qmail/queue/info/${i} \
			${destroot}${prefix}/var/qmail/queue/remote/${i} \
			${destroot}${prefix}/var/qmail/queue/local/${i} \
			${destroot}${prefix}/var/qmail/queue/mess/${i}
		if {[variant_isset bigtodo]} {
			destroot.keepdirs-append \
				${destroot}${prefix}/var/qmail/queue/todo/${i} \
				${destroot}${prefix}/var/qmail/queue/intd/${i}
		}
	}
}

post-destroot {
	# Minimal requirements per Qmail install doc
	touch ${destroot}${prefix}/var/qmail/alias/.qmail-postmaster
	touch ${destroot}${prefix}/var/qmail/alias/.qmail-mailer-daemon
	touch ${destroot}${prefix}/var/qmail/alias/.qmail-root

	# Create the log directories
	xinstall -d -o qmaill ${destroot}${prefix}/var/log/qmail/smtpd

	# Copy example files
	xinstall -d ${destroot}${prefix}/var/qmail/samples
	xinstall -m 644 -W ${filespath} README.txt ${destroot}${prefix}/var/qmail/samples
	foreach file {run qmailctl qmail-send-run qmail-send-log-run qmail-smtpd-run qmail-smtpd-log-run} {
		xinstall -W ${filespath} ${file}.in ${destroot}${prefix}/var/qmail/samples/${file}
		reinplace "s%@PREFIX@%${prefix}%g" ${destroot}${prefix}/var/qmail/samples/${file}
	}
	
	# Additional Spamcontrol files
	xinstall -d ${destroot}${prefix}/var/qmail/scripts
	foreach file {qmail-alias2recipients qmail-users2recipients qmail-pwd2recipients qmail-vpopmail2recipients} {
		xinstall -W ${worksrcpath} ${file} ${destroot}${prefix}/var/qmail/scripts
	}
	foreach file {conf-spamcontrol ucspi-ssl-0.70_ucspitls-0.4.patch_ clamav-0.90.1_output.patch_ Makefile.djbdns badmailfrom badmimetypes badloadertypes badrcptto INSTALL.spamcontrol HISTORY.spamcontrol FILES.spamcontrol LICENSE.spamcontrol TODO.spamcontrol LOGGING.spamcontrol SMTPREPLY.spamcontrol PROPOSAL.mav install_spamcontrol.sh} {
		xinstall -m 644 -W ${worksrcpath} ${file} ${destroot}${prefix}/var/qmail/doc
	}
	foreach file [glob -directory ${worksrcpath} README* RELEASE*] {
		xinstall -m 644 ${file} ${destroot}${prefix}/var/qmail/doc
	}

	# User must run this
	xinstall -W ${worksrcpath} config-fast ${destroot}${prefix}/var/qmail/scripts/config-fast

	# Per Qmail install doc
	xinstall ${destroot}${prefix}/var/qmail/boot/home ${destroot}${prefix}/var/qmail/rc.sample
}

post-install {
ui_msg "\n
To control qmail, the daemontools and ucspi-tcp ports are highly recommended.
A good reference for setting up qmail is http://www.lifewithqmail.org/ .
This port includes some sample files based on the Life with Qmail web site.
They can be found in ${prefix}/var/qmail/samples .
Also look at https://www.fehcom.de/qmail/spamcontrol.html for further info.
The fehcom site has docs for all the stuff added to the base qmail software.
There are numerous configuration options to qmail. Please read all the docs!
******************************
NEW INSTALLS ONLY
For now, you must run ${prefix}/var/qmail/scripts/config-fast your.domain.name
to set up some files for your mail server. your.domain.name should point
to this machine you are installing qmail on.
******************************
\n"
}

post-activate {

	# Remove the turd files, it is not known if they cause a problem but
        # people have recommended not having them there.
	system "cd ${prefix}/var/qmail; find . -name .turd_${name} -delete"
}

livecheck.type			regex
livecheck.regex			{SPAMCONTROL\sVersion\s([0-9.]+)}
