# $Id: Portfile 66285 2010-04-08 08:09:09Z jmr@macports.org $
# vim: ts=8 sw=8

PortSystem		1.0
name			zope
version			2.8.7
revision		2
categories		www zope python
maintainers		nomaintainer
description		Object-Oriented Python Web Application Server
long_description 	Zope is a leading open source application server, \
			specializing in content management, portals, and \
			custom applications.
platforms		darwin freebsd
homepage		http://www.zope.org
master_sites		${homepage}/Products/Zope/${version}/${version}
distname		Zope-${version}-final
extract.suffix		.tgz
checksums		sha1 9d8da00c85f93e7b3fa5afc211277fb744ac1f31

# Use Python 2.6 from MacPorts explicitly 
set python		${prefix}/bin/python2.6

# Default Zope User and Password.  The password is changed from what is 
# printed durring the build, as the user will not see that by default.
set zuser		admin
set zpasswd		mypass

# Location to install Zope and the Zope user
set zopehome		${prefix}/libexec/Zope
set zopeinst		${prefix}/www/Zope
set zopeuser		zope
set zopegroup		www

depends_build		port:python26

platform freebsd {
	# FreeBSD calls it gtar, we call it gnutar... need a better solution
	# so we don't have to install gnutar from dports just for this if it
	# exists from FreeBSD ports
	depends_build-append	bin:gnutar:gnutar
}

extract.post_args	| gnutar -xf -

configure.args		--prefix=${zopehome} --with-python=${python}

build.target		default

destroot.destdir	PREFIX=${destroot}${zopehome}

pre-destroot {
	adduser 	${zopeuser} gid=[existsgroup ${zopegroup}]
}

post-destroot {	
	system		"chown -R ${zopeuser} ${destroot}${zopehome}"
	system		"chgrp -R ${zopegroup} ${destroot}${zopehome}"

	system		"${destroot}${zopehome}/bin/mkzopeinstance.py \
			-u ${zuser}:${zpasswd} -d ${destroot}${zopeinst}"
	system		"chown -R ${zopeuser} ${destroot}${zopeinst}"
	system		"chgrp -R ${zopegroup} ${destroot}${zopeinst}"

	file rename	${destroot}${zopeinst}/inituser \
			${destroot}${zopeinst}/inituser-dist
	file delete	${destroot}${zopeinst}/bin/runzope.bat

	reinplace 	"s|^#\[ \t\]*effective-user.*$|&\\\neffective-user ${zopeuser}|" \
			${destroot}${zopeinst}/etc/zope.conf

	reinplace	"s|${destroot}||g" \
			${destroot}${zopeinst}/bin/runzope \
			${destroot}${zopeinst}/bin/zopectl \
			${destroot}${zopeinst}/bin/zopeservice.py \
			${destroot}${zopeinst}/etc/zope.conf

	xinstall	-o ${zopeuser} -m 0644 ${filespath}/version.txt \
			${destroot}${zopehome}/lib/python

	reinplace	"s|%%VERSION%%|${version}|g" \
			${destroot}${zopehome}/lib/python/version.txt

	xinstall	-d -o root -m 0755 ${destroot}${prefix}/bin
	xinstall 	-o root -m 0755 ${filespath}/zopectl \
			${destroot}${prefix}/bin

	reinplace 	"s|%%ZOPEINST%%|${zopeinst}|g" \
			${destroot}${prefix}/bin/zopectl
	reinplace 	"s|%%ZOPEUSER%%|${zopeuser}|g" \
			${destroot}${prefix}/bin/zopectl

	# Create startup rc script (if non-Darwin)
	if {${os.platform} != "darwin"} {
		xinstall -o root -m 0755 -d ${destroot}${prefix}/etc/rc.d
		xinstall -o root -m 0755 ${filespath}/zope.sh.in \
			${destroot}${prefix}/etc/rc.d/zope.sh
		reinplace "s|__ZOPEINST|${zopeinst}|g" \
			${destroot}${prefix}/etc/rc.d/zope.sh
	}
}

pre-install {
	adduser 	${zopeuser} gid=[existsgroup ${zopegroup}]
}

post-activate {
	# If Zope filesystem is not already present, set it up for init
	# This allows proper upgrading of Zope after an existing
	# database has already been created.
	if {![file exists ${zopeinst}/var/Data.fs]} {
		xinstall -o ${zopeuser} -m 0644 ${zopeinst}/inituser-dist ${zopeinst}/inituser
		ui_msg	"The '${zuser}' user's password has been set to '${zpasswd}'.  Please change it."
	}
}

platform darwin {
	startupitem.create	yes
	startupitem.name	Zope
	startupitem.start	"echo \"`date`: \$0: start\" >>${zopeinst}/log/zopectl.log 2>&1"
	startupitem.start-append "${prefix}/bin/zopectl start >>${zopeinst}/log/zopectl.log 2>&1"
	startupitem.stop	"echo \"`date`: \$0: stop\" >>${zopeinst}/log/zopectl.log 2>&1"
	startupitem.stop-append	"${prefix}/bin/zopectl stop >>${zopeinst}/log/zopectl.log 2>&1"
	startupitem.restart	"echo \"`date`: \$0: restart\" >>${zopeinst}/log/zopectl.log 2>&1"
	startupitem.restart-append "${prefix}/bin/zopectl restart >>${zopeinst}/log/zopectl.log 2>&1"
}

