# $Id: Portfile,v 1.36 2006/02/16 21:09:08 mww Exp $

PortSystem 1.0

name			postgresql8
version			8.1.3
categories		databases
platforms		darwin
maintainers		mww@opendarwin.org
description		The most advanced open-source database available anywhere
long_description PostgreSQL is Object-Relational Database Management System. \
				It supports almost all SQL constructs, including transactions, \
				subselects, and user-defined types and functions. Write-ahead \
				logging means increased performance while retaining data \
				integrity.

homepage		http://www.postgresql.org/
master_sites	postgresql:source/v${version}
set dbase		postgresql-base-${version}.tar.bz2
set dopt		postgresql-opt-${version}.tar.bz2
set dtest		postgresql-test-${version}.tar.bz2
distfiles		${dbase} ${dopt} ${dtest}

checksums		${dbase} sha1 64fdc0c0d90a4998aea663f5b761e78a5588661a \
				${dopt} sha1 90361a36aa54dd3356d324b3e9b0a8df99e887d4 \
				${dtest} sha1 b33778af51193b4d0bf17d8f98ef42e265832891
use_bzip2		yes

depends_lib		port:readline port:openssl port:zlib
depends_build	port:bison

worksrcdir		postgresql-${version}

set libdir		${prefix}/lib/pgsql8
configure.args	--sysconfdir=${prefix}/etc/pgsql8 \
				--bindir=${libdir}/bin \
				--libdir=${libdir} \
				--includedir=${prefix}/include/pgsql8 \
				--datadir=${prefix}/share/${name} \
				--mandir=${prefix}/share/man \
				--with-docdir=${prefix}/share/doc/${name} \
				--with-includes=${prefix}/include \
				--with-libraries=${prefix}/lib \
				--with-openssl \
				--with-rendezvous \
				--with-readline \
				--with-zlib \
				--enable-thread-safety \
				--enable-integer-datetimes \
				--enable-multibyte \
				--enable-unicode
configure.env	CPPFLAGS=-I${prefix}/include \
				CFLAGS="-I${worksrcpath}/src/interfaces/libpq -I${prefix}/include" \
				LDFLAGS="-L${prefix}/lib"

build.type		gnu
build.target

test.run		yes
test.target		check

set dbdir		${prefix}/var/db/pgsql8/defaultdb
set logdir		${prefix}/var/log/pgsql8
set dbuser		postgres8
set dbgrp		postgres
set contribs	"fulltextindex fuzzystrmatch pg_buffercache pg_trgm tsearch2 vacuumlo"

post-build {
	foreach contrib ${contribs} {
		system "cd ${worksrcpath}/contrib/${contrib}/ && ${build.cmd}"
	}
}

post-destroot {
	foreach contrib ${contribs} {
		system "echo contrib: \"${contrib}\""
		system "cd ${worksrcpath}/contrib/${contrib}/ && \
			${build.cmd} install ${destroot.destdir} && touch DONE"
	}
	system "cd ${destroot}${prefix}/bin && ln -sf ${libdir}/bin/psql psql8"
	file delete -force ${destroot}${prefix}/share/doc/${name}
}

variant python {
	depends_lib-append		port:python24
	configure.args-append	--with-python
	configure.env-append	PYTHON=${prefix}/bin/python2.4
}

variant krb5 {
#	depends_lib-append			bin:kerberos:kerberos5
	configure.args-append		--with-krb5
}

variant perl {
	depends_lib-append			bin:perl:perl5.8
	configure.args-append		--with-perl
}

platform darwin 8 {
	configure.env-append	CC=/usr/bin/gcc-4.0 CPP=/usr/bin/cpp-4.0 CXX=/usr/bin/g++-4.0
}

variant server {
	startupitem.create	yes
	startupitem.init	\
"PGCTL=${libdir}/bin/pg_ctl"
	startupitem.start	\
"su ${dbuser} -c \"\${PGCTL} -D \${POSTGRESQL8DATA:=${dbdir}} start -l ${logdir}/postgres.log\""
	startupitem.stop	\
"su ${dbuser} -c \"\${PGCTL} -D \${POSTGRESQL8DATA:=${dbdir}} stop -s -m fast\""

	pre-destroot {
		addgroup ${dbgrp}
		set gid [existsgroup ${dbgrp}]
		adduser ${dbuser} shell=/bin/sh gid=${gid} \
			home=${prefix}/var/db/pgsql8 \
			realname=PostgreSQL-8\ Server
		xinstall -m 755 -d ${destroot}${logdir}
		system "touch ${destroot}${logdir}/postgres.log"
		system "chown ${dbuser}:${dbgrp} ${destroot}${logdir}/postgres.log"
	}

	post-install {
		ui_msg "\nTo create a database instance, after install do\n\
			sudo mkdir -p ${dbdir}\n\
			sudo chown ${dbuser}:${dbgrp} ${dbdir}\n\
			sudo su ${dbuser} -c \'${libdir}/bin/initdb -D ${dbdir}\'"
		ui_msg "\nTo activate the PostgreSQL8 server, add\n\
			POSTGRESQL8=-YES-\n\
			to your ${prefix}/etc/rc.conf"
		ui_msg "\nTo change the location of your database, add\n\
			POSTGRESQL8DATA=/location\n\
			to your ${prefix}/etc/rc.conf"
		ui_msg "\nTo tweak your DBMS, consider increasing kern.sysv.shmmax\
			by adding an increased kern.sysv.shmmax .. to /etc/sysctl.conf"
	}
}

