# $Id: Portfile,v 1.9 2006/02/26 05:23:56 markd Exp $

PortSystem	1.0

name				vm-pop3d  
version				1.1.6
revision			2
categories			mail
maintainers			markd@opendarwin.org
platforms			darwin

description			POP3 server for Postfix that supports virtual domains and mailboxes

long_description	        A POP3 server that supports virtual domains and virtual mailboxes \
				such that users and mailboxes need not be associated with local\
				Unix accounts.

homepage			http://www.reedmedia.net/software/virtualmail-pop3d/
					
master_sites		        http://www.ibiblio.org/pub/Linux/system/mail/pop/ \
				ftp://sunsite.unc.edu/pub/Linux/system/mail/pop/

checksums			md5 0399cc06f5329a6eaebec05d959a6ec1

patchfiles                      patch-vm-pop3d.init

destroot.destdir		prefix=${destroot}${prefix}

depends_lib			port:postfix

configure.args			--enable-virtual


variant server {

    startupitem.create    yes
    startupitem.start "${prefix}/share/${name}/vm-pop3d.init start"
    startupitem.stop "${prefix}/share/${name}/vm-pop3d.init stop"
}

pre-configure {
# Set vm-pop3d.h apop file path
	reinplace "s|/etc/apop|${prefix}/etc/${name}/apop|g" \
                "${worksrcpath}/vm-pop3d.h"

# Set vm-pop3d.h virtual directory domain spool path
	reinplace "s|/var/spool/virtual|${prefix}/var/${name}/spool|g" \
                "${worksrcpath}/vm-pop3d.h"

# Set vm-pop3d.h virtual directory domain password path
	reinplace "s|/etc/virtual|${prefix}/etc/${name}|g" \
                "${worksrcpath}/vm-pop3d.h"
}

post-destroot {
# Create spool and passwd file paths for the virtual domains
        xinstall -m 755 -d ${destroot}${prefix}/var/${name}/spool
        xinstall -m 755 -d ${destroot}${prefix}/etc/${name}
	system "chown postfix:postfix ${destroot}${prefix}/var/${name}/spool"

# Create lock file path
	xinstall -m 755 -d ${destroot}${prefix}/var/${name}/lock

# Keep the empty virtual spool and passwd file paths
destroot.keepdirs \
        ${destroot}${prefix}/var/${name}/spool \
        ${destroot}${prefix}/var/${name}/lock \
	${destroot}${prefix}/etc/${name}

# Install documentation type stuff
	xinstall -d ${destroot}${prefix}/share/doc/${name}
	xinstall -m 644 ${worksrcpath}/AUTHORS ${destroot}${prefix}/share/doc/${name}
	xinstall -m 644 ${worksrcpath}/CHANGES ${destroot}${prefix}/share/doc/${name}
	xinstall -m 644 ${worksrcpath}/COPYING ${destroot}${prefix}/share/doc/${name}
	xinstall -m 644 ${worksrcpath}/FAQ ${destroot}${prefix}/share/doc/${name}
        xinstall -m 644 ${worksrcpath}/INSTALL ${destroot}${prefix}/share/doc/${name}
        xinstall -m 644 ${worksrcpath}/README ${destroot}${prefix}/share/doc/${name}
	xinstall -m 644 ${worksrcpath}/TODO ${destroot}${prefix}/share/doc/${name}

# Fix and copy the init script
	reinplace "s|daemon vm-pop3d|${prefix}/sbin/vm-pop3d --user postfix --group postfix|g" \
                "${worksrcpath}/vm-pop3d.init"

	reinplace "s|/usr/local/sbin|${prefix}/sbin|g" \
                "${worksrcpath}/vm-pop3d.init"

	reinplace "s|/var/lock/subsys|${prefix}/var/${name}/lock|g" \
                "${worksrcpath}/vm-pop3d.init"

	reinplace "s|/etc/rc.d/init.d/functions|/etc/rc.common|g" \
		"${worksrcpath}/vm-pop3d.init"

	xinstall -d -m 755  ${destroot}${prefix}/share/${name}
	
	xinstall -m 755 ${worksrcpath}/vm-pop3d.init ${destroot}${prefix}/share/${name}
}

post-activate {

ui_msg "\n#### To setup vm-pop3d with Postfix ####

Notice: The paths to files used by vm-pop3d are hard coded at compile time so
         if you deviate from the paths described below vm-pop3d will not work.

1) Setup Postfix (if first-time setup) and create spool directories
   for your vm-pop3d virtual domains.

	cd ${prefix}/etc/postfix
	sudo cp master.cf.sample master.cf
	sudo cp main.cf.sample main.cf
 
	sudo mkdir ${prefix}/var/${name}/spool/my-vdomain1.com
	sudo chown postfix:postfix ${prefix}/var/${name}/spool/my-vdomain1.com
	sudo chmod 775 ${prefix}/var/${name}/spool/my-vdomain1.com


2) Configure Postfix for virtual mailboxes

   Add to main.cf

	virtual_mailbox_base = ${prefix}/var/${name}/spool 
	virtual_mailbox_maps = hash:${prefix}/etc/${name}/vmailbox_maps
	virtual_mailbox_domains = hash:${prefix}/etc/${name}/vdomain_maps
	virtual_uid_maps = static:<postfix user's uid>
	virtual_gid_maps = static:<postfix user's gid>
	virtual_mailbox_lock = dotlock
	virtual_minimum_uid = <postfix user's uid>

   NOTE: Do NOT include the virtual domain name in main.cf's \$mydestination
	 or Postfix will reject mail to this domain since local (Unix) accounts
	 for virtual domains do not exist.


3) Create virtual mailbox lookup tables

	sudo touch ${prefix}/etc/${name}/vdomain_maps (contents below)

   my-vdomain1.com     placeholder (any text)
   my-vdomain2.com     placeholder

	sudo touch ${prefix}/etc/vm-pop3d/vmailbox_maps (contents below)

   user1@my-vdomain1.com         my-vdomain1.com/user1
   user2@my-vdomain1.com         my-vdomain1.com/user2

	sudo postmap ${prefix}/etc/${name}/vdomain_maps (make .db hash files)
	sudo postmap ${prefix}/etc/${name}/vmailbox_maps


4) Restart Postfix and test SMTP message delivery to virtual domains

	sudo postfix reload

   Now send an email to an account in a virtual domain; look here to see if
   messages are arriving:

	ls ${prefix}/var/${name}/spool/my-vdomainx.com for messages.

   Check the error logs for either delivery errors or (after next step)
   POP errors:

	tail /var/log/mail.log


5) Create virtual domain passwd files, substituting real domain names for my-vdomain

	cd ${prefix}/etc/${name}
	sudo mkdir my-vdomain1.com
	cd my-vdomain1.com
	sudo htpasswd -c passwd user1 (-c creates the file 'passwd' to hold the 1st vdomain1 user)
	sudo htpasswd passwd user2 (don't need -c for subsequent users of the vdomain1)


6) Set vm-pop3d to launch at startup with launchctl, start it, and test POP access to virtual domains with telnet

   OS X 10.4 - Run launchctl so Nagios will start at system boot
	sudo launchctl load -w /Library/LaunchDaemons/org.darwinports.vm-pop3d.plist

   OS X 10.3 and previous - A startup item was created in /Library/StartupItems
        Add \"VMPOP-3D=-YES-\" to the /etc/hostconfig file to run vm-pop3d at system boot

   Start vm-pop3d manually:
	sudo /opt/local/share/vm-pop3d/vm-pop3d.init start (stop|status)

	telnet localhost 110  (response ->)  +OK POP3 Welcome to vm-pop3d 1.x.x
	USER <user@domain>
	PASS <password>
	LIST (to see messages)
	RETR 1 (to view message #1)

   Documentation is in ${prefix}/share/doc/${name}\n"
}
